How can PHP be used to create a user login system with specific access to different sections of a website?
To create a user login system with specific access to different sections of a website, you can use PHP to authenticate users and manage their permissions. This can be achieved by storing user information in a database, checking credentials during login, and then controlling access to different sections based on the user's role or permissions.
// Check if user is logged in
session_start();
if(!isset($_SESSION['user_id'])) {
header("Location: login.php");
exit();
}
// Check user's role or permissions
$user_id = $_SESSION['user_id'];
// Query database to get user's role or permissions
// Example: $user_role = get_user_role($user_id);
// Check user's role or permissions to access specific sections
if($user_role !== 'admin') {
header("Location: unauthorized.php");
exit();
}
// Display content for authorized users
echo "Welcome, Admin!";