douxuanwei1980 2016-04-07 17:49
浏览 45
已采纳

使用php验证txt文件中是否已存在给定的用户名

I have an assignment where i need to create a register page and verify if a user doesn't exist already in a txt file. I found some answers online and tried to apply them but doesn't seem to work for me. everything works except the part that he needs to verify. here is my code: this is my code

<?php
if($_POST['formSubmit'] == "Submit")
{
    $errorMessage ="";
    $link_Create = "CreateAnAccount.php";
    $Username = $_POST['userName'];
    $password = $_POST['Password'];

    if(empty($_POST['userName']))
    {
        echo "<li>You forget to enter a UserName</li><a href='".$link_Create."'>Start again</a>";
        header("Location:PopUp.html");
        exit;
    }

    if(empty($_POST['Password']))
    {
        echo "<li>You forget to enter a Password</li><a href='".$link_Create."'>Start again</a>";
        header("Location:PopUp.html");
        exit;
    }
    if(!preg_match('/^(?=.*\d)(?=.*[A-Za-z])[0-9A-Za-z!@#$%]{4,24}$/', $password))
    {
        echo "<li>Your Password input was wrong, please notice Paswword most be at least 4 characters long, have at least one letter and at least one digit. </li><a href='".$link_Create."'>Start again</a>";
        header("Location:PopUp.html");
        exit;
    }

    if(empty($errorMessage)){
        $userlist = fopen("login.txt","r");
        $success = true;
        foreach ($userlist as $user) {
            $user_details = explode('|', $user);
            if ($user_details[0] == $Username) {
                $success = false;
                break;
            }
        }

        fclose($userlist);
        if ($success==true) {
            $writer = fopen("login.txt", "a") or die("Unable to open file.");
            fwrite($writer,$Username."|");
            fwrite($writer,$password."
");
            fclose($writer);

            echo "<br>You have been logged in. <br>";
            header("Location:PopUp.html");
            exit;
        }
        else {
            echo "<li>This User Name already exist!</li><a href='".$link_Create."'>Start again</a>";
            header("Location:PopUp.html");
            exit;
        }
    }
}
?>

Let me know if there is the need of my html code but i don't think it has any effect on the issue. Thank you very much I appreciate the help.

  • 写回答

1条回答 默认 最新

  • douyan1921 2016-04-07 18:04
    关注

    You have problem with iterating over resource which return fopen function

    $userlist = fopen("login.txt","r");
    $success = true;
    foreach ($userlist as $user) {
        $user_details = explode('|', $user);
        if ($user_details[0] == $Username) {
            $success = false;
            break;
        }
    }
    

    Try to replace it with this iterating over each line of userlist file:

    $file = new \SplFileObject("login.txt");
    // Loop until we reach the end of the file
    while (!$file->eof()) {
        $user = $file->fgets();
        $user_details = explode('|', $user);
        if ($user_details[0] == $Username) {
            $success = false;
            break;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 (SQL语句|查询结果翻了4倍)
  • ¥15 Odoo17操作下面代码的模块时出现没有'读取'来访问
  • ¥50 .net core 并发调用接口问题
  • ¥15 网上各种方法试过了,pip还是无法使用
  • ¥15 用verilog实现tanh函数和softplus函数
  • ¥15 Hadoop集群部署启动Hadoop时碰到问题
  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 QTableWidget重绘程序崩溃
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊