我正在尝试使用一些非常基本的身份验证。。。基本上,我想让我的网站上线,但有一个密码,我可以发送给一些人,这样他们就可以访问它,而不是其他人。
我一直在尝试使用cookie,但我对它们相对缺乏经验。下面的代码就是我目前所掌握的。
在我的标题顶部由于某种原因,这不会重定向到authenticate.php
<?php if(!isset($_COOKIE["user"])) header("Location: authenticate.php?invalid=0"); ?>
authenticate.php
<?php
if($_GET["invalid"] == 1) {
echo '<p> Invalid password. </p>';
}
echo '<form action="enter.php" method="post">
<span>Enter the password</span><br />
<textarea name="mainPass"></textarea>
<input type="submit" value="Submit!" />
</form>';
?>
enter.php
<?php
$pass = $_POST['mainPass'];
if(strstr($pass, "<password removed>")) {
setcookie("user", time()+3600);
header("Location: index.php");
} else {
header("Location: authenticate.php?invalid=1 ");
}
?>
快速而肮脏。不要用这个太久。不需要任何形式。将用户名和密码硬编码到函数代码中。您必须在每一页的顶部运行函数,这样才能工作。
// quick and dirty http authentication
function authenticateHttp() {
@session_start();
// credential check
if (!isset($_SESSION['auth_realm']) || !isset($_SERVER['PHP_AUTH_USER'])) {
// credentials not supplied yet
// send http credential request
$_SESSION['auth_realm'] = uniqid('', true);
header('WWW-Authenticate: Basic realm="' . $_SESSION['auth_realm'] . '"');
header('HTTP/1.0 401 Unauthorized');
// if cancel button clicked, show this
exit('You must authenticate before accessing this area.');
} else {
// credentials supplied
if ($_SERVER['PHP_AUTH_USER'] !== '{your chosen username}' || $_SERVER['PHP_AUTH_PW'] !== '{your chosen password}') {
unset($_SESSION['auth_realm']);
exit('You have entered an invalid login.');
}
}
}