分解换行符不起作用,只能读取第一行

Explode newline character not working, can only read the first line

所以我做了一个登录页面和一个注册页面。在注册页面中,您键入一个输入,然后它将输入存储在将在登录页面中使用的 .txt 文件中。

问题是,登录页面只能从 .txt 文件中读取第一行。所以如果你创建了 2 个帐户,你只能从第一个(顶部)登录。第二个以后的账号无法使用

这里是注册函数

    private function _regsave()
{
    if (isset($_POST['register'])) {
        $nisreg = $_POST['nisreg'];
        $namareg = $_POST['namereg'];
        $passwordreg = $_POST['passwordreg'];
        $text = $nisreg . "," . $namareg . "," . $passwordreg . "\n";
        $data = "#/config.txt";
        $dirname = dirname($data);

        if (!is_dir($dirname)) {
            mkdir($dirname, 0755, true);
        }

        $fp = fopen($data, 'a+');
        if (fwrite($fp, $text)) {
            $this->session->set_flashdata('message', '<div class="alert alert-success" role="alert">
            Congratulations! Your account has been created. You can now log in.</div>');
            redirect('Auth');
        }
    }
}

这是登录功能:

private function _login()
{
    if (isset($_POST['login'])) {
        $data = file_get_contents("#/config.txt");
        $contents = explode("\n", $data);

        foreach ($contents as $values) {
            $login = explode(",", $values);
            $nis = $login[0];
            $name = $login[1];
            $password = $login[2];

            if ($nis == $_POST['nis']) {

                if ($password == $_POST['password']) {
                    $data = [
                        'nis' => $nis,
                        'name' => $name
                    ];
                    $this->session->set_userdata($data);
                    redirect('User');
                } else {
                    $this->session->set_flashdata('message', '<div class="alert alert-danger" role="alert">Wrong password!</div>');
                    redirect('Auth');
                }
            } else {
                $this->session->set_flashdata('message', '<div class="alert alert-danger" role="alert">Account is not registered!</div>');
                redirect('Auth');
            }
        }
    }
}

每次我尝试使用第二个帐户登录时,它总是说该帐户未注册,即使 .txt 文件包含所有数据。

我哪里错了?

您的 else 语句(帐户未注册)为真,因为对于 txt 文件的第一次迭代,$nis 不等于 $_POST['nis']。

将 else 条件(帐户未注册)移出 foreach 循环。

if (isset($_POST['login'])) {
$data = file_get_contents("#/config.txt");
$contents = explode("\n", $data);

foreach ($contents as $values) {
    $login = explode(",", $values);
    $nis = $login[0];
    $name = $login[1];
    $password = $login[2];

    if ($nis == $_POST['nis']) {

        if ($password == $_POST['password']) {
            $data = [
                'nis' => $nis,
                'name' => $name
            ];
            $this->session->set_userdata($data);
            redirect('User');
        } else {
            $this->session->set_flashdata('message', '<div class="alert alert-danger" role="alert">Wrong password!</div>');
            redirect('Auth');
        }
    }
}
$this->session->set_flashdata('message', '<div class="alert alert-danger" role="alert">Account is not registered!</div>');
redirect('Auth');