How can PHP be used to differentiate between admin and user functions on a website?
To differentiate between admin and user functions on a website using PHP, you can create a session variable upon login that stores the user's role (admin or user). Then, you can check this session variable on each page where admin-specific functionality is required.
// Upon successful login, set the user's role in a session variable
$_SESSION['role'] = 'admin'; // or 'user'
// Check the user's role on each page where admin-specific functionality is required
if($_SESSION['role'] == 'admin'){
// Admin-specific functionality here
} else {
// User-specific functionality here
}
Keywords
Related Questions
- What steps can be taken to ensure that all input fields in a form are properly filled and submitted to avoid empty values in PHP?
- Is it considered a best practice to have all website content run through index.php in PHP development?
- How can PHP scripts be modified to automatically delete old images when a new image is uploaded by a user?