Jump to content

restrict permissions..


techker

Recommended Posts

Hey guys i have a script that i made with multiple permissions..

 

i need to add in the pages restitutions for diffrent levels..

 

 

so i got the level

 

$query = "SELECT * FROM users WHERE `username`='$username_from_cookie'"; 

$numresults=mysql_query($query);
$numrows=mysql_num_rows($numresults); 

// get results
$result = mysql_query($query) or die("Couldn't execute query");

// now you can display the results returned
while ($row10= mysql_fetch_array($result)) {

$permissions= $row10["permissions"];

echo '$permissions';

}

Now to restick im ok with like to but more then that i get confused..

 

this shows navigation on levels of permissions..

 

if ($row10['permissions'] == 2) {
print "<a href=\"U.php\"><img src=\"./Icons/Users.png\" title=\"Prof\" /></a>";
}
else {
print "<img src=\"./Icons/Users_o.png\"/>";
}

 

2 levels


if ($row10['permissions'] == 5) {
print "<a href=\"Prof_1.php\"><img src=\"./Icons/sec.png\" title=\"Enseignant(e)\"/></a>";
} elseif ($row10['permissions'] == 2) {
print "<a href=\"Prof_1.php\"><img src=\"./Icons/sec.png\" title=\"Enseignant(e)\"/></a>";
} else {
print "<img src=\"./Icons/sec_o.png\" title=\"Enseignant(e)\"/>";
}

 

ok so instead of have 10 lines of codes can i

 

$row10['permissions'] == 5&2&3  ???

 

and can i do if not permissions ==5 redirect to loggin..

thx

Link to comment
Share on other sites

I'm not sure I follow you completely. Before I try to answer your question I'd like to point out that the first block of code you posted is running the query twice! And, if you only need one column from the query use that in the SELECT statement instead of '*':

$query = "SELECT permissions FROM users WHERE `username`='$username_from_cookie'"; 
$result = mysql_query($query) or die("Couldn't execute query");

if(mysql_num_rows($numresults))
{
    $permissions = mysql_result($result, 0);
    echo "Permission level: $permissions";
}
else
{
    echo "No records returned.";
}

 

Now, as to your problem. It seems you have some functions that should be enabled based upon several permission levels. I would suggest you redo your permission levels and functions in one of two ways.

 

1. Design the permission levels such that someone with permission level 2 has all the functions for permission level 2 and permission level 1. Likewise, someone with permission level 5 has all functions for permissions 5-1. So, then your permission checking becomes simple:

if($permissions >= 5)
{
    //Show feature that is available for permissions 1-5
}

 

Or, you can utilize a bitwise operator. In this model your permissions are set based upon the "bits" in a binary number (however they are stored an regular 10 base numbers). In the binary number 00101 there are two bits in the ON/True positions: the first and the third - they work backwards. In this model each bit is a different permission. Perhaps one for managing users, another for managing products, or whatever. The decimal equivalent of that number would be 5. You could then build a function that passes the user's permission value along with a number that represents what bits are required for a particular feature and have that function return true or false.

 

PHP has many functions for dealing with bitwise operators so you don't have to do the math. But, it is beyond the scope of a forum post to explain them or how this would be built. There are probably tutorials available if you care to search.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.