+ 2

How to allow ADMIN only access for a specific PHP page?

I have a table ‘users’ in MySQL with a ‘role’ column. I only want admins to access users.php (show all users of the system). How do I do this? In the users.php file, I have: f ($_SESSION['role'] != 'admin'){ echo 'acess denied'; exit; } I’m getting: Warning: Undefined array key "role" in C:\xampp\htdocs\info2180-project2\users.php on line 4 acess denied https://code.sololearn.com/wZS49vxskyjK/?ref=app

4th Dec 2022, 11:28 PM
Triz
Triz - avatar
1 Answer
+ 2
Using the session is a good idea, but you have to actually set the 'role' key in the session at some point. Most likely a good opportunity to do that, is when the user has just logged in. https://www.w3schools.com/php/php_sessions.asp
5th Dec 2022, 4:21 AM
Tibor Santa
Tibor Santa - avatar