-1

我想用文本文件构建一个超级简单的登录脚本。文本文件包含

password    username    name

密码、用户名和名称之间是一个选项卡。我阅读了该文件,按选项卡将其分解,然后根据行检查用户输入。

但我总是得到(一个)Undefined offset错误。我认为是因为爆炸功能,但我不知道为什么......

这是我的代码:

if(!empty($_POST['login_inputEmail']) || !empty($_POST['login_inputPassword']))
{
    $log = 0; //not logged in
    $username = $_POST['login_inputEmail'];
    $password = $_POST['login_inputPassword'];
    $userdatei = fopen ("documents/user.txt","r");
    while (!feof($userdatei))
       {
       $zeile = fgets($userdatei,800);
       $userdata = explode("\t", $zeile);
       if ($username == $userdata[1] && $password == trim($userdata[0]))
          {
          $log = 1; //logged in
          }
       }
    fclose($userdatei);
}
4

1 回答 1

0

在代码中添加 is_array() 和 isset() 以避免错误。参考这个

 if(!empty($_POST['login_inputEmail']) || !empty($_POST['login_inputPassword']))
 {
     $log = 0; //not logged in
     $username = $_POST['login_inputEmail'];
     $password = $_POST['login_inputPassword'];
     $userdatei = fopen ("documents/user.txt","r");
     while (!feof($userdatei))
     {
        $zeile = fgets($userdatei,800);
        $userdata = explode("\t", $zeile);
        if(is_array($userdata))
        {
           if(isset($userdata[1]) && isset($userdata[0]))
           {
              if ($username == $userdata[1] && $password == trim($userdata[0]))
              {
                 $log = 1; //logged in
              }
           }
        }
     }
    fclose($userdatei);
 }
于 2013-09-05T08:58:28.773 回答