【问题标题】:php cache vs cookiephp缓存与cookie
【发布时间】:2013-03-17 08:22:29
【问题描述】:

我正在集成一个登录页面(固定用户名和密码)。

用户登录后,他将被重定向到另一个页面“x”(在我的服务器上)。

但是,当用户关闭浏览器(或选项卡)并重新打开它时,他会自动被定向到页面“x”,而无需询问用户名并通过。

但是,如果我从浏览器 (firefox) 设置中删除 cookie,一切都会恢复正常。删除缓存没有任何作用。

我知道我需要插入几行代码才能删除到 cookie。 我的问题是,

  1. 这是 100% 的 cookie 问题吗?还是我也需要防止存储到本地缓存中?
  2. cookie 预防发生在哪个级别?在登录或重定向期间?
  3. 一旦我被重定向到页面“x”,是否在此处放置一个注销按钮可以注销重定向的会话?

下面是我的代码。

<?php
session_start();
if(isset($_POST['username'])){
 if(($_POST['username'] == "user") && ($_POST['password'] == "pass"))
{
  $_SESSION['secured'] = "Secured";
 }else{
  echo "Wrong username and password.  <p>
  <a href='?'retry</a>";
 }
}

if(!isset($_SESSION['secured']))
{    
echo "<form method='post'>
Username: <input type='text' name='username' maxlength='10' /><br>
Password: <input type='password' name='password' maxlength='10' /><br>
<input type='submit' value='login' />
</form>";
}else{
?>

<html>
<head>
<title>Session Login</title>
</head>
<body>
<p>redirecting....
<meta HTTP-EQUIV="REFRESH" content="1; url=http://x.php">
</p>
</body>
</html>

<?php
}
?>

【问题讨论】:

  • 您是否将 cookie (setcookie()) 存储在任何地方?
  • @Luceos 没有。我使用的代码与上面完全相同。
  • 您使用的是哪个浏览器,当您关闭浏览器和选项卡时是否会出现这种“继续”行为?
  • @Luceos 我正在使用 Firefox。我实施了韦恩斯建议 unset($_SESSION['secured']);它正在发挥作用。
  • 显然你的会话在标签/浏览器关闭后保持活动状态。所以这是“预期的”行为。

标签: php session redirect cookies logout


【解决方案1】:

如果你可以创建一个 logout.php 页面来破坏会话:

unset($_SESSION['secured']);
header('Location: login.php');
exit;

只需访问该页面,登录就会被销毁。

如果您希望会话在预定时间段后超时,您可以使用类似于in this example 显示的代码。

如果您想在用户登陆 x.php 后终止会话

<?php
session_start();

//First make sure that they're allowed access to x.php
if(!isset($_SESSION['secured'])){
    //They shouldn't be here.
    header('Location: login.php'); //Redirect back to your login page
    exit;
}

//Ok, user is obviously logged in. Unset the session variable so that they can only view this page once (unless they login again)
unset($_SESSION['secured']);

//Show content of x.php

【讨论】:

  • wayne,如果我可以在重定向后完成会话,我更喜欢它。这怎么可能?
猜你喜欢
  • 1970-01-01
  • 2013-02-10
  • 2012-03-01
  • 1970-01-01
  • 2016-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多