【发布时间】:2012-02-18 13:51:28
【问题描述】:
我目前正在使用具有登录名(用户名和密码)的站点 - 密码保护由 Web 服务器中的操作系统在操作系统中称为领域的文件夹级别完成。现在必须这样做,直到我们找到合适的 PHP 登录系统。
以下代码基于previous question on the stack overflow.
我正在使用 3 个文件(参见底部的代码 sn-ps)。
过程是: - 单击 index.php 上的登录按钮 - 输入用户名和密码以访问认证索引文件。 - 单击注销按钮,它引用 logout.php 文件 - 它应该清除缓存并将用户返回到顶级索引。
它不会“破坏会话”,因为在提示时不会要求您重新输入密码,这基本上是我想要发生的事情。
我对 php 的了解很少,这让我有点困惑。
index.php(带有登录按钮的顶级文件)
<?php session_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Test</title>
</head>
<body>
<a href="authenticate/index.php">Log In Btn</a>
</body>
</html>
authenticate/index.php(此文件夹受密码保护 - 包含索引文件和链接到 logout.php 文件的注销按钮)
<?php session_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Log out</title>
</head>
<body>
<a href="logout.php">Log Out Btn</a>
</body>
</html>
验证/logout.php
<?php
session_start(); //to ensure you are using same session
session_destroy(); //destroy the session
header("location:/index.php"); //to redirect back to "index.php" after logging out
exit();
?>
【问题讨论】:
-
如果文件夹受密码保护,则 PHP 不会进行身份验证。 Apache(或网络服务器)是。
-
确实有道理,非常感谢@xbonez