redirecting the user to the same page after login


I have a header bar including a login form in every page of my website:

<header> <form action="login.php" method="post"> <input id="username" name="username" placeholder="Benutzername" type="text"> <input id="password" name="password" placeholder="Passwort" type="password"> <input class="submit-button" type="submit" value="Login"> </form> </header>

When clicking submit a php script in a separate login.php is executed. login.php is also an extra page where the user can try again after failing to login.

login.php looks like this:

$ousername = ''; //We check if the form has been sent if(isset($_POST['username'], $_POST['password'])) { //We remove slashes depending on the configuration if(get_magic_quotes_gpc()) { $ousername = stripslashes($_POST['username']); $username = mysql_real_escape_string(stripslashes($_POST['username'])); $password = stripslashes($_POST['password']); } else { $username = mysql_real_escape_string($_POST['username']); $password = $_POST['password']; } //We get the password of the user $req = mysql_query('select password,id from users where username="'.$username.'"'); $dn = mysql_fetch_array($req); //We compare the submited password and the real one, and we check if the user exists if($dn['password']==$password and mysql_num_rows($req)>0) { //If the password is right header('Location: index.php');

up here is where the problem starts. As you can see, the user is redirected to index.php when the form is sent and the login succeeds. What I would like to do is redirect the user to whatever page they are currently on when submitting the form (e.g. when the user is currently on a page that shows my website guidelines and logs in, I want him to be redirected to the very same guidelines-page when the login succeeds.)

I tried something like this:

if($dn['password']==$password and mysql_num_rows($req)>0) { if(($_SERVER['REQUEST_URl'] == login.php)){ header('Location: index.php'); } else { header('Location: '.$_SERVER['REQUEST_URl']); }

This is the last part of login.php:

//We save the user name in the session username and the user Id in the session userid $_SESSION['username'] = $_POST['username']; $_SESSION['userid'] = $dn['id']; lots html following...

I hope this is clear. Thank you!


You should use HTTP_REFERER instead of REQUEST_URl :

if($dn['password']==$password and mysql_num_rows($req)>0) { if(($_SERVER['HTTP_REFERER'] === "login.php")){ header('Location: index.php'); } else { header('Location: '.$_SERVER['HTTP_REFERER']); } }


you will need to use:


to return to the very last page before the login process!


You can access browser history using javaScript. Just include the script in the index page and send the users to the desired page.

Use window.history.go methodwindow.history reference<br /><a href="https://developer.mozilla.org/en-US/docs/Web/API/Window.history" rel="nofollow">https://developer.mozilla.org/en-US/docs/Web/API/Window.history</a> <br />

Or without jS

<a href="http://ha.ckers.org/blog/20070228/steal-browser-history-without-javascript/" rel="nofollow">http://ha.ckers.org/blog/20070228/steal-browser-history-without-javascript/</a>


  • In Python, how should one extract the second-last directory name in a path?
  • Custom URI routing by query string with CodeIgniter?
  • staticfiles and STATIC_URL with virrtualenv - django
  • Compare variables PHP
  • Yii: any way to save the images in compressed form?
  • Encode string to match encoded form field name in PHP POST array
  • Passing parameter through “window.location.href”
  • array_search() in Session Array
  • Extending the Django 1.11 User Model
  • google maps autocomplete bounces back already cleared text …odd…odd…odd
  • How can I make this modal persistent?
  • Getting error 'Cannot read property 'document' of undefined' while importing exp
  • Jquery Knockout: ko.computed() vs classic function?
  • quiver not drawing arrows just lots of blue, matlab
  • How to print columns containing value
  • Sending keystrokes/mouse clicks to a Java program with Autohotkey
  • Elasticsearch script query involving root and nested values
  • Execute scripts AJAX returns
  • Check for zero lines output from command over SSH
  • What and where is mdimport
  • With Hadoop, can I create a tasktracker on a machine that isn't running a datanode?
  • How do I open a C file with a relative path?
  • How to avoid particles glitching together in an elastic particle collision simulator?
  • How to install a .deb file on a jailbroken iphone programmatically?
  • Recording logins for password protected directories
  • DomPDF {PAGE_NUM} not on first page
  • Splitting given String into two variables - php
  • Javascript simulate pressing enter in input box
  • Regex thinks I'm nesting, but I'm not
  • What is the “return” in scheme?
  • Check if a string to interpolate provides expected placeholders
  • Does CUDA 5 support STL or THRUST inside the device code?
  • ActionScript 2 vs ActionScript 3 performance
  • Importing jscolor library in angular 2
  • Upload files with Ajax and Jquery
  • Delete MySQLi record without showing the id in the URL
  • php design question - will a Helper help here?
  • How to disable jQuery.jplayer autoplay?
  • A cron job substitute?
  • Proper folder structure for lots of source files