开发者

Filtering access to admin

I have a side menu that I want to 开发者_JAVA技巧to have the Admin option ONLY display IF the username is an admin. Simply if they are an admin, it's there, if they are not it shows another link to their profile.

Again, KISS (Keep It Super Simple)...please I am an noob.

Thanks


add to your database 'user' table column 'is_admin' and set it up for user

get the value and do sth like:

if(isadmin($user))
{
  echo "your admin bar";
}


When the user connects, you can store a boolean in a session variable that says if the user is an admin. Then you can retrieve it :

if($_SESSION['admin'])
    echo 'The admin link';
else
    echo 'The profile link';

See the php manual to know how to use session variables.


First, you go and do a Query to see if there are any records with that username, and the admin field checked on. If there are, then that username is an admin. This won't work however if there are multiple users with the same username, so you might want to use a user ID instead.

function check_admin($username)
{
    // Look for a person with that username, and the admin field is set to ON
    $sql = 'SELECT * FROM TABLE WHERE User_Name="'.$username.'" AND Admin=1';
    $result = mysql_query($sql);
    // If there are any results, the person should be an admin (given usernames are unique)
    if(mysql_num_rows($result) !== 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}

Then you would just use it as a check

if(check_admin("bob") == true)
{
     // super secret admin stuff
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜