PHP从文本文件登录

时间:2013-10-06 05:03:47

标签: php login text-files

您好我已经查看过有关此事的其他帖子,但是他们已经有2年或更长时间了,所以我认为最好重新开始。

正如标题所示,我正在尝试使用php创建一个登录页面。用户应该能够登录到特定会员页面。用户名和密码存储在文本文件中(注意这是一个分配,否则我会使用SQL)。我的代码在

之下
  ?php

  echo "Username: <input type=\"text\" name=\user-name\"><br>";
 echo "Password: <input type=\"text\" name=\pass-word\"><br>";
 echo "<input type=\"submit\" value=\"login\" name=\"login\"><br>";

$userN = $_POST['user-name'];
$passW = $_POST['pass-word'];
$userlist = file ('users.txt');
$checkUser =$userlist[0];

if (isset($_POST['login']))
{
if ($userN == $userlist)
{
    echo "<br> Hi $user you have been logged in. <br>";
}
else
{
echo "<br> You have entered the wrong username or password. Please try again. <br>";
}
}
?>
<form action="login.php" method="post">
Username: <input type="text" name="username">
<br />
Password: <input type="password" nme="pass"
<br />
<input type="submit" name="submitlogin" value="Login">

我知道我需要使用explode功能,我需要定义文本文件的设置方式。理想的用户名|密码。在一个名为users.txt的文件中,用户文件还必须包含诸如电子邮件(可以替换用户名),客户名称,商家名称(客户)和成员特殊价格等信息。

1 个答案:

答案 0 :(得分:1)

让我们说你的文本文件看起来像这样:

pete|petepass|pete@somesite.com|Pete Enterprizes
john|johnpass|john@somedomain.com|John Corporation

您的代码可以是这样的:

$userN = $_POST['user-name'];
$passW = $_POST['pass-word'];
$userlist = file ('users.txt');

$email = "";
$company = "";

$success = false;
foreach ($userlist as $user) {
    $user_details = explode('|', $user);
    if ($user_details[0] == $userN && $user_details[1] == $passW) {
        $success = true;
        $email = $user_details[2];
        $company = $user_details[3];
        break;
    }
}

if ($success) {
    echo "<br> Hi $userN you have been logged in. <br>";
} else {
    echo "<br> You have entered the wrong username or password. Please try again. <br>";
}