PHP login from text file

前端 未结 1 1968
野趣味
野趣味 2021-02-10 18:23

Hi I have looked at other posts about this but they are 2 or more years old so I thought it was better to start fresh.

As the title suggests I am trying to make a login

1条回答
  •  醉梦人生
    2021-02-10 19:13

    Lets say your text file looks something like this:

    pete|petepass|pete@somesite.com|Pete Enterprizes
    john|johnpass|john@somedomain.com|John Corporation
    

    Your code can read something like this:

    $userN = $_POST['user-name'];
    $passW = $_POST['pass-word'];
    $userlist = file ('users.txt');
    
    $email = "";
    $company = "";
    
    $success = false;
    foreach ($userlist as $user) {
        $user_details = explode('|', $user);
        if ($user_details[0] == $userN && $user_details[1] == $passW) {
            $success = true;
            $email = $user_details[2];
            $company = $user_details[3];
            break;
        }
    }
    
    if ($success) {
        echo "
    Hi $userN you have been logged in.
    "; } else { echo "
    You have entered the wrong username or password. Please try again.
    "; }

    0 讨论(0)
提交回复
热议问题