Artisans Web

Are you looking to create a login system on your website with PHP? Having a website login system allows access to important pages only to registered users. Users need to enter their credentials to access those important pages. To do this, you need to create a login page for your website. This page will have a form where users can add their credentials and log in to the system. The logged in user should also have the option to log out.

Basically, the login system should cover the following points.

  • Login Form session
  • Validate the user’s credentials
  • If the credentials are incorrect, display the error message.
  • If the credentials are correct, redirect the user to the account page.
  • Logging out the user

In this article, we will create a PHP login system that will cover all of the above steps. To handle all the mentioned points, we will use:

  • MySQL database
  • PHP sessions

The database contains the information of the user. information such as email, password, full name, etc. We will verify the details provided by the user against the records in the database. If a match is found, we will allow users to log in to the system.

To keep users logged into the application, we will set the user id in the PHP session. Using the value set in the session, the system can recognize the user and keep them logged in.

See Also:  How to create an email to all contacts in gmail

Database setup

As I said, we need a MySQL database to store the user details. This tutorial mainly focuses on creating a login system. So, I’ll create a database table and add some dummy entries to it. I’m not going to create a separate registration form or embed code for it. For the registration flow, the user can refer to the article: PHP Registration System.

Run the following SQL which creates a user table in the MySQL database.

If you notice, I have added 2 values ​​for the status column.

  • 1 : It means that the user has activated their account.
  • 0 : This account is not yet activated.

We will allow only users with an active account to log into the system. Users with inactive accounts will see the error message on a login page.

Next, add the dummy rows to the users table using the following SQL.

In SQL, I am using the MD5 hashed version of a password. The password is kept as ‘123456’ for testing purposes. After creating a login form, try the emails added above and this password.

Database class (class-db.php)

To set a database connection and verify the provided credentials against the database, let’s create a class-db.php and add the following code to it.

Be sure to replace the placeholders DB_HOST, DB_USERNAME, DB_PASSWORD, DB_PASSWORD, DB_NAME with their actual values. In the constructor, we write a code for the connection to the database.

The check_credentials() method will accept parameters: email and password. It will check if the credentials are correct or not and return the corresponding response.

See Also:  How to Integrate Alexa with Z-Wave Home Security System

Login Form (login.php)

The login form will have the inputs for the mail email and password. The user needs to fill in these details and press a submit button. The server-side script will take these inputs, process them, and handle the output.

In the above code, upon receiving a successful response, I set the user id and full name in the session variable ‘ user’. PHP sessions are available throughout the application. It helps us to recognize which user should stay logged in.

Upon successful login, the user will be redirected to myaccount.php. You can adjust this redirect page according to your flow.

Myaccount.php will simply have the welcome message and the logout link.

Logout user (logout.php)

The logout.php will remove the ‘user’ session variable. As a result, this session variable will be empty and the application will not keep the user logged in.

I hope you understand how to create a login system on your website with PHP. You can also extend this login functionality by integrating social login into your app. Take a look at the following articles that explain how to add social login with PHP.

  • Google website login with PHP
  • Login with Twitter on PHP website
  • Login with LinkedIn on PHP website

If you liked this article, please subscribe to our YouTube channel for video tutorials .


Leave a Reply

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