Introduction
User login and logout functionality is a basic requirement for many web applications. In this article, we will look at a simple implementation of login and logout functionality using PHP sessions.
The login process verifies the username and password against the database and stores the authenticated user’s name in a PHP session. The logout process destroys the session and redirects the user back to the login page.
Before starting, you can also refer to our previous article:
Basic Insert, Delete, Update, Database and Image Upload Using PHP
Prerequisites
Before implementing the login and logout functionality, make sure you have:
- A PHP development environment.
- A MySQL database.
- A database table containing user registration details.
- Basic knowledge of PHP and MySQL.
- Basic understanding of PHP sessions.
Implementation
1. Login Using PHP
Create a file named login.php.
The following code connects to the database, starts a PHP session, validates the submitted username and password, and stores the username in the session after successful authentication.
$query=mysql_connect("localhost","root","");
mysql_select_db("demo",$query);
session_start();
if(isset($_POST['send']))
{
$name=$_POST['name'];
$password=$_POST['password'];
if($name!="" && $password!="")
{
$s=mysql_query("select *from reg where name = '$name' AND password = '$password'");
$a=mysql_fetch_array($s);
$count=mysql_num_rows($s);
if($count==0)
{
echo "user does not exist";
}
else
{
$_SESSION['name']=$a['name'];
header("location:indedx.php");
}
}
}
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form action="" method="post">
<table width="400" border="0" cellspacing="0" cellpadding="0" >
<tr>
<td>username</td>
<td>:</td>
<td><input type="text" name="name" /></td>
</tr>
<tr>
<td>password</td>
<td>:</td>
<td><input type="password" name="password" /></td>
</tr>
<tr>
<td><input type="submit" name="send" /></td>
</tr>
</table>
</form>
</body>
</html>
2. Logout Using PHP
Create a file named logout.php.
The logout script starts the session, destroys the existing session, and redirects the user to the login page.
<?php
session_start();
session_destroy();
header("location:login.php");
?>
3. How the Login and Logout Flow Works
The basic flow is:
- The user enters their username and password.
- PHP checks the submitted credentials against the database.
- If the user exists, the username is stored in the PHP session.
- The user is redirected to the application page.
- When the user selects logout, the PHP session is destroyed.
- The user is redirected back to
login.php.
Conclusion
PHP sessions provide a simple way to maintain login information between requests. In this basic implementation, the login process verifies the user credentials and stores the username in a session, while the logout process destroys the session and redirects the user to the login page.
Frequently Asked Questions
1. Why are PHP sessions used for login?
PHP sessions allow user information to be maintained across multiple page requests after successful authentication.
2. How does PHP logout work?
The logout script calls session_destroy() to destroy the current session and then redirects the user to the login page.
3. Can this login implementation be used with modern PHP?
The concept of using sessions for login and logout is still applicable. However, the mysql_* functions used in this original example are no longer supported in modern PHP. Modern implementations should use PDO or MySQLi with prepared statements.
Related Articles
How to Install Apache, PHP, and MySQL on Red Hat 7?
How to install MySQL 8.0 on CentOS 7
Talk to Our Technology Experts
Planning your AWS infrastructure or looking to improve an existing cloud environment? Our team can help with cloud architecture, infrastructure, DevOps, security, deployment, and ongoing optimization.