我运行此代码来保护我的网站,但我想知道如何在没有数据库的情况下使用多个用户。我读到你可以用一个文本文件来做,但不知道怎么做,我对 PHP 的概念很少。
谢谢您的帮助。
<?php
session_start();
$username = "test";
$password = "test";
if(isset($_GET['logout']))
{
unset($_SESSION["login"]);
echo "You have logout ... ";
echo "[<a href='" . $_SERVER['PHP_SELF'] . "'>Login</a>]";
exit;
}
if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW']) || !isset($_SESSION["login"]))
{
header("WWW-Authenticate: Basic realm=\"Ghost SMS\"");
header("HTTP/1.0 401 Unauthorized");
$_SESSION["login"] = true;
echo "You are unauthorized ... ";
echo "[<a href='" . $_SERVER['PHP_SELF'] . "'>Login</a>]";
exit;
}
else
{
if($_SERVER['PHP_AUTH_USER'] == $username && $_SERVER['PHP_AUTH_PW'] == $password)
{
echo "Welcome ";
echo "[<a href='" . $_SERVER['PHP_SELF'] . "?logout'>Logout</a>]";
}
else
{
unset($_SESSION["login"]);
header("Location: " . $_SERVER['PHP_SELF']);
}
}
// content here
?>