Skip to content
Advertisement

Cannot Redirect to the correct page using PHP session

In my application I have 4 roles. As per the roles, Users can view their access different pages. Currently I am maintaining 2 different sessions. $_SESSION['user_id'] and $_SESSION['role_id'].

My problem is, suppose we say the role_id =1 is Admin, role_id=2 is Doctor. If I logout from this session as Admin and logging as a Doctor to the site. Then I have click back button in web browser, it will redirect to the Admin page. I don’t want to redirect to the Admin page, just I want to remain in the same page.

Here I have used this codes to redirect but no working.

 //  redirect to main page according to the user role 
     if(isset($_SESSION['user_id']) && $_SESSION['role_id'] == 1){//admin
        header("location: index-2.php");
        exit;
     }

     if(isset($_SESSION['user_id']) && $_SESSION['role_id'] == 2){ // doctor
        header("location: index.php");
        exit;
     }
     
     if(isset($_SESSION['user_id']) && $_SESSION['role_id'] == 3){// lab
        header("location: index-lab.php");
        exit;
     }

     if(isset($_SESSION['user_id']) && $_SESSION['role_id'] == 4){// recep
        header("location: index-recep.php");
        exit;
     }

I don’t know where I went wrong. Could someone help me. Any help may highly appreciated.

Advertisement

Answer

As Cid mentioned, the cache is loaded when you click the back button. This isn’t something I’ve really tried, but you could try setting cache settings for your application to prevent the pages from being cached. See How do we control web page caching, across all browsers?

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement