How can PHP sessions be used to control access to specific pages on a website?
To control access to specific pages on a website using PHP sessions, you can set a session variable when a user logs in or is authenticated. Then, on the restricted pages, check if the session variable is set to allow access. If the variable is not set, redirect the user to a login page or display an access denied message.
session_start();
// Check if the user is logged in
if(!isset($_SESSION['user_id'])) {
header("Location: login.php");
exit();
}
// Restricted page content here
echo "Welcome to the restricted page!";
Related questions
-
session.save_path PHP configuration errors
-
Image functions PHP thumbnails
-
PHP variables HTML blocks