我想将其设置为如果有人发送“注销”请求,它会自动将他们带到一个显示“成功注销”的页面。如果客户尝试按后退按钮或进入受限区域,它将再次要求 HTTP 身份验证。
到目前为止我所拥有的是这样的:
example.com/restricted/index.php:
<?php
session_start();
if(isset($_GET['logout']))
{
unset($_SESSION["login"]);
header("location: ../logout.php");
exit;
}
if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW']) || !isset($_SESSION["login"]))
{
header("HTTP/1.0 401 Unauthorized");
header("WWW-authenticate: Basic realm=\"Tets\"");
header("Content-type: text/html");
$_SESSION["login"] = true;
// Print HTML that a password is required
exit;
}
?>
// The rest of the page is then displayed like normal
用户成功访问example.com/logout.php if example.com/restricted/index.php?logout被访问。当用户尝试返回时,无论发生什么随机的事情,有时它会要求两次 HTTP 身份验证(???),有时它会不断地循环请求身份验证(?),有时它会让我直接返回,好像我从来没有退出过。
我对会话的工作方式很陌生,但我的理解是这样的:如果/当该人被验证时,它会在其会话中存储一个名为登录的变量,其值为 true...如果它每次收到带有注销的 GET 请求,它将然后删除该会话变量并返回 logout.php...为什么当我单击返回索引时,当会话[login] 未设置时,它会让我返回而不要求身份验证。
对此 PHP 代码的任何改进都值得赞赏。我知道我不应该使用 HTTP Basic 而应该合并 SQL,但是呃。这是一个临时解决方案。
编辑:如果包含带有说明的示例,我将接受 MySQL 的解决方案。我还没有 MySQL 或 PHP 数据库知识
一个粗略的想法让你开始:
<?php
session_start();
if( isset( $_GET['logout'] ) )
{
session_destroy();
header('Location: ../logout.php');
exit;
}
if( !isset( $_SESSION['login'] ) )
{
if( !isset( $_SERVER['PHP_AUTH_USER'] ) || !isset( $_SERVER['PHP_AUTH_PW'] ) )
{
header("HTTP/1.0 401 Unauthorized");
header("WWW-authenticate: Basic realm=\"Tets\"");
header("Content-type: text/html");
// Print HTML that a password is required
exit;
}
else
{
// Validate the $_SERVER['PHP_AUTH_USER'] & $_SERVER['PHP_AUTH_PW']
if( $_SERVER['PHP_AUTH_USER']!='TheUsername'
|| $_SERVER['PHP_AUTH_PW']!='ThePassword' )
{
// Invalid: 401 Error & Exit
header("HTTP/1.0 401 Unauthorized");
header("WWW-authenticate: Basic realm=\"Tets\"");
header("Content-type: text/html");
// Print HTML that a username or password is not valid
exit;
}
else
{
// Valid
$_SESSION['login']=true;
}
}
}
?>
// The rest of the page is then displayed like normal
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)