Welcome Guest, Not a member yet? Register   Sign In
Determining whether a session expired
#1

[eluser]mrmeyers99[/eluser]
On my site I'm using the PHP session library to store the user sessions. If a user tries to access a page when they aren't logged in, I need to redirect them. How can I tell if their session expired vs if they just try to get to that page without being logged in?
#2

[eluser]TheFuzzy0ne[/eluser]
Assuming your referring to native PHP sessions, when the user logs you'll normally set some kind of variable to indicate that a user has logged in:

Code:
$_SESSION['logged_in'] = TRUE;

All you need to do is check that isset($_SESSION['logged_in']), and the job's a good'en.
#3

[eluser]mrmeyers99[/eluser]
but once the session expires $this->session->userdata('logged_in') will return the empty string.
#4

[eluser]Dam1an[/eluser]
it returns false, so if you do
Code:
if($this->session->userdata('logged_in'))

and it returns false, its either cause they're not logged in or its timed out, and in both cases, you want the same to happen Smile
#5

[eluser]mrmeyers99[/eluser]
Right but I want to give them a different message depending on if their session expired vs. if they just tried to access the page without logging in. (i.e. "Your session has expired" vs. "You must log in to see this page")
#6

[eluser]mrmeyers99[/eluser]
Plenty of other sites do this, so it must be possible. If the session expires after 10 minutes of inactivity there has to be a way to tell between that and someone who hasn't logged in right?
#7

[eluser]TheFuzzy0ne[/eluser]
I think it's usually done using two cookies. One expires normally, but the other lasts much longer (I like to call it the "remember me" cookie).




Theme © iAndrew 2016 - Forum software by © MyBB