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.

registration-php

4. Creating a Login Form

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

login-php

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.

secured-area

8. Creating Dashboard Page

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

dashboard

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.

128 Comments

  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” );
      exit();
      }

  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
    password=’$password'”;
    $result = mysqli_query($con,$query) or die(mysql_error());
    $rows = mysqli_num_rows($result);
    if($rows==1){
    $_SESSION[‘username’] = $username;
    header(“Location: index.php”); // Redirect user to index.php
    }else{
    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.

    Thanks
    Hussain

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

Leave a Reply

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