我创建了两个php文件..一个包含所有功能,另一个包含用户名&密码..但是当我输入正确的用户名时,我收到错误。密码。尽管我在表单中输入了正确的密码,但它告诉我错误的组合。请验证我的代码:
文件login.php
include 'init.php';
if(empty($_POST) === false){
$username = $_POST['username'];
$password = $_POST['password'];
if(empty($username) === true || empty($password) === true){
$errors[] = 'You need to enter a username and password';
} else if(user_exists($username) === false){
$errors[] = 'We cannot find that username. Have you registered yet?';
} else if(user_active($username) === false){
$errors[] = 'You have not activated your account yet';
} else{
$login = login($username, $password);
if($login === false){
$errors[] = 'That username or password combination is incorrect';
} else{
echo 'Okay';
}
}
print_r($errors);
}
Users.php
function user_id_from_username($username){
$username = sanitize($username);
return mysql_result(mysql_query("SELECT `user_id` FROM `users` WHERE `username` = '$username'"), 0, 'user_id');
}
function login($username, $password){
$user_id = user_id_from_username($username);
$username = sanitize($username);
$password = md5($password);
return (mysql_result(mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `username` = '$username' AND 'password' = '$password'"), 0) == 1) ? $user_id : false;
}
init.php文件
<?php
session_start();
//error_reporting(0);
require 'database/connect.php';
require 'functions/general.php'; // general contains the senatanize function
require 'functions/users.php';
$errors = array();
?>