Complete user registration system using PHP and MySQL database (2024)

In this tutorial, I walk you through the complete process of creating a user registration system where users can create an account by providing username, email and password, login and logout using PHP and MySQL. I will also show you how you can make some pages accessible only to logged-in users. Any other user not logged in will not be able to access the page.

The first thing we'll need to do is set up our database.

Download my source code on how to build a complete blog with PHP & MySQL database. $2 only! 👉Complete Blog - HTML, CSS, PHP & MySQL

Create a database calledregistration. In the registration database, add a table calledusers. The users table will take the following four fields.

  • id
  • username - varchar(100)
  • email - varchar(100)
  • password - varchar(100)

You can create this using a MySQL clientlike PHPMyAdmin.

Complete user registration system using PHP and MySQL database (1)

Or you can create it on the MySQL prompt using the following SQL script:

CREATE TABLE `users` ( `id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, `username` varchar(100) NOT NULL, `email` varchar(100) NOT NULL, `password` varchar(100) NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=latin1;

And that's it with the database.

Now create a folder calledregistrationin a directoryaccessible to our server. i.e create the folder inside htdocs (if you are using XAMPP server) or insidewww(if you are using wampp server).

Inside the folder registration,create the following files:

Complete user registration system using PHP and MySQL database (2)

Open these files up in a text editor of your choice. Mine is Sublime Text 3.

Registering a user

Open the register.php file and paste the following code in it:

regiser.php:

<?php include('server.php') ?><!DOCTYPE html><html><head> <title>Registration system PHP and MySQL</title> <link rel="stylesheet" type="text/css" href="style.css"></head><body> <div class="header"> <h2>Register</h2> </div> <form method="post" action="register.php"> <?php include('errors.php'); ?> <div class="input-group"> <label>Username</label> <input type="text" name="username" value="<?php echo $username; ?>"> </div> <div class="input-group"> <label>Email</label> <input type="email" name="email" value="<?php echo $email; ?>"> </div> <div class="input-group"> <label>Password</label> <input type="password" name="password_1"> </div> <div class="input-group"> <label>Confirm password</label> <input type="password" name="password_2"> </div> <div class="input-group"> <button type="submit" class="btn" name="reg_user">Register</button> </div> <p> Already a member? <a href="login.php">Sign in</a> </p> </form></body></html>

Nothing complicated so far right?

ez_ad

A few things to note here:

First is that our form'sactionattribute is set to register.php. This means that when the form submit button is clicked, all the datain the form will be submitted to the same page (register.php). The part of the code that receives this form data is written in the server.php file and that's why we are including it at the very top of the register.php file.

Notice also that we are including the errors.php file to display form errors. We will come to that soon.

As you can seein the head section, we are linking to a style.css file. Open up the style.css file and pastethe following CSS in it:

* { margin: 0px; padding: 0px;}body { font-size: 120%; background: #F8F8FF;}.header { width: 30%; margin: 50px auto 0px; color: white; background: #5F9EA0; text-align: center; border: 1px solid #B0C4DE; border-bottom: none; border-radius: 10px 10px 0px 0px; padding: 20px;}form, .content { width: 30%; margin: 0px auto; padding: 20px; border: 1px solid #B0C4DE; background: white; border-radius: 0px 0px 10px 10px;}.input-group { margin: 10px 0px 10px 0px;}.input-group label { display: block; text-align: left; margin: 3px;}.input-group input { height: 30px; width: 93%; padding: 5px 10px; font-size: 16px; border-radius: 5px; border: 1px solid gray;}.btn { padding: 10px; font-size: 15px; color: white; background: #5F9EA0; border: none; border-radius: 5px;}.error { width: 92%; margin: 0px auto; padding: 10px; border: 1px solid #a94442; color: #a94442; background: #f2dede; border-radius: 5px; text-align: left;}.success { color: #3c763d; background: #dff0d8; border: 1px solid #3c763d; margin-bottom: 20px;}

Now the form looks beautiful.

vli_ad

Let's now write the code that will receive information submitted from the form and store (register) the information in the database. As promised earlier, we do this in the server.php file.

Open server.php and paste this code in it:

server.php

<?phpsession_start();// initializing variables$username = "";$email = "";$errors = array(); // connect to the database$db = mysqli_connect('localhost', 'root', '', 'registration');// REGISTER USERif (isset($_POST['reg_user'])) { // receive all input values from the form $username = mysqli_real_escape_string($db, $_POST['username']); $email = mysqli_real_escape_string($db, $_POST['email']); $password_1 = mysqli_real_escape_string($db, $_POST['password_1']); $password_2 = mysqli_real_escape_string($db, $_POST['password_2']); // form validation: ensure that the form is correctly filled ... // by adding (array_push()) corresponding error unto $errors array if (empty($username)) { array_push($errors, "Username is required"); } if (empty($email)) { array_push($errors, "Email is required"); } if (empty($password_1)) { array_push($errors, "Password is required"); } if ($password_1 != $password_2) { array_push($errors, "The two passwords do not match"); } // first check the database to make sure // a user does not already exist with the same username and/or email $user_check_query = "SELECT * FROM users WHERE username='$username' OR email='$email' LIMIT 1"; $result = mysqli_query($db, $user_check_query); $user = mysqli_fetch_assoc($result); if ($user) { // if user exists if ($user['username'] === $username) { array_push($errors, "Username already exists"); } if ($user['email'] === $email) { array_push($errors, "email already exists"); } } // Finally, register user if there are no errors in the form if (count($errors) == 0) { $password = md5($password_1);//encrypt the password before saving in the database $query = "INSERT INTO users (username, email, password) VALUES('$username', '$email', '$password')"; mysqli_query($db, $query); $_SESSION['username'] = $username; $_SESSION['success'] = "You are now logged in"; header('location: index.php'); }}// ... 

Sessions are used to track logged in users and so we include a session_start() at the top of the file.

The comments in the code pretty much explain everything, but I'll highlight a few things here.

The if statement determines if the reg_user button on the registration form is clicked. Remember, in our form, the submit button has a name attribute set to reg_user and that is what we are referencing in the if statement.

All the data is received from the form and checked to make sure that the user correctly filled the form. Passwords are also compared to make sure they match.

If no errors were encountered, the user is registered in theuserstable in the database with a hashed password. The hashed password is for security reasons. It ensures that even if a hacker manages to gain access to your database, they would not be able to read your password.

But error messages are not displayingnow because our errors.php file is still empty. To display the errors, paste this code in the errors.php file.

<?php if (count($errors) > 0) : ?> <div class="error"> <?php foreach ($errors as $error) : ?> <p><?php echo $error ?></p> <?php endforeach ?> </div><?php endif ?>

When a user is registered in the database, they are immediately logged in and redirected to the index.php page.

And that's it for registration. Let's look at user login.

vli_ad

Login user

Logging a user in is an even easier thing to do. Just open the login page and put this code inside it:

<?php include('server.php') ?><!DOCTYPE html><html><head> <title>Registration system PHP and MySQL</title> <link rel="stylesheet" type="text/css" href="style.css"></head><body> <div class="header"> <h2>Login</h2> </div> <form method="post" action="login.php"> <?php include('errors.php'); ?> <div class="input-group"> <label>Username</label> <input type="text" name="username" > </div> <div class="input-group"> <label>Password</label> <input type="password" name="password"> </div> <div class="input-group"> <button type="submit" class="btn" name="login_user">Login</button> </div> <p> Not yet a member? <a href="register.php">Sign up</a> </p> </form></body></html>

Everything on this page is quite similar to the register.php page.

Now the code that logs the user in is to be written in the same server.php file. So open the server.php file and add this code at the end of the file:

// ... // LOGIN USERif (isset($_POST['login_user'])) { $username = mysqli_real_escape_string($db, $_POST['username']); $password = mysqli_real_escape_string($db, $_POST['password']); if (empty($username)) { array_push($errors, "Username is required"); } if (empty($password)) { array_push($errors, "Password is required"); } if (count($errors) == 0) { $password = md5($password); $query = "SELECT * FROM users WHERE username='$username' AND password='$password'"; $results = mysqli_query($db, $query); if (mysqli_num_rows($results) == 1) { $_SESSION['username'] = $username; $_SESSION['success'] = "You are now logged in"; header('location: index.php'); }else { array_push($errors, "Wrong username/password combination"); } }}?>

Again all this does is check if the user has filled the form correctly, verifies that their credentials match a record from the database and logs them in if it does. After logging in, the user is redirected them to the index.php file with a success message.

ez_ad

Now let's see what happens in the index.php file. Open it up and paste the following code in it:

<?php session_start(); if (!isset($_SESSION['username'])) { $_SESSION['msg'] = "You must log in first"; header('location: login.php'); } if (isset($_GET['logout'])) { session_destroy(); unset($_SESSION['username']); header("location: login.php"); }?><!DOCTYPE html><html><head> <title>Home</title> <link rel="stylesheet" type="text/css" href="style.css"></head><body><div class="header"> <h2>Home Page</h2></div><div class="content"> <!-- notification message --> <?php if (isset($_SESSION['success'])) : ?> <div class="error success" > <h3> <?php echo $_SESSION['success']; unset($_SESSION['success']); ?> </h3> </div> <?php endif ?> <!-- logged in user information --> <?php if (isset($_SESSION['username'])) : ?> <p>Welcome <strong><?php echo $_SESSION['username']; ?></strong></p> <p> <a href="index.php?logout='1'" style="color: red;">logout</a> </p> <?php endif ?></div> </body></html>

The first if statement checks if the user is already logged in. If they are not logged in, they will be redirected to the login page. Hence this page is accessible to only logged in users. If you'd like to make any page accessible only to logged in users, all you have to do is place this if statement at the top of the file.

The second if statement checks if the user has clicked the logout button. If yes, the system logs them out and redirects them back to the login page.

And that's it!

Now go on, customize it to suit your needs and build an awesome site. If you have any worries or anything you need to clarify, leave it in the comments below and help will come.

You can always support by sharing on social media or recommending my blog to your friends and colleagues.

Best regards :D

Awa Melvine

You might also like:

  • How to create a blog in PHP and MySQL database

ez_ad

Complete user registration system using PHP and MySQL database (2024)

FAQs

How to approve new user registration in PHP MySQL? ›

You can easily implement it by adding a column is_approved to your user table. When a user registers you set this field to false by default. Send the admin an email about the new registration and to approve the user just change is_approved to true .

How to connect database using PHP and MySQL? ›

Use the mysqli_connect() function to establish a connection to the MySQL database, passing the hostname, username, password, and database name as parameters. Verify if the connection was successful using mysqli_connect_errno() and mysqli_connect_error() functions. Close the connection using mysqli_close() when finished.

How to create a user in MySQL using PHP? ›

To create a new user account in MySQL, follow these steps:
  1. Access command line and enter MySQL server: mysql.
  2. The script will return this result, which verifies that you are accessing a MySQL server. mysql>
  3. Then, execute the following command: CREATE USER 'new_user'@'localhost' IDENTIFIED BY 'password';
Mar 1, 2024

What is the command used to create a database using PHP and MySQL? ›

Creating a Database

PHP uses mysql_query function to create a MySQL database. This function takes two parameters and returns TRUE on success or FALSE on failure.

How to create a database in MySQL for a login page? ›

Create MySQL Database and User
  1. Login to the server or machine which has permission to access the SQL nodes of NDB cluster.
  2. Connect to the SQL nodes of NDB cluster one by one.
  3. Execute the following command to login to the MySQL prompt using root permission or user, which has permission to create users with permissions:

How do I register a new user in MySQL? ›

Create a new user and grant permissions in MySQL
  1. Using MySQL commands. Following are helpful suggestions for MySQL commands. ...
  2. Log in. Log in to MySQL as the root user by using the following command: mysql -u root -p.
  3. Create a new user. ...
  4. Grant permission. ...
  5. Revoke permissions. ...
  6. Check permissions. ...
  7. Apply permissions. ...
  8. Log out.

How do I verify a user in MySQL? ›

Show Information About Current User

You can use a built-in function of MySQL to see the name and host of the user that you used to log into the MySQL command line. It's the “user()” function, and all you have to do is select it: SELECT user(); The output should give you information on the user running the query.

How to connect registration form with database in PHP? ›

For this you need to follow the following steps:
  1. Step 1: Filter your HTML form requirements for your contact us web page. ...
  2. Step 2: Create a database and a table in MySQL. ...
  3. Step 3: Create HTML form for connecting to database. ...
  4. Step 4: Create a PHP page to save data from HTML form to your MySQL database. ...
  5. Step 5: All done!

How do you execute a MySQL query in PHP using MySQLi? ›

The mysqli::execute_query() method is a shortcut for mysqli::prepare(), mysqli_stmt::bind_param(), mysqli_stmt::execute(), and mysqli_stmt::get_result(). The statement template can contain zero or more question mark ( ? ) parameter markers⁠—also called placeholders.

How can I create a database in MySQL? ›

MySQL Command Line Client

We can create a new database in MySQL by using the CREATE DATABASE statement with the below syntax: CREATE DATABASE [IF NOT EXISTS] database_name. [CHARACTER SET charset_name] [COLLATE collation_name];

How to create a contact form using PHP and MySQL? ›

Create a database table tbl_content with the name, email and more columns corresponding to our contact form fields. After getting the form data in the PHP, they will be used to form the MySQL INSERT statement. By executing the INSERT statement, the contact form data will be stored in the database.

How to make multi user role based login form in PHP and MySQL? ›

Follow the steps below.
  1. Create Database and Table by SQL query.
  2. Create Database Connection file.
  3. Create Sign Up HTML Form with role in PHP.
  4. Create administrator Login HTML Form with PHP.
  5. Create administrator Dashboard page after login success with Role.
  6. Create administrator Logout page in PHP.
Jan 24, 2021

How to create a login page using PHP and SQL? ›

There are few steps that are given below to setup the environment.
  1. Open the XAMPP Control Panel.
  2. Start the Apache server by clicking on the Start button.
  3. Start the MySQL by clicking on the Start button.
  4. Create all the files needed for login.
  5. Create login table in the database using phpMyAdmin in XAMPP.

How to create API using PHP and MySQL? ›

Steps to perform
  1. Create a Database & table. Create a database & table in MySQL to store the data. ...
  2. Create config.php script. This PHP script will store database connection-related information. ...
  3. Create a PHP script add-to-do. php to add To-Do's. ...
  4. Create a PHP script info.php to fetch To-Do information from the list of To-Do's.
Jan 9, 2020

Top Articles
Latest Posts
Article information

Author: Rev. Leonie Wyman

Last Updated:

Views: 6158

Rating: 4.9 / 5 (79 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Rev. Leonie Wyman

Birthday: 1993-07-01

Address: Suite 763 6272 Lang Bypass, New Xochitlport, VT 72704-3308

Phone: +22014484519944

Job: Banking Officer

Hobby: Sailing, Gaming, Basketball, Calligraphy, Mycology, Astronomy, Juggling

Introduction: My name is Rev. Leonie Wyman, I am a colorful, tasty, splendid, fair, witty, gorgeous, splendid person who loves writing and wants to share my knowledge and understanding with you.