What are common methods for implementing login/logout functionality in PHP without using databases?
One common method for implementing login/logout functionality in PHP without using databases is by using session variables to store user login information. When a user logs in, their username or ID can be stored in a session variable, and when they log out, the session can be destroyed. This allows for a simple and effective way to manage user authentication without the need for a database.
<?php
session_start();
// Check if user is logged in
if(isset($_SESSION['user_id'])) {
echo 'Welcome, ' . $_SESSION['user_id'] . '! <a href="logout.php">Logout</a>';
} else {
echo '<a href="login.php">Login</a>';
}
// Logout functionality
if(isset($_GET['logout'])) {
session_destroy();
header('Location: index.php');
exit();
}
?>
Keywords
Related Questions
- What best practices can be followed to ensure that data saved in a database from PHP is accurate and consistent?
- How can PHP be used to detect the HTTP_HOST and redirect users to specific subdirectories based on the domain entered?
- How can special characters in file names affect file downloads in Internet Explorer and how can this be mitigated in PHP?