banner image
Sedang Dalam Perbaikan

Simple Login and Registration using PHP and MYSQL


This article is based on how to create a Login and Registration system with the use of PHP and MYSQL. When you are visiting some sites, you are asked to register to the site and then you should login to see what you want from that site. Simply consider a social media channel. You have to give your details and register. Then only you are allowed to use the channel. For this scenario, I have created a simple system to which can be understood easily. But before you follow this, you have to configure PHP in your machine.

First we have to build up a database connection. To do that, you are required to create a database. In my case, it is called as testdb. Structure is given below.



Two files should be created called user.php and dbcon.php. Among these two files, user.php file is used to include the main parameters that are needed to build the connection and dbcon.php file contains how to set the  connection. I have previously published an article on this scenario. if you are unaware of it, go to this link to view that article. So, finally created files are like this. Get the codes for user.php file and dbcon.php file.

Now we have to code for the logic to build db connection. Here I use 3 files called index.php, register.php and homepage.php.

  1. register.php - Used to register a new user into the system.
  2. index.php - Used to login to the system
  3. homepage.php - After logged in, this page will be displayed.
Screenshots :







HTML Form is used to recruit new users and to give them the chance to login. if you do not know how to build it, refer here to get an idea.

index.php and register.php

As the code is bit large I have given you the link of my Github repository. Visit here to get the full set of source files. After you have downloaded it you can see the explanation.

Explanation : 

In both files(index and register), at the top a php code in included. That is to link the dbcon file. If it is not there, database connection is unknown. By this code, simplay we can connect a particular file with the database we created.

register.php file

In register.php file there's a HTML form to get the required information. Carefully look at the code to understand it. I have used a table tag within the form  just to align the content. Remember, table is not essential to create a form. This is not the only way to set up a form.

Then we reach to the logic part of the registration process. First take an if condition to check whether the user has clicked on the register button.  Remember to use the same names that you used for input tags in HTML form. Within the if condition, isset and POST are used to do this. To get ad idea between $GET and $POST in php, visit here.

Then we take the variable that are needed to do the process. Next the password is checked to make sure that the password and retyped password has the same value. If they are equal only, the process goes on. Otherwise an error message is shown as Password and confirm password does not match!. It is at the end of the file. If the values are equal, then a SQL query is run to get the username entered and check whether there is an existing user in the database with the same name. If so, an error is shown as User already exists..Try another one!. If there is no user with the same name, then the details are inserted into the database.

If the registration is successfully  done, a message will be displayed indicating the job has been has done. Otherwise, another error message is shown saying the process has not been done correctly.

index.php file

Here we used a code called session_start( ) specially to start the session of a particular user. Sessions are a simple way to store data for individual users against a unique session ID. Session IDs are usually sent to the browser via session cookies and the ID is used to retrieve existing session data.

Next we have to include a login form to get the login data from the user. This is same as the registration form; only contains some changes. As we previously used, if condition is used to check whether the user has clicked on login button and then the required variables are taken. After that a SQL query is run to retrieve the username and password. if there's an user with the entered name(indicated by query_run > 0; not zero; means a record is already in database), username of the session is taken and directed to the page called homepage.php. It is the page belongs to a user after he or she has logged in. But if a user has entered wrong username or password, an error message is shown as Invaild credentials.

homepage.php file

This is the file used to welcome a user. There's a simple PHP echo to display the owner of the ongoing session. After that a logout button is included to quit from the user account. When it is clicked, session is destroyed and redirected to the login page again.

So, now you have created and dusted with a simple Login and Registration coded in PHP and MYSQL. Run the project in localhost of your machine.







Simple Login and Registration using PHP and MYSQL Simple Login and Registration using PHP and MYSQL Reviewed by DAL on July 20, 2017 Rating: 5

No comments:

Powered by Blogger.