15

5 Steps to Create Simple Login Script in PHP and MySql

This is a simple login script using php and mysql. In this script, a form will be displayed with two fields, username and password. When user is submitting with valid user name and password, then he can access authenticated page. Other wise user again has to fill in the form. This is a continuation from the last article Simple User Registration Script in PHP and MySql. If you want to follow next article these are the links.

  1. Simple User Registration Script in PHP and MySQL
  2. Simple Login Script in PHP and MySQL
  3. User Login with activation check using PHP and MySQL
  4. Send Forgotten Password by mail using PHP and MySQL

Steps to create Simple Login Script in PHP and mysql

  1. First step we will connect to the database.
  2. Then we will select the database.
  3. We are checking, if the form is submitted or not. In this step we have two logic’s.
    • What if the form is submitted.
      1. If the form is submitted, we are assigning the posted values to variables and checking the values are existing in the database or not.
      2. If the values submitted in the form and the values in the database are equal, then we will create a session for the user.
      3. If the values are not equal then it will display an error message.
      4. And then we checks for the session, if the session exists we will great him with the username, otherwise the form will be displayed.
    • What if not the form is submitted.
      1. When the user comes first time, he will be displayed with a simple form.
      2. User Name, Password and a submit button.

Below is the My Video on User Login, Registration Script in PHP & MySQL

Buy My Complete Video Course at $19 Instead $120

More than 3000+ stdents Joined, It’s 5 star rated course.

You can get My complete video course with all project files for as low as $19 only 10 coupons available

If you missed the above offer, you can use these below offers.

Access for $29 only 20 coupons available

Access for $39 only 50 coupons available

Hurry!! If these coupons runs out, you have to get this course at full price $120.

1. Create a Databese Table

If you are already following from previous article, you should already have database table created. If you don’t have create the table.

2. Creating HTML Form

This is the form, only displayed if message variable in not set.

3. Adding styles to form

And the styles for the form, if you have added styles in previous article. Skip this step.

4. Connect to Database

If you are following from previous user registration article, no need to create this file. Other wise create connect.php file.

5. PHP Logic for User Login

And this is the PHP code for logging in user

Logout.php

Complete Code of login.php

If you want to learn other things you can read other posts – registration, login, user login with activation check, and sending forgotten password by email .

I hope this article helped you to learn user login using PHP and MySQL . To get latest news and updates follow us on twitter & facebook, subscribe to newsletter. If you have any feedback please let us know by using comment form.

Buy My Complete Video Course at $19 Instead $120

More than 3000+ stdents Joined, It’s 5 star rated course.

You can get My complete video course with all project files for as low as $19 only 10 coupons available

If you missed the above offer, you can use these below offers.

Access for $29 only 20 coupons available

Access for $39 only 50 coupons available

Hurry!! If these coupons runs out, you have to get this course at full price $120.

Incoming search terms:

  • simple login form in php
  • php login script
  • simple php login script
  • simple login php
  • simple login page in php
  • login php mysql
  • php simple login
  • login script php
  • login php script
  • simple login in php
Vivek Vengala
 

Vivek Vengala is a Online Entrepreneur, Web Developer from Hyderabad India.

Click Here to Leave a Comment Below 15 comments
Max

Step 5 add a “?>” in the end : )

Reply
Alan

how do i log out?

Reply
Sarah Ellix

Do you have any code for logging out?

Reply
stufftrivia

dude thank uuuuuu… after 2 dayss of surfing .. my code working …!!! greatest website everr ..!!!! haha 😀

Reply
ganesh

Nice job Its helpful code for beginners

Reply
Dennis Briggs

Very nice on the html form after register I added forgot password line
Forgot Password

Reply
Jomar

Hi, I’m having a problem in 3. 9. Username and Email Checking in Database. Nothing pops up “Username or Email already exist” when I create a new duplicate account and I’m confused about your table user. I thought your only table is usermanagement. Is your usermanagement table is similar to your user table???? I’m really confused. Thanks

Reply
    Vivek Vengala

    Hello Jomar,

    Please check you have inserted rows in database table or not?

    User table is for my development purpose.

    Reply
mony

Hi dear,
It works perfectly but when a user logout and then again he click on login button “which will take him to login page ” it took him directly to the user dashboard using old stored details.

How to prevent accessing page directly if I have the URL. so you always be redirected to login page if you are not already logged in may be setting sessions . can you help please ?

Reply
    Vivek Vengala

    Hi mony,

    Check the session in members area page, if session does not exist redirect user to login page using header PHP function.

    Reply
Andrew

I have this error

Parse error: syntax error, unexpected end of file in D:\Xampp\htdocs\usermanagement\login.php on line 66

at line 66 it’s the end of the html tag ()

Reply
    Vivek Vengala

    Hi Andrew,

    Please post your code, so that I can see the problem clearly.

    To fix unexpected end of file error, please check you are not missing any closing braces for if condition.

    Reply

Leave a Reply:

x

Join My Facebook Group to Receive Coding Tutorial for Free

email-subsribe