I have a facility to redirect people to the login page when they try to access another, this is the code; $url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']); if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\') ) { $url = substr ($url, 0, -1); } $url .= '/login.php'; ob_end_clean(); header("Location: $url"); exit(); Code (markup): What I would like to do after they have logged in is to detect which page they were trying to access and redirect them to that page. Can anyone help?
Store it in a variable then bounce them back. header('Location: '.$_SERVER['HTTP_REFERER']); Code (markup):
Would I need to do that on the page that I redirect in the first place, or on the login page? Something like this. $redir = header('Location: '.$_SERVER['HTTP_REFERER']); $url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']); if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\') ) { $url = substr ($url, 0, -1); } $url .= '/login.php'; ob_end_clean(); header("Location: $url"); header("Location: $redir"); exit(); Code (markup):
What he means is in login.php after they've successfully logged in you'd redirect them back to the page they came from. The code you have for the page is fine. You'd add the redirect to the login script.
Please forgive my ignorance, but I have tried a few ways to incorporate this code into my login script, but with no success at all. Could you please show me how it should work?
I do not think their method will work because you're first redirecting them to the login page, then when they submit the login info, they want you to make it redirect to the referrer page. If you think about it, wouldn't the referrer page always be the login.php page because that's the page they used to submit the information? The only thing I can think of is setting a SESSION variable, which I believe uses some type of temporary cookie only for the current session (expires if they're inactive). This would require them to have cookies enabled but obviously they do if they're able to login. Anyways.. try this! Put this ABOVE your header("Location: $url"); $_SESSION['last_page'] = basename($_SERVER['PHP_SELF']); PHP: Then, in the script that login.php sends the login information to for processing (most likely login.php itself), when you're ready to redirect them back to where they were, put this: if(!empty($_SESSION['last_page'])) { //They were redirected from another page $last_page = $_SESSION['last_page']; //$last_page is now set to the filename they were previously trying to access! $_SESSION['last_page'] = null; //Remove the value unset($_SESSION['last_page']); //Make sure it's completely DEAD.. probably not necessary :P header("Location: $last_page"); } PHP: If your files are also in a bunch of different directories and you want to make sure they go back to the proper directory, just remove the basename( ) part as that will only extract the filename and will remove the dir names.
You're right but you don't need to use a session variable. Just set it in a hidden field. In login.php add this to the form: <input type='hidden' name='redirectto' value='<?php echo $_SERVER['HTTP_REFERER']; ?>'> and then when the user successfully logs in you'd redirect to that page. The one trick is that if they don't input the correct username/password you'll want to retain the redirect info from the form rather than using the referrer.
Thanks Zerxer, that code works perfectly, let me know if you like our recordings, and I'll send a link to any album of your choice from our site. The site is www.home-groan.com, that is the html site.
You should (in most cases) add die() after this code. header('Location: '.$_SERVER['HTTP_REFERER']); die(); Code (markup):
Zerxer, If you send me an image logo I'll place a link to your site on my main navigation column, with a little thanks for the script help you gave me.
does HTTP_REFERER even work on a header redirect? why not add another variable in the url called &page=/account