我需要帮助使用PHP登录基于数组的登录

问题描述 投票:-2回答:1

继承人我在我的小网站上使用的代码只是为了我自己,但我需要帮助添加数组中的用户,因为我不想使用mysql继承人我的意思的一个例子...

ex) $user = array("lol", "lol1", "lol2", "lol3");
ex2) $pass = array("lol", "lol1", "lol2", "lol3");

<?php

// Start the session
session_start();

// Defines username and password. Retrieve however you like,
$user = "lol";
$pass = "lol";

// Error message
$error = "";

// Checks to see if the user is already logged in. If so, refirect to correct page.
if (isset($_SESSION['loggedIn']) && $_SESSION['loggedIn'] == true) {
    $error = "success";
    header('Location: success.php');
} 

// Checks to see if the username and password have been entered.
// If so and are equal to the username and password defined above, log them in.
if (isset($_POST['username']) && isset($_POST['password'])) {
    if ($_POST['username'] == $user && $_POST['password'] == $pass) {
        $_SESSION['loggedIn'] = true;
        header('Location: success.php');
    } else {
        $_SESSION['loggedIn'] = false;
        $error = "Invalid username and password!";
    }
}
?>
php
1个回答
2
投票

首先,您需要检查发布的用户名是否在$ user数组中。如果它可用,从$ user数组中获取密钥并将$ pass数组中的密码与密钥进行比较。

if (isset($_POST['username']) && isset($_POST['password'])) {
    // get key of posted username from $user array, will be false if not available  
    if(($key = array_search($_POST['username'], $username)) !== NULL){ 
        // now check the password
      if($_POST['password'] == $password[$key]){
         $_SESSION['loggedIn'] = true;
         header('Location: success.php'); 
      }
    }
}

$username = array('user1','user2');
$password = array ('pass1','pass2');

user1 / pass1或user2 / pass2的组合将允许您登录。

© www.soinside.com 2019 - 2024. All rights reserved.