Registration and Login Script in PHP and MySQLi with Validation

Registration and Login Script in PHP
In this tutorial we are going to explain How to create advanced Registration and Login Script in PHP with MySQLi. This is basic requirement of every web application, allow user to register on website and access secure area for logged in users. We have created this script in procedural way, so its easy to implement with your web application.

In this script we also used form input validation like password length, email validation, input field validation etc.. In this script we are using PHP 7 version.

Steps to Create Registration and Login Script in PHP and MySQLi

1) Create Database table with Five column : id, username, email, password, register_date
2) Create Database Connection using PHP MySQLi
3) Make Registration form
4) Create Form Input Field jQuery Validation
5) Write MySQLi Prepared Statements to insert user data
6) Make Login Form
7) Authenticate User
8) Create Index Page
9) Create Logout Page

Lets start to learn by following above mentioned steps:

1) Create Database table

First create an database table using below MySQL query. This will create 5 column database table, make ‘id’ column as primary key.

2) Create Database Connection using PHP MySQLi

Now write down an PHP script to make database connection using MySQLi. Create a database.php file and mention your database configuration access. Use below code to create database connection.

3) Make Registration form

Create an register.php page and make an HTML form to take user input like Username, Email, Password. We are using Bootstrap HTML form, just include the bootstrap CSS and JS link in it. You don’t need to built own CSS. Check below code:

registeration form

4) Create Form Input Field jQuery Validation

We have putted many validation in this registration form. Like check input fields are empty or not, check for valid email and Password strength.

To check valid email we are using PHP sanitize and validate method. After submit the form, Remove all illegal characters from it and checking email is valid or not like:

For other validation we are using jQuery, check it below script code which perform all validation. Place this script code in register.php page in Head section.

In this script validate fields on form submit. To stop form submit after alert message return false value on form submit via its attribute. Like:

$(‘#formID’).attr(‘onSubmit’,’return false’);

5) Write MySQLi Prepared Statements to insert user data

Now write down MySQLi queries to insert register form data in database table. We are using MySQLi Prepared Statements to insert data. Prepared statements are very useful against SQL injections. Using this way you can apply same SQL injection repeatedly with high frequency.

In this process, first we write down the insert query like this:

$stmt = $conn->prepare(“INSERT INTO register_users (username, email, password, register_date) VALUES (?, ?, ?, ?)”);

Than bind the input parameter with this SQL injection command.

$stmt->bind_param(“ssss”, $username, $email, $password, $register_date);

In this ‘ssss’ describe the input data is string. First ‘s’ describe that username is string value. For integer value we use ‘i’ value.

Now to execute this MySQLi query use below command:


To insert registration form data in database, use below PHP code:

To view error message, we are using $msg, $checkuser and $validate_msg variables. To display these message just put in HTML form via PHP tag. Like :

6) Make Login Form

Now create an login.php page for Login form. Use below code to execute login process:

login form

7) Authenticate User

Create a session.php page to check the user session. Include this PHP file on require page to check user has access to view it or not.

In this we are checking “$_SESSION[‘name’]” is empty or not. If session is empty than redirect the user to Login page.

8) Create Index Page

Now create an index.php as secure page. After successful login user can view this page. Check below code to create this:

9) Create Logout Page

Create a logout.php file to destroy the user session. It will remove the current user session and redirect back to login page. We have placed this logout page link in Index page.

All done. Using above mentioned steps you can easily create registration and login Script in PHP and MySQLi with Validation. We hope this tutorial help you to create this functionality. If you found useful please share it and leave your feedback.

Check Live demo and Free Download source code also available.


Leave a Comment