Simple User Registration & Login Script in PHP and MySQLi

simple-user-registration-login

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.

95 Comments

  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

      1. I am getting the same message when I try to login.
        ………….. Code is Skipped
        This is the message that i keep getting after logging in:
        Warning: Cannot modify header information – headers already sent by (output started at /home/soiree/public_html/Login/login.php:43) in /home/soiree/public_html/Login/login.php on line 60

  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.
    Thanks!

  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

    require(‘db.php’);
    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

  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…
    viju….

  21. I used code in your download but i kept seeing this?

    Warning: session_start() [function.session-start]: Cannot send session cookie – headers already sent by (output started at C:\xampp\htdocs\simple-user-registration-login\auth.php:8) in C:\xampp\htdocs\simple-user-registration-login\auth.php on line 9

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

    Warning: Cannot modify header information – headers already sent by (output started at C:\xampp\htdocs\simple-user-registration-login\auth.php:8) in C:\xampp\htdocs\simple-user-registration-login\auth.php on line 11

  22. Very good tutorial!
    But I have problem:
    “Warning: session_start(): Cannot send session cookie – headers already sent by (output started at /auth.php:8) in /auth.php on line 9”
    Each file is saved without BOM (UTF-8 BOM). Only UTF-8. What can I do?

  23. Goodevening Javed, thanks for the script. I am creating a volunteer website. Assuming I have 10 volunteers who have registered for the program (user-id 1-10), how can I echo the details of user-id 5 (name, phone number and department) to show on the dashboard of user-id 3 and vice-versa. Thanks.

  24. Hi Mr. Javad the code is not complete please I want you to send the code the complete codes of all the attributes in a PDF format into my email please thanks… daniel@**** and may I ask a question…
    Do you know how to design/program a website confirmation like mmm or any ponzi scheme that when any one registers it send activation to some other user?

  25. when I do the copy paste of what u have instructed it works but I can’t login using the same codes in my other db, but I can register, and when I try to login after registration it shows incorrect username/password even though my username and password are correct….by the way I’ve changed db name in db.php, table name in login.php and registration.php..where should I correct more…plis help!!! presentation nearby 🙂

    1. Sorry for the late reply, i was busy in some other tasks, well i think there may be some issue related to DB connection, you should try to connect db first, if you are changing it, and make sure if you register then its data is in database.

  26. how can I add details to my registration page I mean something like age sex state likes dislikes or for them to choose between security question and all that please how do I go about it

    thanks your tutorial is awesome keep it

  27. Hi Javed, nice prog. Works OK on my Linux setup. I would suggest in order to increase password security (at least for those who use passwords in Xampp or other database setup and including phpmyadmin, to put the db.php file outside the webroot. In other words put the file in the user root in a directory called ext_include and call that in the login and registration php files as follows ‘require’/ext_include/db.php’; This means that web users cannot readily discover the phpmyadmin (database0 password.
    I have altered it for my own set-up. I will when ready with other small modifications let you know to benefit all.#Well done on this, it saved me a few days headaches and paracetamols. Thanks

    1. Yes you are right, but what i tried here, i simple explain how things work, obviously there is much things for improvement but that should be deal in the later stage, my focus was only to tell the newbie how it actually works 🙂 thanks for your input.

  28. I like your system. Can I add to the registration form a field that accepts a registration number (sent to the user in advance) that validates the number from a database list and then, if valid, continues with the rest of the registration process?
    If so, can the login part of the script then give access to the private folder, or does yet another script have to exist on every html page within the private folder for each page to be accessible?

    1. Well tom, i actually didn’t get you clearly, but if you want to give registration unique no. or id then you can simply fetch the user id and give it to user, and giving access to private folder can be possible but it will be more advance feature, and currently i didn’t write any tutorial about it.

  29. WOW.
    I just came by this simple registration using PHP/MYSQLI
    It is lovely and a really helpful aid to newbies like me.

    I really wished you could do some advanced user registration tutorial that checks for duplicate users details during registration and validation of fields.

  30. Hi Javed

    This warning message appear when i am opening php file on local host.

    Warning: mysqli_connect(): (HY000/1049): Unknown database ‘register’ in /Applications/XAMPP/xamppfiles/htdocs/db.php on line 8
    Failed to connect to MySQL: Unknown database ‘register’

    What should i do next?

    Thanks
    Sandeep

    1. Dear Sandeep, i would suggest you to create database with name register in your phpmyadmin, this error means it is not able to connect with DB, or you check your username or password.

Leave a Reply

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