在重定向系统中对这个 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 堆叠(不会去任何地方)即使我已经登录,它应该去自动 thread.php。
这是我在 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 中的提交按钮(转到 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,这是造成这种情况的原因吗?或者如果我在一页中设置 2 个 cookie 有关系吗?另一个cookie设置是这样的(在页面顶部)
$cooval2='juna';
setcookie($coousername, $cooval2, time() + (3600 * 24 * 365), "/"); // 1 year
我根本不会使用 cookie。
方法一
一种可能的方法是将访问的 link 存储到 session 变量中,然后当用户到达 login.php 页面时,提供 header 重定向到$url 由 session 变量给出。
将此代码粘贴到您网站或主容器中的所有页面中。
<?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");
方法二
到目前为止,一个更简单的解决方案就是:
<hidden name="redirurl" value="<? echo $_SERVER['HTTP_REFERER']; ?>" />
然后在他们登录后重定向到该地址。
但是,只有在每个页面上都有登录框时,这才有用。
$_SERVER['REQUEST_URI']
将只保留当前页面。你想要做的是使用 $_SERVER['HTTP_REFERER']
。
因此,将 HTTP_REFERER 保存在表单的隐藏元素中,但还要注意在处理表单的 PHP 中,您将需要一些逻辑,如果登录失败,则重定向回登录页面,但也检查引荐来源网址是否确实是您的网站,如果不是,则重定向回主页。
方法三
另一种常用的方法是通过 $_GET 变量将用户的当前页面传递给登录表单。
更改您的脚本,这样也可以让登录页面记住您的位置:
注意:$_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 => 本地存储,请确保在您要登录 url 的每个页面上都有此代码,登录页面除外,否则它会将您重定向回登录页面。
var currentPage = window.location.href;
localStorage.lastPageVisited = currentPage
这就是提交按钮的代码,它将把用户重定向到他访问的最后一个页面。
$(document).ready(function() {
$('button').click(function() {
window.location.href = localStorage.lastPageVisited;
});
});
我在 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 堆叠(不会去任何地方)即使我已经登录,它应该去自动 thread.php。
这是我在 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 中的提交按钮(转到 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,这是造成这种情况的原因吗?或者如果我在一页中设置 2 个 cookie 有关系吗?另一个cookie设置是这样的(在页面顶部)
$cooval2='juna';
setcookie($coousername, $cooval2, time() + (3600 * 24 * 365), "/"); // 1 year
我根本不会使用 cookie。
方法一
一种可能的方法是将访问的 link 存储到 session 变量中,然后当用户到达 login.php 页面时,提供 header 重定向到$url 由 session 变量给出。
将此代码粘贴到您网站或主容器中的所有页面中。
<?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");
方法二
到目前为止,一个更简单的解决方案就是:
<hidden name="redirurl" value="<? echo $_SERVER['HTTP_REFERER']; ?>" />
然后在他们登录后重定向到该地址。
但是,只有在每个页面上都有登录框时,这才有用。
$_SERVER['REQUEST_URI']
将只保留当前页面。你想要做的是使用 $_SERVER['HTTP_REFERER']
。
因此,将 HTTP_REFERER 保存在表单的隐藏元素中,但还要注意在处理表单的 PHP 中,您将需要一些逻辑,如果登录失败,则重定向回登录页面,但也检查引荐来源网址是否确实是您的网站,如果不是,则重定向回主页。
方法三
另一种常用的方法是通过 $_GET 变量将用户的当前页面传递给登录表单。
更改您的脚本,这样也可以让登录页面记住您的位置:
注意:$_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 => 本地存储,请确保在您要登录 url 的每个页面上都有此代码,登录页面除外,否则它会将您重定向回登录页面。
var currentPage = window.location.href;
localStorage.lastPageVisited = currentPage
这就是提交按钮的代码,它将把用户重定向到他访问的最后一个页面。
$(document).ready(function() {
$('button').click(function() {
window.location.href = localStorage.lastPageVisited;
});
});