- Ways to create an authentication system
- About this example
- File structure
- User login interface
- PHP code to process login
- Get logged-in user profile data to display a welcome message
- Handling logout in PHP
- Database script
- Test login details
- PHP login script with session output
In this tutorial, let us create a login script with a session in PHP. It has a simple example of implementing user authentication. This example uses a standard login form to get the user login details. And it preserves the login state with PHP sessions.
Login would be the first step of many application. Sometimes, part of privileged functionalities of application will ask users to login.
So, the login script is an integral part of an application. I will present you the implementation of the login system with minimal code.
Authentication will help us to identify the genuine users. By enabling authentication, we can protect our website from anonymous access.
Ways to create an authentication system
There are different ways of implementing an authentication system. The most popular way is to get the username and password via a login form and authenticate based on them.
Recently, authentication using OTP is also becoming the norm. The OTP will be dynamic and allowed for one-time.
For OTP authentication, the application sends it via wither via SMS or email. In a previous article, we have seen an example code in PHP to login by sending OTP via email.
About this example
In this example, it has users database with name, email, password and more details. It has a HTML form with inputs to get the user login credentials.
When the user submits their login details, then the PHP code will receive the posted data. It compares the entered data against the user database.
If match found, then it sets the user login session. In this authentication code, it preserves the user id in a PHP session. The existence of this session will state user authentication status.
After authentication, the PHP $_SESSION super global variable will contain the user id. That is, the $_SESSION[“member_id”] is set to manage the logged-in session. It will remain until log out or quit from the browser.
While logout, we unset all the session variables using PHP unset() function.
Below screenshot shows the organized file structure of this user login example. The Member.php is the model class with authentication functionalities.
The DataSource.php file contains functions to get a connection and access database.
In a view directory, I have created all the UI related files for the login and the dashboard interface. It also contains a stylesheet used for this UI.
The index.php is the landing page that checks the user logged-in session. Then it redirects users either to log in or to the dashboard.
The login-action.php and logout.php files are the PHP endpoints. They handle actions as requested by the users via the interactive authentication Interface.
User login interface
Creating a HTML form to login is the first step. It is to get the login details from users.
In this example, it has two fields username and password for user login.
I have specified the validation function and the PHP endpoint with the form tag.
The HTML contains elements to display client-side validation error. Also, it has the code to show a server-side error response based on the login result.
and the styles are,
Login form validation
This script is for validating the login data at the client-side. If the users submit the login with empty fields then this script will return boolean false.
When it returns false, it displays a validation error message to the users. By returning boolean 0, the form validation script prevents login to proceed further.
PHP code to process login
The login-action.php file receives and handles the posted login data. It sends the username and password to the processLogin() function.
This method gets the login details and compares with the user database.
It prepares query and binds the login params with it to find the match from the database. If match found the processLogin will return the result.
On successful login, the login-action.php sets the logged-in user session. Otherwise, it will return error by saying “Invalid Credentials”.
Get logged-in user profile data to display a welcome message
This code is to display the dashboard after login. The PHP code embedded with this HTML is for getting the user session and the user data from the database.
It displays the welcome message by addressing the user with their display name.
The dashboard contains a logout link in addition to the welcome text.
This is the PHP class created in this example to handle the login process. The getMemberById method request DataSource to fetch the member results.
Redirect users to Login or Dashboard based on Session
A landing page index.php contains code to check logged-in session and route users accordingly. The following code shows how to redirect users based on the session.
Handling logout in PHP
By clicking the logout link from the dashboard, it calls this PHP script. In this script, it clears the current login session and redirects users back to the login. The logout code is,
This class establishes a connection object to access the database based on the request. It has the select function to prepare a fetch query to return the results.
This script contains the CREATE statement for the registered_users table. Also, it has the data dump to check the example with test login details.
Test login details
After setting this example code and database in your computer, use the following test data to check the example login system.
PHP login script with session output
This output screenshot shows the login form interface. It has the input fields to get the user login details.
This is the screenshot of the welcome message. Once logged-in, then the user will see this response in the browser.
This view will show a welcome message by addressing the logged-in user. It also has a link to logout as shown below.