PHP使用txt文件登录

时间:2015-11-12 17:25:37

标签: php html login

你好我正在开发一个项目,我必须使用文本文件创建一个php登录。我已经列出了基本代码但是当我在文件中输入用户名和密码并尝试登录时,它不起作用。我真的可以使用一些建议。谢谢。下面的代码是我的登录php文件。

<?php
session_start();
$User = $_GET["user"];
$Pass = $_GET["password"];

if (!strpos($User,"@")) {
   $User = $User . "@etown.edu";
}
$Validuser = false;
$_SESSION["user"] = $User;
$_SESSION["pass"] = $Pass;
$_SESSION["login"]= $Validuser;


?>
<!DOCTYPE html>
<html>

<body>

<?php
    print "<h2>Welcome $User</h2>";

$infile = fopen("account.txt","r");
      $entry = fgets($infile);
         while (!feof($infile)) {
        $array = explode(" ",$entry);
            if ($array[0] == $User){
           $name = $array[0];
           $code = $array[1];
           $code = substr($code,0,strlen($code)-1);
           }
            $entry = fgets($infile);
    }
print "Name: $name <br/>";
print "pass on file: $code <br />";

fclose($infile);
    if ($name==$User && $code==$Pass)
       $Validuser = true;
    $_SESSION["login"] = $Validuser;

    print "That's All!<br/>";

    if ($Validuser) {
        print "Welcome valid user<br/>";
}
    else {
         print "You are not a valid user.  Go become one first!";
         print '<script type="text/javascript">';
         print '   //document.location = "register.html";';
         print '</script>';

}

?>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

尝试添加此

    $User = isset($_GET["user"]) ? $_GET["user"] : '';
    $Pass = isset($_GET["user"]) ? $_GET["password"] : '';
    $name ='';
    $code='';

我假设您的txt文件是这样的

ja@etown.edu 12345
ja2@etown.edu 12345
ja3@etown.edu 12345