Forum Moderators: coopster

Message Too Old, No Replies

login page - remember url

         

adammc

1:15 am on Sep 23, 2005 (gmt 0)

10+ Year Member



Hi,

I am including an access control page on all members area pages on my site, that checks that the user is logged in, if they arent logged in it displays a login form.

The problem is; the script isnt remembering the page they came from before having to login :(

For example:
I am running a job search site. On my main page I have links to all the jobs, the links go to each jobs 'more info' page.

On the job info page is a button / link that says 'apply for this job'. The link takes them to a seperate page where they input all the application stuff "IF LOGGED IN".

If they click that link on the job info page and they arent logged in it tells them to login by loading up the login page. Once logged in it takes them to my members area page, not back to the job application page.

I have created a seperate access control page and login form page just for this process but I have no idea how to get the script to 'get' the url or the page and how to send them there after logging in.

Can anyone help?

jd01

4:34 am on Sep 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Have you considered switching the login page to an include? Then you could server the content page if they are logged in, or show the login script on the page they are already on... if you post the login page to SELF you should be able to stay on the same page the whole time.

Justin

StupidScript

6:25 pm on Sep 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



jd01 gives an excellent suggestion, but if you really need to keep the login form off of each page, may I suggest using $_SESSION or $_COOKIE arrays?

Your issue is that you need to "maintain state", somehow. You are already doing it by "remembering" whether a visitor has logged in or not. You should be able to use the same mechanism for "remembering" which page they came from for the login.

(For these examples I'm assuming that the auth check comes on a "members only" page that redirects to the login page when not logged in and that you want them to return to after logging in ...?)

If you are using $_SESSION, then add something like this to the routine that checks whether they are logged in:

if (!isset($_SESSION["loggedin"]) {

session_start();

$_SESSION["prevPage"]=$PHP_SELF;

$loginPage="http: //example.com /login.php";

header("Location: $loginPage");

exit;

}

And after a successful login (in the login verification section):

if (isset($_SESSION["prevPage"])) {

$prevPage=$_SESSION["prevPage"];

header("Location: $prevPage");

exit;

} else {

header("Location: http: //example.com /index.html");

exit;

}

If you're using $_COOKIE then swap the $_SESSION code for $_COOKIE code.