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;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 Macbookpro 连接热点正常上网,连接不了Wi-Fi。
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题