Syed Arif Iqbal
Syed Arif Iqbal

Reputation: 1427

checking user role and restriction for user

Let suppose i’m working an application ( PHP & MYSQL )
and in this app has an user table for storing user information.
Users table column name

id user_name password role

There are some user role for accessing application. i) All
ii) Read only
iii) Edit
iv) Entery only
v) Etc.

I’was store there role separated with , now i’m not understanding how to check user role and code for that application...

I’m trying something like that..

$roles = implode(“.”,$_SESSION[‘user_role’]);  
If( array_key_exist(“all”,$roles) && array_key_exist(“edit”,$roles) ){
    // do some stuff...
}
If( array_key_exist(“read only”,$roles) ){
    // do some stuff...
}

Is it right way to do/checking role or please give me some suggestion if there is another best way to do so.. thanks

Upvotes: 0

Views: 75

Answers (1)

andy
andy

Reputation: 2002

First, assuming your $_SESSION contains the string with comma separated roles, you need to explode(',', $_SESSION['user_role']) it to get an array of roles.

To check if a role is contained in that array, use in_array("All", $roles) instead of array_key_exists() which will only search for keys, not values.

Upvotes: 1

Related Questions