Tis is how I did it and it proved to be a good concept. I have defined access levels which were integers. The higher the level (value) the higher the privileges.
Between each level I had a space for new levels if I need them later. The access level is saved in the user database for each user.
access_level | description
--------------------------
220 | application admin (developes only)
200 | contents admin
180 | system admin
100 | regular user (edit, view)
60 | viewer (view)
0 | no access
As you can see I use unsigned integer for access level so it does not take much space in db. I have plenty of room below and above the range and also between access levels which proved good tactics since I had to add levels already. The access level gets stored in the session during login so all I have to do is to compare it with required level on the beginning of each page:
// example for checking a system admin's access level
if(!isset($_SESSION['access_level']) || $_SESSION['access_level'] < 180) {
header('location: logout.php');
}
In the above example only system admin and higher levels can access the page.
I also define constants to make the code more readable:
define('ACCESS_LVL_APPADMIN', 220);
define('ACCESS_LVL_CONTADMIN', 200);
define('ACCESS_LVL_SYSADMIN', 180);
define('ACCESS_LVL_REGUSER', 100);
define('ACCESS_LVL_VIEWER', 60);
define('ACCESS_LVL_NOACCESS', 0);
// example for checking a system admin's access level
if(!isset($_SESSION['access_level']) ||
$_SESSION['access_level'] < ACCESS_LVL_SYSADMIN) {
header('location: logout.php');
}
broj1
Nearly a Posting Virtuoso
1,211 posts since Jan 2011
Reputation Points: 167
Solved Threads: 164
Skill Endorsements: 13
I'd use a bitwise operator and give groups this sort of value:
Group1 - 1
Group2 - 2
Group3 - 4
Group4 - 8
Group5 - 16
(etc)
Then an user can be members of more than one group:
User1 - 12 (memner of group 3 and 4)
meaning that they now have security clearance for both those groups.
You can check for rights:
define('GROUP1', 1);
define('GROUP2', 2);
define('GROUP3', 4);
if($userlevel & GROUP3){
//this will allow anybody with userlevels 4,5,6,7,12... access to this section of code
}
diafol
Keep Smiling
10,642 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,508
Skill Endorsements: 57
if the user's access_level is below than 180 it will directly go to the logout page right?
Yes. It also redirects you to logout if the session variable does not exist.
i tried that way, but even the access level is higher that 180, it still directly go to logout page.
Can you show the code. Have you started the session? Does the $_SESSION['access_level'] exist at all?
broj1
Nearly a Posting Virtuoso
1,211 posts since Jan 2011
Reputation Points: 167
Solved Threads: 164
Skill Endorsements: 13
You can do it using case statement:
if(!isset($_SESSION['access_level']) || $_SESSION['access_level'] < 180) {
header('location: logout.php');
} else {
switch($_SESSION['access_level']) {
case 220 : header('location:restricted_page1.php'); break;
case 200 : header('location:restricted_page2.php'); break;
case 180 : header('location:restricted_page1.php'); break;
default : header('location:logout.php');
}
}
I haven't noticed your question to me about access levels in one of your previous posts. Have you got those answers yet?
broj1
Nearly a Posting Virtuoso
1,211 posts since Jan 2011
Reputation Points: 167
Solved Threads: 164
Skill Endorsements: 13
would you explain a bit more about your method..
there is one page, only 2 group(G1 and G3) can access the page
i think, this problem can be solved with your method..but i'm not sure how to do it..
No problem - just so that you're aware, broj1 has the solutions that you're looking for. But for completeness and since I left it dangling there, I elucidate:
DB Table
group_id (autonumber) | groupname (varchar) | groupvalue (int)
1 | G1 | 1
2 | G2 | 2
3 | G3 | 4
4 | G4 | 8
5 | G5 | 16
6 | G6 | 32
User Table
user_id (autonumber) | username (varchar) | groups (int)
23 | diafol | 7
45 | eros | 17
The above means:
diafol is a member of groups G1, G2, G3 (1 + 2 + 4)
eros is a member of groups G1, G5 (1 + 16)
I suppose you could create your constants dynamically.
define("G1", 1);
define("G2", 2);
define("G3", 4);
define("G4", 8);
define("G5", 16);
define("G6", 32);
$usergroups = 34; //this would be set from DB on login - 34 only made up from 2 + 32
if($usergroups & G1)echo "G1"; // (34 & 1) - no match
if($usergroups & G2)echo "G2"; // (34 & 2) - match!
if($usergroups & G3)echo "G3"; // (34 & 4) - no match
if($usergroups & G4)echo "G4"; // (34 & 8) - no match
if($usergroups & G5)echo "G5"; // (34 & 16) - no match
if($usergroups & G6)echo "G6"; // (34 & 32) - match!
diafol
Keep Smiling
10,642 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,508
Skill Endorsements: 57
But for completeness and since I left it dangling there
Still very nice example of using bitwise operators. Useful in other cases, too, like PHP error reporting.
broj1
Nearly a Posting Virtuoso
1,211 posts since Jan 2011
Reputation Points: 167
Solved Threads: 164
Skill Endorsements: 13
1) You need to place exit; after a header();
2) Check the value of $_SESSION['access_level'] with an echo immediately after the session_start();
This should block the header() as it causes output, but at least you'll see the value on the screen.
diafol
Keep Smiling
10,642 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,508
Skill Endorsements: 57
It would be also expected that you logout visitors with access level less than 100, not greater than 100. The higher the acces level the more rights the user has. Like below:
if(!isset($_SESSION['access_level']) || $_SESSION['access_level'] < 100) {
broj1
Nearly a Posting Virtuoso
1,211 posts since Jan 2011
Reputation Points: 167
Solved Threads: 164
Skill Endorsements: 13
The only thing I can come up with is that the url might be incorrect. Have you checked spelling?
broj1
Nearly a Posting Virtuoso
1,211 posts since Jan 2011
Reputation Points: 167
Solved Threads: 164
Skill Endorsements: 13
Check if $_SESSION['access_level'] is really 220. You can also try to put the line
header('location:borangK8.php');
on top of the script to see whether redirection works.
Also make sure no html (not even a space) is sent before header() function. Check your script and included files for output.
broj1
Nearly a Posting Virtuoso
1,211 posts since Jan 2011
Reputation Points: 167
Solved Threads: 164
Skill Endorsements: 13