How can the PHP code be modified to prevent unauthorized access to different shop user accounts?
To prevent unauthorized access to different shop user accounts, you can implement a role-based access control system in your PHP code. This involves assigning roles to users (e.g., admin, customer) and checking the user's role before allowing access to specific functionalities or data.
// Example of role-based access control in PHP
session_start();
// Check if user is authenticated
if (!isset($_SESSION['user_id'])) {
// Redirect to login page
header("Location: login.php");
exit;
}
// Check user role before accessing sensitive information
if ($_SESSION['role'] !== 'admin') {
// Redirect to unauthorized page
header("Location: unauthorized.php");
exit;
}
// Admin-only functionality
echo "Welcome, Admin!";