重定向系统对此cookie感到困惑


confusing about this cookies in redirecting system

我在PHP中工作。我想在登录后将页面重定向到我想访问的最后一个页面,但我仍然在5个小时后堆积在这里,我仍然没有成功。这是模式,我有3个php文件。

newest.php (before login), 
signin.php (before login), 
thread.php (after login). 

我正在使用cookie进行重定向。首先我去了newest.php,然后我点击了按钮(转到thread.php)。然后thread.php发现你还没有登录,然后重定向到signin.php。然后我填写了登录表单后,我点击了提交按钮(signin.php),然后我在signin.php上堆叠(哪儿也不去),即使登录后,它也应该自动转到threadphp。

这是我在newest.php&thread.php(不在signin.php中):

$coopage='coopage';
$current_page='http://'.$_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI];
setcookie($coopage, $current_page,time()+86400,'/');

newest.php中的submit按钮(转到thread.php):

echo "<center><button onclick='"window.location='/thread/form''">add new thread</button></center>"; 

在signin.php中(在我点击提交按钮后或在提交区域,因为表单和提交后我在同一页面中制作)(在页面底部):

if(isset($_COOKIE[$coopage])){
    $url=$_COOKIE[$coopage];
    unset($_COOKIE[$coopage]);
    header('location:'.$url);
}

注意:在signin.php中,我在这个cookie之前还有另一个cookie设置,这是原因吗?或者,如果我在一个页面上设置了两个cookie,这有关系吗?另一个cookie设置是这样的(在页面顶部)

$cooval2='juna';
setcookie($coousername, $cooval2, time() + (3600 * 24 * 365), "/"); // 1 year

我根本不会使用cookie。

方法1

一种可能的方法是将访问的链接存储到会话变量中,然后当用户到达login.php页面时,提供一个重定向到会话变量给定的$url的头。

将此代码粘贴到网站或主容器上的所有页面中。

<?php
session_start(); 
$_SESSION['url'] = $_SERVER['REQUEST_URI']; 

对于登录页面,您可以拥有:

<?php
session_start();  // needed for sessions.
if(isset($_SESSION['url'])) 
   $url = $_SESSION['url']; // holds url for last page visited.
else 
   $url = "student_account.php"; 
header("Location: http://example.com/$url"); 

方法2

到目前为止,一个更简单的解决方案就是:

<hidden name="redirurl" value="<? echo $_SERVER['HTTP_REFERER']; ?>" />

然后在他们登录后重定向到该地址。

然而,只有当你在每个页面上都有一个登录框时,这才是好的。

$_SERVER['REQUEST_URI']将简单地保持当前页面。您要做的是使用$_SERVER['HTTP_REFERER']。因此,请将HTTP_REFERER保存在表单的隐藏元素中,但也要注意,在处理表单的PHP中,如果登录失败,您需要一些逻辑来重定向回登录页面,但也需要检查refer是否真的是您的网站,如果不是,则重定向回主页。

方法3

另一种常见的方法是通过$_GET变量将用户的当前页面传递到Login表单。

修改你的脚本,这样也会告诉登录页面记住你在哪里:

注意:$_SERVER['REQUEST_URI']是您当前的页面

header("Location:login.php?location=" . urlencode($_SERVER['REQUEST_URI']));

现在检查它是否已填充,然后将用户发送到以下位置:login.php

echo '<input type="hidden" name="location" value="';
if(isset($_GET['location'])) {
    echo htmlspecialchars($_GET['location']);
}
echo '" />';
//  Will show something like this:
//  <input type="hidden" name="location" value="previousPage.php" />

login-check.php

session_start();
//  our url is now stored as $_POST['location'] (posted from login.php). If it's blank, let's ignore it. Otherwise, let's do something with it.
$redirect = NULL;
if($_POST['location'] != '') {
    $redirect = $_POST['location'];
}
if((empty($username) OR empty($password) AND !isset($_SESSION['id_login']))) {
    $url = 'login.php?p=1';
    // if we have a redirect URL, pass it back to login.php so we don't forget it
    if(isset($redirect)) {
        $url .= '&location=' . urlencode($redirect);
    }
   header("Location: " . $url);
   exit();
}
elseif (!user_exists($username,$password) AND !isset($_SESSION['id_login'])) {
    $url = 'login.php?p=2';
    if(isset($redirect)) {
        $url .= '&location=' . urlencode($redirect);
    }
   header("Location:" . $url);
   exit();
}
elseif(isset($_SESSION['id_login'])) {
    // if login is successful and there is a redirect address, send the user directly there
    if($redirect)) {
        header("Location:". $redirect);
    } else {
        header("Location:login.php?p=3");
    }
    exit();
}

您的新cookie=>本地存储,请确保在除登录页面外的每个页面上都有此代码,否则它会将您重定向回登录页面。

var currentPage = window.location.href;
localStorage.lastPageVisited = currentPage

这就是提交按钮的代码,它会将用户重定向到他访问的最后一个页面。

$(document).ready(function() {
    $('button').click(function() {
        window.location.href = localStorage.lastPageVisited;
    });
});