duanqiechui2378 2013-10-06 05:03
浏览 80

PHP从文本文件登录

Hi I have looked at other posts about this but they are 2 or more years old so I thought it was better to start fresh.

As the title suggests I am trying to make a login page with php. Users should be able to login to a special member only page. The usernames and passwords are stored in a textfile (note this is for an assignment otherwise I'd use SQL). My code is below

  ?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">

I know I need to use the explode function and I need to define how the text file will be set out. ideally username|password. in a file called users.txt The users file also has to contain information such as the email (can replace username) the customers name, the business name (of the customer) and special prices for members.

  • 写回答

1条回答 默认 最新

  • douyi1197 2014-01-29 21:15
    关注

    Lets say your text file looks something like this:

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

    Your code can read something like this:

    $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>";
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答