Biology Forums - Study Force

Other Fields Homework Help Other Topic started by: JuliaH on Nov 30, 2012



Title: How to check what kind of user is logged in using PHP?
Post by: JuliaH on Nov 30, 2012
I want to program a website that has two different kind of users: one is admin and the other one is called user.

The admin has certain rights whereas the user does not have all the right an admin have.

How do I use PHP to store what type of user is logged in and can be used on every other pages? In my database, admin has an id of 1, and user has an id of 2.


Title: How to check what kind of user is logged in using PHP?
Post by: rizzi on Nov 30, 2012
Interesting... You could try making a table with a session list in it, and store a cookie with an id or something. For every page a user visits, a new entry is made in the session table for that id. Then it's just a simple matter of writing a function that outputs that session table.


Title: How to check what kind of user is logged in using PHP?
Post by: rj.lall on Nov 30, 2012
You're going to want to use $_SESSION.  When the user logs in run a mysql query to grab the type of user.  After you have the variable just use something like this to store it:session_start();
$_SESSION['uid'] = $uid;
$_SESSION['type'] = $type;
session_write_close();
?>

Then whenever you want to check what kind of user is logged in you can just use:

session_start();
$type = $_SESSION['type'];
session_write_close();
?>

Just make sure to use it at the top of the page.  If you try to run session_start() after anything has been output to the web page it won't work.  Just email me if you need any help with this.