Basic Login and Logout System Using PHP
Introduction
A login system is one of the most common features in web applications. It allows users to authenticate themselves before accessing protected resources. PHP provides built-in session management, making it easy to implement a basic login and logout mechanism.
This article demonstrates a simple PHP login system using MySQL and PHP sessions.
Note: The example below is intended for learning purposes. It uses the legacy
mysql_*extension and stores passwords in plain text, which are no longer recommended for production environments.
Prerequisites
Before you begin, ensure you have:
- PHP installed
- MySQL or MariaDB server
- Apache or another supported web server
- A database (e.g.,
demo) - A user table (e.g.,
reg) containing username and password fields
Reference Links:
How to Set Up MySQL and Create a User on Ubuntu 24.04
Install Apache2, php7, mysql on Redhat 7 server
Install Apache with php-fpm on Ubuntu 16
Implementation
Step 1: Create the Login Page
Create a file named login.php and add the following code:
<?php
$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:index.php");
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
</head>
<body>
<form action="" method="post">
<table>
<tr>
<td>Username</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td><input type="submit" name="send" value="Login"></td>
</tr>
</table>
</form>
</body>
</html>
When valid credentials are entered, the username is stored in a session and the user is redirected to index.php.
Step 2: Create the Logout Page
Create a file named logout.php.
<?php
session_start();
session_destroy();
header("Location:login.php");
?>
This script destroys the current session and redirects the user back to the login page.
Step 3: Test the Login System
- Open
login.phpin your browser. - Enter a valid username and password stored in the database.
- After successful authentication, you will be redirected to the application.
- Access
logout.phpto end the session and return to the login page.
Conclusion
This example demonstrates a basic login and logout system using PHP sessions and a MySQL database. While it is suitable for understanding the authentication flow, it should not be used in production environments without improvements.
For production applications, consider the following best practices:
- Use MySQLi or PDO instead of the deprecated
mysql_*functions. - Store passwords securely using
password_hash()and verify them withpassword_verify(). - Use prepared statements to prevent SQL injection.
- Implement proper session management and input validation for enhanced security.
Related Article
To learn how to perform basic database operations in PHP, including Insert, Update, Delete, and Image Upload, refer to our previous guide:
Basic insert, delete, update database & Image upload using php?
