我如何编辑下面的PHP以允许额外或多个用户名和密码。例如User1的密码为JHFDE3,User2的密码为LKFW34等。从我读过的内容来看,一个关联数组可能是最好的。没有数据库,它是硬编码,只需登录并能够查看一个PHP页面。PHP用户名/密码登录
对于下面的特定代码,我将如何使它成为一个关联数组?谢谢。
<?php
session_start(); //initiates the sessions
if ($_POST['submit']) //checks to make sure the login form has been submitted
{
$user = "XXXXX";
$pass = "XXXXXXX";
if ($_POST['pass'] == $pass && $_POST['user'] == $user) //checks if the password submitted by the user matches the password stored in the $pass variable
{
$_SESSION['access'] = 1; //if login is successful create a session that can be authenticated
header("Location: " . $_SERVER['PHP_SELF']); //reload the page. The page will now not load the login form (see the first if condition)
} else //if password is incorrect reload the login form
{
header("Location: " . $_SERVER['PHP_SELF']);
}
} else if (!$_SESSION['access']) //if the "access" session is not accessible show the form (not logged in)
{
?>
因此该代码可以使用单个用户...但它的工作代码,对不对? – Hackerman
一般来说,我们欢迎提出问题的问题,首先您可以在PHP手册网站上搜索“关联数组”。你也需要一个'foreach'。这就是说,你真的应该使用一个数据库 - 也许http://usercake.com/会被使用?我没有用过它,但似乎在这里提到了一点点。 – halfer
如果这不是学生作业,硬编码密码是一个坏主意。 即使它是一项任务,它也正在设置坏习惯。 – Steve