login php mysql with session

It is important to know how to manage PHP sessions to apply them to different web projects, so we will learn how to Login with PHP and MySQL with sessions. Not without first inviting you to review the post on how to add users to the MySQL database.
Making user login php
Create the following files:
login.html
checklogin.php
panel-control.php
logout.php
login.html file
In this file, we create a form in the modal window style and add two inputs: username and password, where we will effectively ask the user for their credentials and send them to checklogin.php to validate the fields and then move on to the desired user profile .
checklogin.php
We start the script by adding a login and the connection data, then the data from the login.html is taken, verifying that they exist in the database.
$ username = $ _POST ['username'];
$ password = $ _POST ['password'];
$ sql = "SELECT * FROM $ tbl_name WHERE username = '$ username'";
If all goes well, the session data is stored and redirected to the control panel. Otherwise the user must return to login.
Something new that we will find in the file is the time to deactivate the session for idle time:
$ _SESSION ['expire'] = $ _SESSION ['start'] + (5 * 60);
We set the time to 5 minutes, then the user must log in again for security reasons.
panel-control.php
A small control panel is accessed for the user to view their data, reports, etc. Here you add what you want the user to see in their profile.
Something very important is that the session time started is monitored at every moment. After 5 minutes it will be closed due to inactivity.
A php logout button is also added, which calls the logout.php file that runs the following code:
<?php
session_start();
unset ($SESSION['username']);
session_destroy();
header('Location: http://localhost/Login/login.html');
?>
With this file you will be able to close the user and address the login page again or another section if you prefer:
header ('Location: http: //localhost/Login/login.html');
Do you have questions or comments about it? Write us for further assistance.