Simple User Registration & Login Script in PHP and MySQLi


Demo Download

Today i will share Simple User Registration & Login Script in PHP and MySQLi, i will explain the basic functionality of registration and access secured area. In this tutorial user can register, login to member secured area and logout.

Steps to Create Simple User Registration & Login Script in PHP and MySQLi

  1. Create a Database
  2. Create a Database Table with Following Five Fields:
    id, username, email, password, trn_date
  3. Create a Registration Form
  4. Create a Login Form
  5. Connect to the Database by using PHP
  6. Authenticate Logged in User
  7. Create Index Page
  8. Create Dashboard Page
  9. Create a Log Out
  10. Create a CSS File

1. Create a Database

To create a database, login to phpmyadmin and click on database tab, enter your database name and click on create database button or simply execute the below query. In my case i created database with the name register.

2. Creating a Database Table

To create table, execute the below query in SQL.

3. Creating a Registration Form

Simple create a page with name registration.php and paste the following code in it.


4. Creating a Login Form

Create a page with name login.php and paste the following code in it.


5. Connect to the Database

Create a page with name db.php and paste the below code in it.

6. Authenticate Logged in User

Create a page with name auth.php and paste the below code in it.

7. Creating Index Page

Create a page with name index.php and paste the below code in it.


8. Creating Dashboard Page

Create a page with name dashboard.php and paste the below code in it.


9. Creating a Log Out

Create a page with name logout.php and paste the following code in it.

10. Creating a CSS File

Create a page with name style.css and paste the below code in it.

Note: In this tutorial CSS file is placed in css/style.css folder. So make sure that you also placed CSS file in the same css folder.

Click here to Insert, View, Edit and Delete Record from Database Using PHP and MySQLi

If you found this tutorial helpful so share it with your friends, developer groups and leave your comment.

Article By
Javed Ur Rehman is a passionate blogger and web developer, he loves to share web development tutorials and blogging tips. He usually writes about HTML, CSS, JavaScript, Jquery, Ajax, PHP and MySQL.


  1. In your registration form, what is the mechanism of user already exists in the database. The same user can be inserted any time. plz check it

    1. Thanks for stopping by, Naveed yes you are right but i didn’t use here to check is user already exist or not, i tried to explain simple registration for newbie users. Yes we can implement so many things in registration form. 🙂

  2. Hello!

    I followed the directions here exactly!
    But I can’t seem to get this to connect to my database, I keep getting the code

    “Database Selection FailedAccess denied for user ‘mikereve’@’localhost’ (using password: NO)”

    At the top line of my login page.

    Please advise! 🙂

    1. You need to make sure that you are providing the correct detail for connecting database, commonly root is the user and make sure you are providing the correct password, if there is no password in your phpmyadmin so you do not need to provide password on local environment.

  3. I have got these 2 warnings, please help.

    Warning: mysqli_select_db() expects exactly 2 parameters, 1 given in C:\wamp64\www\fresh\db.php on line 6
    Warning: mysqli_error() expects exactly 1 parameter, 0 given in C:\wamp64\www\fresh\db.php on line 8

    Please help!
    Thank you.

  4. Hi there this really help me a lot. How can i implement a function that check user if exist in registration form??

  5. Mr. Javed good day I am Isah from Nigeria I have experienced difficulties in running your code on notepad++ please send the way out through ******

  6. I have tried to make it work but any page that calls the auth.php file seems to just output the code onto the page and not show what it should.

  7. Great tutorial! However, it worked fine for me at first but now when I try to register a blank page just appears. Any ideas?

    1. It seems like after clicking on register button you are getting error, although you can see in my code that if form is submitted it must show either error or successful message, if you can explain it in more detail so may i can i figure out what is actually issue you are facing, are you doing it local server or on host? try it host if you are facing issue in local server, may be problem in your local server or not configured properly.

      1. Cheers for the reply. When i attempt to register a user the error or successful message doesn’t load, the page just appears blank. It’s on host just now and I still can’t figure out the error

        1. Try to register with plain user and password, like
          username: user
          password: pass

          If your issue is resolved then just simply check mysqli_real_escape_string() function because it escape special characters in variables.

  8. Dear Mr. Javed,

    Thank you for your good work. I need a full payment system script for my small business where my customers can login and make and receive payments. Is this possible

    1. Dear Mark, i am glad that you like it, but for payment system there are various options it depends that which option you want to use, like PayPal, or any other payment method. Script for payment will be vary according to your and payment service provider needs.

  9. This is exactly what I needed for my existing website. I’m smart enough to figure how to add more required fields and insert the info to my db. However, is there an easy way for me to display a username on a page? As in “Hi [username], welcome to my site”.

  10. Hi Javed, first i would like to tell thanks to you!! The code is going fine upto login page when i enter correct username and password it shows incorrect username or password.

    1. Hi Shamanth thanks that you find it helpful, if you are talking about demo link then i have place there demo user and password, you can login only with that one, or if you have set it up on your local server then you should check after registration that is your registration is going into database? If not then you should enable your local phpmyadmin. Or you can try it online if you have any hosting available.

  11. Hello javed
    Thank you for sharing this post.
    It worked fine till registration and login but once i registered my self and use the login and password which i have created using registration page it doesn’t redirect to index page but gives this error

    Cannot modify header information – headers already sent by (****/****/public_html/myaccount/register/login.php:11) in /****/****/public_html/myaccount/register/login.php on line 28

  12. This helped me out! Thanks!
    One small thing I’ve added is an link back from register to login, for users that didn’t want to register but expediently clicked on it.

  13. Hi Javed,

    This script is simple and straight to the point. Thank you.

    Everything works wonderfully on my site, however, I am having one issue where after login the login.php does not redirect to index.php. So after a successful login, the page refreshes login.php and it is blank. If I manually go to the index.php the session is open and the users info will display correctly. Can you help with the redirect on login to index?

    1. Try to register with plain user and password, like
      username: user
      password: pass

      If your issue is resolved then just simply check mysqli_real_escape_string() function because it escape special characters in variables.

  14. Hi Javed,

    Thank you for this simple script and i was able to learn much, but i would like to ask about this part

    if (isset($_REQUEST[‘username’])){
    $username = stripslashes($_REQUEST[‘username’]);
    $username = mysqli_real_escape_string($con,$username);

    the username on the if line, whenever i change it the script would stop working/not redirect to any page and i would like to know how does it work or if it does grab something from the db.php, thank you

    1. As you can see that form is submitting on the same page, i am checking here that if this field is filled, which means that form is submitted then perform the actions mention in the parenthesis {}.

  15. I have used the code in the download file that you provided. I keep getting this when I login:

    Warning: session_start(): Cannot send session cache limiter – headers already sent (output started at /public_html/login.php:9) in /home/s1541/public_html/login.php on line 11

    Warning: Cannot modify header information – headers already sent by (output started at /home/s1541/public_html/login.php:9) in /home/s1541/public_html/login.php on line 28

    Here is the code for the index.php page:


    // If form submitted, insert values into the database.
    if (isset($_POST['username'])){
    // removes backslashes
    $username = stripslashes($_REQUEST['username']);
    //escapes special characters in a string
    $username = mysqli_real_escape_string($con,$username);
    $password = stripslashes($_REQUEST['password']);
    $password = mysqli_real_escape_string($con,$password);
    //Checking is user existing in the database or not
    $query = "SELECT * FROM users WHERE username='$username'
    and password='".md5($password)."'";
    $result = mysqli_query($con,$query) or die(mysql_error());
    $rows = mysqli_num_rows($result);
    $_SESSION['username'] = $username;
    // Redirect user to index.php
    header("Location: index.php");
    echo "
    Username/password is incorrect.
    Click here to Login“;

    Log In

    Not registered yet? Register Here

  16. hi, i cant figure why after i try success register, when i try to login it either give me error message username/password incorrect and blank page, wasnt it suppose to go to index.php? help me.

  17. Hi Javed,
    When I try to register it gives me this error msg: “Failed to connect to MySQL: Access denied for user ‘root’@’localhost’ (using password: NO)”
    ,so what can I do to fix that??
    Thanks in advance

  18. Javed; Great Work!

    Any way to add:

    1) That the member registered account expired one year after created?

    2) Registration Code field – a registration code that we will print and provide to the users and when they register need to put on the registration form to validate their account.

    Thanks in advance!

    1. Thanks Samuel,
      Well yes you can create expire date field and enter value in it using date() function, you can enter any date you want, just Google it and you will have lots of date formats along with future dates.
      and for registration code i actually didn’t get you clearly but if you are saying that user will provide registration code which is provide to them manually, then you can simply compare any field using any value either in database or in PHP page if registration code is similar. Otherwise you will need to create another table in DB add all the Reg. Code there and find the user provided code in the table first, if it matches then proceed to register.

  19. I like your code and it seems it will work well. I am having an issue on all pages like registration and login, where when i fill in the information and click on the button, it just reloads the page. No error, no successful message and the database is not updated. I can use INSERT INTO and it will insert into the database so I know it is connected, but it will not do it when i use the register button

  20. hiiii javed sir i’m very worried about my final year project if you have Online Shopping portel code please send me on my gmail account…

Leave a Reply

Your email address will not be published. Required fields are marked *