我正在尝试为我的类制作登录验证页面,这是我为页面LoginDataModel.php提供的代码。将用户给出的值与ini文件中的值进行比较
<?php
//define a constant variable for fxUsers.ini
define('FX_LOGIN_INI_FILE', 'fxUsers.ini');
class LoginDataModel {
private $ini_array;
//construct class will read and store an associative array
public function __construct() {
$this->ini_array = parse_ini_file(FX_LOGIN_INI_FILE);
}
//validateUser function will compare the username and password
//given by the user to the values stured in the ini file.
public function validateUser($username, $password){
if(in_array($username,$this->ini_array) && in_array($password,$this->ini_array)){
return TRUE;
} else {
return FALSE;
}
}
}
?>
一旦用户通过他的凭证,这段代码将在我的login.php页面中被调用。如果用户凭证不匹配,他将被重新路由回登录页面以再次尝试。登录页面的代码是
<?PHP
//check for key to see if this is the first time loading the page
if (empty($_POST['txtUser'])){
$user = '';
$pass = '';
} else {
$user = $_POST['txtUser'];
$pass = $_POST['txtPassword'];
}
//call method from a different file
require_once ('LoginDataModel.php');
$LoginDataModel = new LoginDataModel();
$control = $LoginDataModel->validateUser($user, $pass);
//if user and password match, continue to next file and exit current file
if($control === TRUE){
include 'fxCalc.php';
exit();
}
?>
虽然我相信拥有一切设置,我唯一需要的是怎样的值和用户之间的值进行比较的ini文件。任何帮助,将不胜感激
编辑
我应该有mentione我的ini文件将只是
[section]
admin = pass
EDIT 2 我的代码反映我做了感谢变化这篇文章的支持以及回顾我的课本。我现在的问题是,当我传递用户并通过文件时,即使字符串完全匹配,它仍会返回false。
会在你的ini文件是什么,是不是php.ini的内容或别的东西 –
看到上面的编辑,它被称为fxUsers.ini –