栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

我根本不会使用cookie。

方法1

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

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

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

对于登录页面,您可以:

<?phpsession_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中,您将需要一些逻辑,如果登录失败,该逻辑将重定向回登录页面,同时还要检查引荐来源是否确实是您的网站(如果不是),则重定向回首页。

方法3

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

更改脚本,这样还可以告诉登录页面记住您的位置:

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

header("Location:login.php?location=" . urlenpre($_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" />

登录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=' . urlenpre($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=' . urlenpre($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();}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/443061.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号