Log in

View Full Version : Login form that redirects to the page it came from



Beverleyh
06-16-2009, 10:52 AM
Hi,

This login form is in my website's template header file and php included so it shows on all pages of my website.


<form action="login.php" method="post" enctype="multipart/form-data">
Username: <input type="text" name="username"><br>
Password: <input type="password" name="password"><br>
Remember Me? <input type="checkbox" name="autologin"><br>
<input type="hidden" name="redirect" value="index.php">
<input type="submit" value="Login" name="login">
</form>

At the moment, a user can log in from any page but the hidden redirect always sends the user back to the index page.

I would like the form to send the user back to the page where they logged in from so its less confusing.

Is there a script that can do this or do I have to resort to putting a login form in every page and customising the redirect url?

Any advice is welcome.

Thanks

traq
06-16-2009, 02:10 PM
Try this. (not tested.) Make sure all your pages are named whatever.php instead of .html.


<form action="login.php" method="post" enctype="multipart/form-data">
Username: <input type="text" name="username"><br>
Password: <input type="password" name="password"><br>
Remember Me? <input type="checkbox" name="autologin"><br>
<input type="hidden" name="redirect" value="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="submit" value="Login" name="login">
</form>

Beverleyh
06-16-2009, 03:48 PM
Hi traq,

Thanks for replying. I tried your suggestion along with $_SERVER['HTTP_REFERER']); but they didnt work for me.

It got my cogs turning though cos I found a workaround - I created a new page called "redirect.php" with this code in it:

<?php
header('Location:' . $_SERVER['HTTP_REFERER']);
exit();
?>

I then changed the redirect url to point to it like: <input type="hidden" name="redirect" value="redirect.php"> and it does the job brilliantly.

Thanks

traq
06-18-2009, 04:59 AM
glad you got it working. I figured that the hidden input was used by some part of the login page, so without seeing that page, it was kinda a shot in the dark as to whether it would work or not.