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. I tried to run this on my apache server and when I navigated to the registration page all I got was a blank page. Do you know where I need to save the css file or can I just change the code to include the filepath? Also do I keep or remove all the quotations in the code?

  2. Hi am just using your registration codes, it works well but the datas username,email, password is not showing under my phpmyadmin databse.

    1. “Warning: session_start() [function.session-start]: Cannot send session cookie – headers already sent by (output started at C:\xampp\htdocs\xampp\seva1\login.php:9) in C:\xampp\htdocs\xampp\seva1\login.php on line 10

      Warning: session_start() [function.session-start]: Cannot send session cache limiter – headers already sent (output started at C:\xampp\htdocs\xampp\seva1\login.php:9) in C:\xampp\htdocs\xampp\seva1\login.php on line 10”

  3. thank you i have neen using this for some time both versions . recently the <5.5 version has curiously stopped working. i cannot discover if it is a php upgrade or not as my hosting has changed. version now is 5.4.45. shame i love the simplicity of this script

  4. Hi Javed. First of all thanks for the whole tutorial…but after i successful login, it just refresh the login.php but with a totally blank…is it any php tutorial for the connection after login? and after the registration…the data that i key in was not exist in the database…

  5. Thank you for your good work.
    But after registration and login, I only get a blank page.
    If I open index page when I log in, I’ll think so
    BUT, you want to get to index.php directly when you log in.
    This does not help
    Session_start ();
    Require (‘db.php’);

  6. Hi Javed greetings thanks alot. thanks alot for your tutorial. it really helps… hope i can change the form action to the register.php in my web host?

  7. Thanks for perfectly simple example, it’s exactly what I needed. Almost 🙂
    Though, I want to use it only as an addition to another website, yet it insists to make user log in before they can enter main index file.
    Is there a simple way to change this?

    1. I think I figured out – if not logged in, it should redirect to homepage, and not login page.

      if ( !isset( $_SESSION[ “username” ] ) ) {
      header( “Location: index.php” );

  8. Notice: Undefined index: email in C:\xampp\htdocs\DASH\registration.php on line 13
    You are registered successfully.

    Click here to Login

  9. hi Javed. this is great tutorial indeed and is simple to understand but one more problem: when do u promise to show us the reset password tutorial? apart from this, I liked your tutorial

  10. Sir, thanks for the help, but my registration page isn’t connecting to the my database via php myadmin… Pls need urgent reply

  11. Hi Javed

    This is brilliant. For a beginner to understand this it is purely simple and easy to understand code.
    I have used everything as shown, just changed to suit my database and parameters.
    The registration works perfect.

    I had some problems with the login page however.
    First, due to the md5 for password somehow it was reading the key stored in the database. I changed that and that is solved.
    The second issue which i am unable to solve which is actually a repeat by some other comments above, is the redirect after login to the index page where it would show the username based on the session.

    i havent touched your code.

    $query = “SELECT * FROM userdata WHERE username=’$username’ and
    $result = mysqli_query($con,$query) or die(mysql_error());
    $rows = mysqli_num_rows($result);
    $_SESSION[‘username’] = $username;
    header(“Location: index.php”); // Redirect user to index.php
    echo “Username/password is incorrect.
    Click here to Login“;

    The header attribute doesnt redirect to index page to show the Welcome “username”.
    It just reloads the login.php page and remains blank.

    However if i manually navigate to index.php it shows be the details.

    Your views please.


    1. You need to push the login script to the very very top of the page, it can’t be below anything else. It’s a header…

  12. I’ve solved the issue everyone is having (after login). Here is how you fix it in 2 simple steps.

    Take the Login.php code and move the HTML part in the top down to below the php code. (Note: PHP code must be in the top in order for the redirect to work). Also, add your URL into the redirect URL line: (see below).

    // 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: http://YOURWEBSITEURL/index.php&quot;);
    echo "
    Username/password is incorrect.
    Click here to Login“;


    Log In

    Not registered yet? Register Here

    1. i downloaded the demo files but when i import it to my website it loads but when i try to sign in it says cant find database how do i create database with thedemo files i downloaded

  13. hi sir, thank you very much for your help/…every thing is work,but i can’t add another user,in registration…

  14. it is not working in my additional pages.
    it is working in only my home page.
    for example when i opened localhost/saghar then it worked correct but when i opend localhost/saghar/contact.php it does not worked. i gave it 4/5 stars because i knew solution for it.

  15. Hello, i feel that i noticed you visited my weblog thus i came to go back the favor?.I am trying to find issues to improve my web site!I assume its adequate to use some of your ideas!!

  16. login.php not redirecting to index.php on server showing blank page, but it is working on localhost. what to do to make it work on server?

  17. Solved the issue with replacing session_start() to ob_start() and put ob_flush() before last } … like



    hope this will helpful to everybody facing the header issue. 🙂

  18. hi, thanks for this tuto

    i’m in local
    i don’t understand something about password

    when u register, u write/choose ur own password
    but in the BDD, there is very complicate password… not the same i choose when i register

    so :
    this complicated password is sent to our email when i registered ??
    or, if is not this
    how can i add/replace MY own password in the BDD ??

Leave a Reply

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