1

我正在按照关于 phpacademy 的教程进行登录和注册。我遵循了 Youtube 上的脚本。在第 3 部分中,一切都很顺利,直到函数的最后一部分。如果我输入也在 te 数据库中的用户名和密码,我会收到消息: Array ( [0] => That username/password

有人可以告诉我我做错了什么吗?到目前为止,这是我的登录部分:

<?php 
include 'core/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 can\'t find that username. Have you registered?';
    } else if (user_active($username) === false) {
        $errors[] = 'You haven\'t activated your account!';
    } else {
        $login = login($username, $password);
        if ($login === false) {
        $errors[] = 'That username/password combination is incorrect';

    } else {
        echo 'ok!';

}
}
}
?>

这是我的用户部分(我认为)最后一部分出错了。

<?php
function user_exists($username) {
    $username = sanitize($username);
    return (mysql_result(mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `username` = '$username'"), 0) == 1) ? true : false;
}

function email_exists($email) {
    $email = sanitize($email);
    return (mysql_result(mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `email` = '$email'"), 0) == 1) ? true : false;
}

function user_active($username) {
    $username = sanitize($username);
    return (mysql_result(mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `username` = '$username' AND `active` = 1"), 0) == 1) ? true : false;
}

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 user_id_from_email($email) {
    $email = sanitize($email);
    return mysql_result(mysql_query("SELECT `user_id` FROM `users` WHERE `email` = '$email'"), 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;
}
?>

希望有人可以帮助我。

提前致谢

4

0 回答 0