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 latex投稿显示click download
  • ¥15 请问读取环境变量文件失败是什么原因?
  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?