Building a Simple Registration System With PHP

Saumya Shah
Aug 13, 2020 · 5 min read

Hello!

Most sites nowadays use a system of accounts linked to a database to keep track of user data, grant different levels of access, and to authenticate users when they are using your website.

In this tutorial, you will learn how to make a simple registration system for your website.

The article assumes you have a basic knowledge of the following languages:

  • HTML
  • JavaScript
  • PHP
  • MySQL

You will need the following:

  • A web host (I personally use 000WebHost)
  • A code editor (I use VSCode)
  • A MySQL database (hosted online)

Let’s get started!

Step 0 — Database Configuration:

Before starting, make sure that your database has the correct fields for your purposes. Make a table in your database named ‘Accounts’, and add the following columns with the appropriate datatypes through your online database manager (such as PHPMyAdmin):

  • Username — text
  • UserID — int
  • Password — text
  • Any other fields that you wish to collect (such as email address, age, etcetera)
a sample sign-up page
A sample login page

Step 1 — Making HTML Forms:

In your website, you will have two forms; one for signing up, and one for logging in.

Making a sign-up form:

  • Make a form element in HTML
  • Add inputs and labels for username, password, and other desired fields. (Don’t forget to set appropriate names for all the inputs)
  • Add a submit button with an appropriate name.
A sample sign-up form

Making a login form:

  • Make another form element in HTML
  • Add inputs and labels for username and password (again, don’t forget to set appropriate names for the inputs)
  • Add a submit button with an appropriate name.
A sample login form

You can include both the forms on the same page, or on different pages.

Step 2 — Connecting To Your Database

Now that your database and your input forms are set up, you will have to set up a connection to your database using PHP. This will require you to know your database’s DB Name, DB Password, DB User, and DB Host.

Setting up the connection is easy. The following code creates an object and connects you to your MySQL database, and then tests your connection to your database, returning an error if you are not connected.

You will have to substitute your own details in the given code.

Once you are done with this file, name it ‘config.php’, and include the following snippet of code before your HTML in all your pages, as shown below:

Step 3 — Sign-Up Procedure

Once you have set up a connection to your database, you are ready to start adding accounts. However, you will need to write some code to get the input from the page, and insert records of new accounts into the database.

Getting and sanitising the inputs:

You will first get the inputs using the names of the inputs, and sanitise them to protect against common hacking techniques (such as SQL injection).

Here, the following line,

$mysqli->real_escape_string(stripslashes(strip_tags($_POST[“Susername”])));

Gets the username that the user inputted using the $_POST[“ ”] method, and sanitises the input. You must repeat this line for all the inputs.

Checking if username exists:

Before allowing the user to register, we must first check if the username is already in use.

To do this, we will first write a MySQL query to get all the records where the existing username matches the inputted username. We will then execute this query, and check if there are any such records. If an account with the inputted username already exists, an error message will be generated and sent to the user.

Adding New Accounts to Database

If the user’s inputted username is unique, we will insert their details into the database, thus creating a new account.

However, before we insert a new record, we will need a new UserID. This code (given below) will get the last existing UserID in the table, and add 1 to it to generate a new UserID for our new account.

After we have the UserID, you have everything you need to insert the new record into the database. You must now make a MySQL query to insert the details into the database, and then execute it.

Step 4 — Login Procedure

Your login procedure will be much simpler than your sign-up procedure, since you only need to verify whether the username exists in the database, and if yes, verify whether the password linked to the username is correct.

This can easily be accomplished with this code:

In this code, the user input is sanitised, and a query is made to check if the inputted username exists in the database. If not, an error message is generated and sent to the user. If it does exist, then another SQL query is made to verify the password, and log in the user.

Step 5 — Adding PHP Sessions

You are very nearly finished with your registration system. However, to actually use the registration system, implementing sessions is very important. PHP sessions are a set of variables in which you can store your current user’s details.

To do this, you must add this to the start of all of your websites:

session_start();

This should come immediately after ‘<?php’, like so:

Now, you can begin storing your user’s data to the session! For this, you must store all the data in the session variables after they sign up and log in, like so:

Storing data after signing up:

Storing data after logging in:

Step 6 — Adding Logout Functionality

Good! You can now allow users to log in! But how do they log out? That is what we aim to add in this step. To do this, you must create a separate file, and link your logout button to open this file.

This code will start your session, destroy all the existing session variables, end the session, and redirect your user back to the login page.

Congratulations! You now know how to create a simple PHP registration system! You can apply CSS to make it look better, and implement it on your websites!

Full Signup/Login Code:

I hope you found this helpful!

Thanks,

Saumya Shah

The Startup

Medium's largest active publication, followed by +773K people. Follow to join our community.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store