php登录/注册到txt数据文件

时间:2016-03-04 19:38:13

标签: php

我有一个简单的项目。在这个项目中,我想创建一个登录页面。 用户可以插入用户名,名称和密码,并向系统注册。如果用户已经注册,系统将发送错误,如果密码不正确,系统也会发送错误。

数据以3行保存到users.txt文件:lin1 = name,line2 = user和line3 = pass

我的代码不起作用,我的注册有问题。

<html>
<head>
    <title>Login Page</title>
</head>
<body align="center">
    <form method="post">
        <br><br><br><br><br><br><br><br><br><br>
        Name:<input type="text" name="name"><br><br>
        Username:<input type="text" name="username"><br><br>
        Password:<input type="password" name="password"><br><br>
        <input type="submit" value="Login">
    </form>

    <?php
        if ($_POST['name'] != null && $_POST['username'] != null && $_POST['password'] != null){
            $a = 0;
            $file = fopen('users.txt', 'r');
            while (!feof($file)) {
                $nam = fgets($file);
                $user = fgets($file);
                $pass = fgets($file);
                if ($nam === $_POST['name'] && $user === $_POST['username'] && $pass === $_POST['password']) {
                    echo "successfull login !";
                    $a = 1;
                    fclose($file);
                }
                else if ($user === $_POST['username'] && $nam !== $_POST['name'] && $pass !== $_POST['password']) {
                    $a = 2;
                    echo "this username already is token!";
                    fclose($file);
                }
                else if ($user === $_POST['username'] && $nam === $_POST['name'] && $pass !== $_POST['password']) {
                    echo "password is incorect";
                    $a = 3;
                    fclose($file);
                }
            }
            fclose($file);
            if ($a == 0){
                $file = fopen('users.txt', 'a+');
                fputs($file, $_POST['name']);
                fputs($file, "\r\n");
                fputs($file, $_POST['username']);
                fputs($file, "\r\n");
                fputs($file, $_POST['password']);
                fputs($file, "\r\n");
                fclose($file);
                echo "register is successfull";
                echo $nam;
                echo $user;
                echo $pass;
            }
        }
    ?>
</body>

0 个答案:

没有答案