How can PHP sessions be used to control access to specific pages based on login status?
To control access to specific pages based on login status using PHP sessions, you can set a session variable upon successful login and then check for the presence of that variable on restricted pages. If the variable is not set, redirect the user to the login page.
// Start the session
session_start();
// Check if the user is logged in
if(!isset($_SESSION['logged_in'])) {
header("Location: login.php");
exit();
}
// Restricted page content here
Related questions
-
foreach PHP error
-
cookies PHP cross-browser compatibility
-
GET variables fopen fclose