2
votes

I am developing a CMS. I have three scripts, and 2 session variables

 $_SESSION['logged']
 $_SESSION['redirect'] 

"auth.inc.php" - Run every time a user requests a page. It checks if $_SESSION['logged'] is set and equal to 1. If it isn't redirects ti main page using

header(location:index.php)

and the page referrer link is saved in $_SESSION['redirect']. If $_SESSION['logged'] is set inactivity time is less than a required value it redirects to the requested page other wise it redirects to logout.php

"index.php" - checks for user name and password. I successful sets

$_SESSION['logged']=1

If $_SESSION['redirect'] is set it redirects to that page otherwise to deafult page

"logout.php" - Its unsets and destroys the session using

session_unset();
session_destroy();

But I want if user is logged out due to session timeout then after login it should be redirected to old page. Since I was unsetting the session $_SESSION['redirect'] was lost. So instead of using session_unset() I only unset $_SESSION['logged']

unset($_SESSION['logged']);
session_destroy();

but still I am not able to retrieve $_SESSION['redirect'] after session_destroy(). The variable is still not set. How should I redirect to old page after logout due to session expire

3

3 Answers

3
votes

session_destroy() will, in fact, destroy the data for the current session. To use any value you set, you have to session_start() again.

Furthermore, if the session expires, nothing you have set as a session cookie will be valid.

The best thing you could do is to save your last page or whatever in the database, since you said the users can log in. Then fetch that value upon login, and redirect to it.

Good luck!

1
votes

Well your first issue is that you are still destroying $_SESSION['redirect'] when you call session_destroy(). What you could do is either hard code a default URL into the system so that you can destroy the session and still redirect. The other option is to remove the session_destroy() command until after you have redirected.... i.e. on the redirect page, check to see if the previous page (referer) was logout.php, if it was destroy the session otherwise dont

0
votes

@Marc Towler: Sorry, I can't post comment now. What I want to say is that not all the Browser support referer page, like IE.