Create Admin Login and Logout Page

In this post, I am going to let you know how to create a Normal/Admin register and login using PHP and MySQL. I also provide features to logout from admin dashboard. I will use PHP session to maintain user visibility.

The Login system of the user helps to restrict the access of sensitive information or pages from unauthorized user.

The admin pages in the application are not accessible to normal users. To log in, all users (administrators and normal users) utilise the same form. Normal users are redirected to the index page after checking in, whereas admin users are redirected to the admin pages.

I am using SB Admin 2 theme.It’s a open source bootstrap based theme.

Registration and Login form in PHP and MySQL

Let’s build a user management system with two types of users: admins and regular users. I’ll save everything to a MySQL database.

Create the following files and directories in the xampp/htdocs(www if you’re using wamp) folder for a user-management application:


Whereas files are:

  • index.php: This is the normal user’s home page.
  • account.php: This file will contain user registration and login methods.
  • admin/dashboard.php: This file will be used to display the admin user’s welcome dashboard page..
  • header.php: This file contains information about the header theme.
  • sidebar.php: This is a partial file that will display an html view of the ace admin sidebar.
  • login.php: It will have a user sign-in view.
  • register.php: This file provides user registration information; I use the same registration form for admins and regular users.

Create MySQL Connection Using PHP

We’ll start by creating a database called test. Also, using the SQL script below, create a login name table in this database:

Create Connection Using PHP and MySQL

Let’s use PHP to make a connection with MySQL data. The following code was added to the top of the account.php file.

Create Partial header.php

We’ll make a header.php file and paste the code below into it. This file will contain all of the CSS and JS include paths. I also used the session start method at the top of the file to start a PHP session.

Add a link to the css/js files directly under the file’s head section.

Create Partial sidebar.php

We’ll make a sidebar.php file and paste the code below into it. This file has a sidebar menu option, which you can customize for both admins and regular users.

Created Admin and User Registration

Using SB admin2 Theme Theme, we’ll first setup user and admin registration. Now, open register.php in your preferred text editor and begin writing some code.

User and Admin Signup HTML

Create an HTML form that receives user input and saves it to the mysql login table. I’ve added the following code to this file:

At the top of the file, I’ve imported header.php and account.php. Create a POST type form request to submit user inputs to the server-side register() procedure after the user presses the submit button.

Added Method to save User Form Data

Let’s add a method to the account.php file that validates and saves data.

I’ve created three methods in the code above:

parse input– The mysqli real_escape_string() method is used to parse user inputs.
signup– This method is used to process and save all form-posted payloads into the MySQL login table.
display error– This technique separates all errors and bids them with a div element, which is then printed on the user registration page.


Checkout other recommended tutorial of User Authentication in PHP,

Created Login/Sign-In Using MySQL

I’ve already established a user registration feature; now I’ll add a user logged-in feature. I’ll take the user’s email address and password and transmit it to the login method. We’ll redirect you to the user page (normal user) or the admin dashboard page once the user credential has been validated (for admin users).

User and Admin Login HTML

Create an HTML form to collect user input and save it to the MySQL login table. I’ve added the following code to this file:

I’ve added an account and header file at the top of the file, then generated a form with username and pass input fields. The login() method receives the form credentials.

Added Method to Login User

In the account.php file, we will add a login mechanism. This method is in charge of validating and establishing a session with the user.

We’ve verified the user’s credentials, and if everything checks up, we’ll redirect based on the user’s type.

The getUserById() method is used to retrieve information about a user based on their id. It returns user details, which are stored in the session variable.

The isLoggedIn() method is used to determine whether a user is logged in or not. If the session contains a user info variable with a value set, the user is logged in; return True otherwise, False.


Logout Using PHP

We’ll implement a logout feature. The following code has been added to the account.php file.

How To Check Logged-in User is Admin

As you are aware, I have saved user information in the PHP session user info variable, which we will use to verify whether the user is an administrator or not. Add the following code to the account.php file.

dashboard- normal-user


I hope you found this tutorial useful. We learned how to utilise PHP and MySQL to develop a user management system. For a user/admin, you can add further functionality such as listing and editing capabilities. You can adapt it to your requirements and use it in your projects. If you have any questions or concerns about this tutorial, please post them in the comments section below.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.