I am trying to create a PHP website with more than one administrator. I have some doubts
How can I check if an admin is logged in? I know I must use sessions for security reasons, but how can I recognize which one of the administrators is logged in?
Should I set a
$_SESSION['username']
variable whenusername
performs login and send a cookie containingusername
too? This way, I know who is him (or pretends to be) and can check ifusername
is actually logged in, checking$_SESSION['username']
before showing pages. Is it secure?
Same way as you check if a normal user is logged in. A session, ofcourse. Both, normal user and administrator should have
$_SESSION['username']
set in. To make someone administrator, I guess, you are using a flag in the db, let's say the column isaccess_level
. ENUMs are1
=> user,2
=> adminitrator. So when you login the user, put into session this one too.$_SESSION['access_level']
will tell you if this user is admin, and$_SESSION['username']
will tell you its username.Sessions are enough. They do set cookies on client site aswell. Do not store additional
$_COOKIE['username']
.In a few words - a model method for login. If user is admin, normal, etc, could be done by the controller by using the extract of the model, which queries the DB taking username, password, access_level, etc.
So check on controller as you wish, with the method above.