Sindu
Sindu

Reputation: 215

Saving sessions to pass to other pages

I have a user management system where the types of users are "admins" and "users". If the logged in user is admin he gets access to few pages. If the role is user he gets access to only one page. I need to save the session of the admin/user and check every time if he is "admin" so that the user doesn't illegally access information by just passing the URL. For now I can just save the name of the user but not his role. What do I do to pass the ROLE to next set of pages.

Home.php

   session_start();
   if( $_SESSION["logging"] && $_SESSION["logged"] )
  {
  echo $_SESSION[user];
  //Can be accessed only by admin 
  dashboard();
  }
  if(!$_SESSION["logging"])
   { 
  $number_of_rows=checkpass();
     if($number_of_rows==1)
        {   
         $_SESSION[user]=$_GET[userlogin];
         $_SESSION[logged]=true;
         print"<h1>you have loged in successfully</h1>";
         if($res['ROLE']=='user')
         {
                      //Accessible only by user.
         print "<h3><a href='user.php'>View Users</a></h3>";
                 logout();
         }
        }
        else
        {
            print "Please enter your log in information!";  
            loginform();
        }
    } 
    function checkpass()
    {
     //Database query
     if($res['ROLE']=='admin')
     { 
       //Redirected 
       dashboard();
     }

Upvotes: 0

Views: 106

Answers (2)

Prasanth Bendra
Prasanth Bendra

Reputation: 32740

Add this were ever required

$_SESSION['role']= role of the user;

Get $_SESSION['role'] in other pages.

Upvotes: 1

Yogesh Suthar
Yogesh Suthar

Reputation: 30488

use this

if($res['ROLE']=='user')
{
     $_SESSION['ROLE'] = "user";
                  //Accessible only by user.
     print "<h3><a href='user.php'>View Users</a></h3>";
     logout();
}

if($res['ROLE']=='admin')
 { 
   $_SESSION['ROLE'] = "admin";
   //Redirected 
   dashboard();
 }

for getting the ROLE use in other pages

echo $_SESSION['ROLE'];

Upvotes: 1

Related Questions