Setup a User Login System with PHP and MySQL

mysql login

Today, most websites provide login and registration functionalities. User authentication is a common component in modern web applications. When building or hosting a website with Yourwebsite, you will find it beneficial to create a PHP and MySQL login and registration system.

In this blog, we’ll walk you through the step by step process of creating a PHP and MySQL login system. First, we will cover creating a registration system and then we will show you how to create a login form. This system will be developed using PHP and MySQL.

How to Create a Registration System

In this section, we’ll guide you in building a registration system that will enable users to create a new account or register by simply filling out a web form. However, before this can be done we need to create a database table that will contain all the user data.

1. Creating a Database Table

You can use a MySQL client such as PHPMyAdmin. Click on the database tab which allows you to create a new database. Then, enter your database name for example; registration and click on the ‘create database’ button.
Similarly, you can copy the subsequent SQL query and execute it, enabling you to create the ‘users’ table inside your MySQL database.

password VARCHAR(255) NOT NULL,

2. Creating the Config. File

After you have completed the creation of your database table we can now move on to creating a PHP script to connect to the My SQL database server. You can create a file named ‘config.php’ and copy the following code to go inside it.

/* Database credentials. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root'); define('DB_PASSWORD', '');
define('DB_NAME', 'demo');
/* Attempt to connect to MySQL database */
$link = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());

After doing this replace ‘demo’ with your own database name. Also, replace ‘root’ with your database username and if there are any passwords you must specify them.

3. Creating a Registration Form

The first thing you need to do in creating a registration form is to make another PHP file and name it ‘registration.php’ or ‘register.php’. The output of the code you used should result in a signup page where username and password are displayed.

Building the Login System

In designing the PHP and MySQL login system, you are creating a form for the user to enter their username and password. If the user fills out all of the required fields and the credentials match, the user is granted access to the website; otherwise, the login attempt is rejected.

1. Creating the Login Form

Similarly, let’s now create the PHP and MySQL login file named ‘login.php’. We have provided a link to the code that you can copy and use to create your form.

2. Creating the Welcome Page

Below is the code for setting up your welcome page. This file will be named ‘welcome.php’ and will be the page the user is directed to after successfully logging in. The following code will be used to create the page.

Creating the Welcome Page

3. Create a Logout Script

To create a logout file, name it ‘logout.php’, just like you did when setting up a PHP and MySQL login system.

The script inside this file will delete the session and redirect the user to the login page when a user clicks the logout or sign out button. Copy the logout script below.

// Initialize the session
// Unset all of the session variables
$_SESSION = array();
// Destroy the session.
// Redirect to login page
header("location: login.php");

Wrap Up

You have finally learned how to set up a PHP and MySQL login system! Now that you have successfully created your website, go ahead and also create a domain name with Yourwebsite.

Yourwebsite is also devoted to helping you power your website with efficient and reliable web hosting. Clients get a secure hosting environment with a hassle-free setup.

Your website will be unstoppable if you combine the PHP and MySQL login configuration you just learned with Yourwebsite to develop your site and provide web hosting services. For more help to setup a user login system with PHP
and MySQL, ask Yourwebsite experts, we’re always willing to help.