简体   繁体   中英

redirect user with session user role in their respective dashboard

I have kept the array in the session and i want to redirect user to admin if the user role is admin and other also. But i am unable to do so?? I dont know where the problem is. The session is not doing properly. Can anyone please help me??

$session_user = array($user_id = $row['user_id'],
$fullname = $row['fullname'],
$username = $row['username'],
$phone_number = $row['phone_number'],
$state = $row['state'],
$city = $row['city'],
$street = $row['street'],
$email = $row['email'],
$user_role = $row['user_role']);

$_SESSION['userdata'] = $session_user;
/*header('Location:hello.php');*/
if($_SESSION['userdata']== 'admin'){
    /*header('Location:admin/admindashboard.php');*/
    header('Location:hello.php');
}else{
    /*header('Location:contributor/contributordashboard.php');*/
        /*header('Location:hello.php');*/
    echo "Error in identifying the user role";
}

try adding (temporarily) this line before the IF clause:

print_r($_SESSION);

This will output the session array so you can determine what is the exact location of the user_role variable within it.

It looks like $_SESSION['userdata'] contains an array.

Best bet is that the user role will be contained in

$_SESSION['userdata']['user_role']

but instead of trial and error, the print_r should give you the definite answer

$session_user = [
    'user_id'      => $row['user_id'],
    'fullname'     => $row['fullname'],
    'username'     => $row['username'],
    'phone_number' => $row['phone_number'],
    'state'        => $row['state'],
    'city'         => $row['city'],
    'street'       => $row['street'],
    'email'        => $row['email'],
    'user_role'    => $row['user_role']
];

This should give you correct indexes.

$session_user->user_role gives you your role

$_SESSION['usersata']['user_role'] should also return your role.

It seems that there is typing mistake in your array. You cannot assign value using php variables and equal operator. You need to use arrow operator for the same. Assuming below array

$session_user = array('user_id' => $row['user_id'],
'fullname' => $row['fullname'],
'username' => $row['username'],
'phone_number' => $row['phone_number'],
'state' => $row['state'],
'city' => $row['city'],
'street' => $row['street'],
'email' => $row['email'],
'user_role' => $row['user_role']);

Change

$_SESSION['userdata']

to

$_SESSION['userdata']['user_role']

to get the user_role because you are storing array in your session. Like below:

if($_SESSION['userdata']['user_role']== 'admin'){
    /*header('Location:admin/admindashboard.php');*/
    header('Location:hello.php');
}else{
    /*header('Location:contributor/contributordashboard.php');*/
        /*header('Location:hello.php');*/
    echo "Error in identifying the user role";
}

PS: you can check your user_role index using

echo "<pre>"
print_r($_SESSION['userdata']);
echo "</pre>"

Hope this will help.

The value of $_SESSION['userdata'] is an array of TRUE s (assignments in PHP result in the value TRUE. In PHP arrays are name to value pairs, and the name should be a string followed by => and then the value. For example:

$session_user = array('user_id' => $row['user_id'],
...

Then you can use the values like @Sehdev suggested.

When you are not sure if the variables/arrays have been correctly filled in with the values you need, you can do var_dump($session_user);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM