Register & Login System Using PHP and MySQLi

In this article, we are going to learn how to create the complete registration and login system using PHP and MySQLi database. If you are a beginner and what to learn how to make registration and login system then this article is for you. The register form something looks like this.

registration form

It looks amazing. All right let’s start the tutorial and see how to create registration and login system using PHP and MySQL database. To make everything separate and nit we are going to separate PHP files. We will use PHP as module way.

I assume that you have installed the XAMPP server and start Apache and MySQL server. Now open up the MySQL server.

So let’s create a new Database and name it register_db. You are free to specify any name to this database that does not matter. But if you change the name of the database make sure you change it in the connection file as well.

Now create a new table in the register_db database. The following is the SQL statement used to create a user table in the database.

Now ones you have the database and user table let’s create a connection file to connect MySQL to the application. Let’s create a new project register.

In this project create a new file mysqli_connect.php. In this file, we are going to connect the MySQL database to the application.

connect the application to mysqli

mysqli_connect.php

In this code, we had defined constant variables. If you have different values to the constant variables change it. I have a password to my database so I specify it in the constant DB_PASSWORD variable.

Then using $con variable we had created a new connection to MySQL object. To the constructor, we had specified constant variables.

I also set the character set of the database using a function mysqli_set_charset().

Before you create a registration form make sure you have assets folder in the project. In this folder, we are going to add some images which we are going to use in the tutorial.

you can download the image and the source code from this link.

Now let’s create a new file register.php create a registration form using post method.

Create a Registration form

register.php

Now let’s examine the code.

In this file, you can see we have used the post method to post the data.

when we submit the form we require a file register-process.php. In this file, we are going to validate user input and secure it from SQL injection. Then we have a simple post form. This form return the post data to itself.

You are free to design this form as per your needs. Make sure the name of the form is exactly the same in the $_POST array. Using the required attribute of HTML I am going to require the empty field.

At the top and bottom, I have added header.php and footer.php. I have separated these files.

So let’s look at these files. create header.php file.

header.php

This file only have a header section of the register file. I am going to separate this file from the register. This is just a simple file where we have a link tag to link bootstrap and custom style.

Now, what about the footer.php file. Create a new file footer.php.

footer.php

This is the footer section of the registration page. make sure you create header.php and footer.php file in the root directory of the project.

Now let’s see how to style this form and make it more beautiful. So let’s create a new file style.css and add style to the registration and login form. So just copy this code in style.css file.

In this file, I have added a font family from google font using the import statement and nothing special.

So now let’s look at the register-process.php file. In this file, we are going to validate user input received from the registration form. Sometimes user inputs not always as expected so we need to validate and sanitize it.

In the register-process.php file, we are going to validate and sanitize user input and store the sanitize value in the database.

Process user Input

register-process.php

So this file validates user input as well as store it in the database.

we will check if there is an error in the $error variable then we display it otherwise we will store the user value in the database. If the user fills valid information then we are going to store the value in the database.

we also gonna have session variables to store session variable value. In this variable, I am storing user id. Using UserID I am going to display user profile on the login page.

To save the form from SQL injection I am using SQL prepare statement. This statement uses to secure use from SQL injection.

You can notice the user profile is also change when we redirect to login.php file. If you set user profile it uploaded in the server and then use it in the file.

Once the user fills valid data and register we will redirect to the login.php file. In this file, we will add the login system of the tutorial.

Using validate_input_text() function i am validating the user input. This function created in the helper.php file. I am using this file to create a different function that can help us to save our time.

helper.php

In this file, you can see we have a few functions which we have created in the for help. So these functions help us to solve problems.

Now if you specify valid user information and register with registration form you will redirect to the login.php file.

Login system

Now let’s create a new file login.php file and put the following code.

This file is just like register.php file but the difference is we had changed the form elements and process file.

Using a login-process.php file, I am going to process the login form. we will allow users to login with the email and register password.

login-process.php

This file processes the user input and allows the user to log in if the email and password is correct.

We will redirect the user to index.php file after login if the user input email and password match in the register user table database. we haven’t done anything special in this file. we are just going to use a select statement to select the user fields and fetch the data according to the email and password specified in the login form.

Now let’s take a look at index.php file. This file is just used to display user information. You are free to change this file whatever you want.

index.php

This is just a simple index file used to display user information. Using a get_user_info() function we are going to get the user information. This function is created in the helper.php file which we already included in the index.php file.

Now let’s check out the registration and login system. It will work absolutely fine.

I hope you like this article. If you want to download the project it’s on the Patreon page.

So that is all, for now, see you in the next article.