gpt4 book ai didi

php - 帮助处理 PHP Cookie 和一些基本身份验证

转载 作者:行者123 更新时间:2023-11-28 00:25:03 24 4
gpt4 key购买 nike

我正在尝试使用一些非常基本的身份验证...基本上,我想让我的网站上线,但我有一个密码可以发送给几个人,这样他们就可以访问它,而不是其他任何人。

我一直在尝试使用 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.');
}
}
}

关于php - 帮助处理 PHP Cookie 和一些基本身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6471471/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com