简体   繁体   中英

PHP login page using JSON to store user details

How would I get this simple PHP login form to work? Currently when I login with the correct details, I get the alert that I have provided "invalid creds" even though I have. Also, after I dismiss the invalid creds alert, all of a sudden I get: "Notice: Undefined index: username in C:\\xampp\\htdocs\\ClassIA3\\login.php on line 18"

Please help me fix, thanks.

<html>
      <head>
        <title>Lonely Bris</title>
      </head>
      <?php
    session_start();
    session_unset();
    $output = file_get_contents("users.json");
    $decode = json_decode($output, true);
    
    if(isset($_POST['login'])) {
      $username = $_REQUEST['username'];
      $password = sha1($_REQUEST['password']);
    
      for($i = 0; $i < count($decode); $i++) {
        if($decode[$i]['username'] == $username && $decode[$i]['password'] == $password) {
          echo "<script>alert('Login successful!')</script>";
          $_SESSION['username'] = $username;
          header("location url=Events.php");
        
        }
        else {  
              echo "<script>alert('Invalid creds!')</script>";
        }
      }
    
    }
    
    ?>
      <body>
    
        <h3>Login</h3>
    
        <form action="" method="post">
    
          <p>Username: <input type="text" name="username" placeholder="Enter Username"></p>
          <p>Password: <input type="password" name="password" placeholder="Enter Password"></p>
          <p><input type="submit" name="login" value="Login"></p>
    
        </form>
      </body>
    </html>

JSON File (users.json)

    [
        {
            "name": "Hello",
            "password": "40bd001563085fc35165329ea1ff5c5ecbdbbeef"
            "account_type": "local"

        },
        {
            "username": "Hello2",
            "password": "40bd001563085fc35165329ea1ff5c5ecbdbbeef"
            "account_type": "local"
        },
        {
            "username": "Sup",
            "password": "40bd001563085fc35165329ea1ff5c5ecbdbbeef",
            "account_type": "traveler"
        },
        {
            "username": "Pony",
            "password": "40bd001563085fc35165329ea1ff5c5ecbdbbeef",
            "account_type": "local"
        }
    ]

在您的 users.json 文件中,第一条记录具有键而不是用户名,这就是您面临错误的原因注意:未定义索引:用户名

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM