登录后将用户重定向到同一页面

时间:2015-01-08 10:46:09

标签: php

我的网站的每个页面都有一个包含登录表单的标题栏:

<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>

单击提交时,将执行单独的login.php中的php脚本。 login.php也是一个额外的页面,用户可以在登录失败后再次尝试。

login.php看起来像这样:

    $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');

这里是问题的起点。如您所见,在发送表单并登录成功时,用户将被重定向到index.php。我想要做的是在提交表单时将用户重定向到他们当前所在的任何页面(例如,当用户当前在显示我的网站指南并登录的页面上时,我希望他被重定向到相同的页面login - 登录成功后的页面。)

我试过这样的事情:

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']);
}

这是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... 

我希望这很清楚。谢谢!

3 个答案:

答案 0 :(得分:1)

你需要使用:

$_SERVER['HTTP_REFERER'];

返回登录过程前的最后一页!

答案 1 :(得分:1)

您可以使用javaScript访问浏览器历史记录。只需将脚本包含在索引页面中,然后将用户发送到所需的页面即可。

使用window.history.go方法

window.history参考
 https://developer.mozilla.org/en-US/docs/Web/API/Window.history

或没有jS

http://ha.ckers.org/blog/20070228/steal-browser-history-without-javascript/

答案 2 :(得分:1)

您应该使用HTTP_REFERER而不是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']);
  }
}
相关问题