CodeIgniter Forums
Session variable related issue - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Session variable related issue (/thread-43329.html)



Session variable related issue - El Forum - 07-07-2011

[eluser]Aziz Chouhan[/eluser]
how can i check session variables when one could click on browser back button,after logout from the site


Session variable related issue - El Forum - 07-07-2011

[eluser]Mirge[/eluser]
They would see a cached version of the page if anything. Upon refresh (assuming you have code in place that IS checking to make sure they are logged in), they wouldn't be able to access anything they're not supposed to.


Session variable related issue - El Forum - 07-08-2011

[eluser]Aziz Chouhan[/eluser]
but i don't want to see that cached version of the page. if one is seeing that page,after loading the page it must go on the log in page


Session variable related issue - El Forum - 07-08-2011

[eluser]boltsabre[/eluser]
You could try looking at the documentation

Session Library

To check if a session variable exists just do this:

Code:
if(!isset($this->session->userdata('userId'))){
   //whatever you want to do, i.e redirect to you login controller
}



Session variable related issue - El Forum - 07-08-2011

[eluser]Aziz Chouhan[/eluser]
thanks for this suggestion.
but i really did so very first moment,but nothing could affect my problem.
ok thanks for suggestion.


Session variable related issue - El Forum - 07-08-2011

[eluser]boltsabre[/eluser]
Hmmm, sorry we couldn't help.

To the best of my knowledge, when a user clicks on the 'back' button either on the screen, or if they have a fancy mouse with a back button and they press it, the page will reload - it does not pull the page from a cache, but will actually pull the file from the server again, thus running all js and php (or whatever) scripts again.

So... if, and correct me if I'm wrong, in your situation you want to check if a user is logged in, even if they click the back button, after they have logged out. Well, any page that can only be accessed whilst logged in should check for a 'logged in' session variable. Best to put it in a helper, and just call it in every controller function that requires it (place it up the top, it should run before any other of that controllers code to prevent issues). It would look something like this:

Code:
// this is your helper function
public function checkLoggedInStatus(){
   if(!isset($this->session->userdata('userId'))){
      // session variable NOT set, user is NOT logged in.
      redirect ('myUserController/login');
   }
}

// and just call it in your controllers like this
public function thisIsAControllerYouMustBeLoggedInToView(){
  // load your helper (well duh)
   $this->load->helper('loggedInHelper');
   $check = checkLoggedInStatus();
   // if user not logged in helper function redirects them to myUserController/login, else
   // continue with this controller and its intended purpose

   // rest of controller code here

And that's it... if a user isn't logged in they get redirected to the login page. Please let us know if that does/does not fix your problem.


Session variable related issue - El Forum - 07-08-2011

[eluser]Mirge[/eluser]
[quote author="Aziz Chouhan" date="1310133508"]but i don't want to see that cached version of the page. if one is seeing that page,after loading the page it must go on the log in page[/quote]

You have no control over how their browser behaves in that regard.