简体   繁体   English

MySQL Query很好,没有给出值或错误

[英]MySQL Query is fine, doesn't give a value or an error

I am trying to find how to check if a variable called active is equal to 1. My attempt at the function is below: 我试图找到如何检查一个称为active的变量是否等于1的方法。

function login($email, $password, $mysqli) {
    // Using prepared statements means that SQL injection is not possible. 
    if ($stmt = $mysqli->prepare("SELECT id, username, password, salt, active 
                  FROM members 
                                  WHERE email = ? LIMIT 1")) {
        $stmt->bind_param('s', $email);  // Bind "$email" to parameter.
        $stmt->execute();    // Execute the prepared query.
        $stmt->store_result();

        // get variables from result.
        $stmt->bind_result($user_id, $username, $db_password, $salt, $active);
        $stmt->fetch();

        // hash the password with the unique salt.
        $password = hash('sha512', $password . $salt);
        if ($stmt->num_rows == 1) {

            // If the user exists we check if the account is locked
            // from too many login attempts 
            if (checkbrute($user_id, $mysqli) == true) {
                // Account is locked 
                // Send an email to user saying their account is locked 
                return false;
            } else {
                // Check if the password in the database matches 
                // the password the user submitted.
                if ($db_password == $password) {
                    // Password is correct!
                    // Get the user-agent string of the user.
                    $user_browser = $_SERVER['HTTP_USER_AGENT'];

                    // XSS protection as we might print this value
                    $user_id = preg_replace("/[^0-9]+/", "", $user_id);
                    $_SESSION['user_id'] = $user_id;

                    // XSS protection as we might print this value
                    $username = preg_replace("/[^a-zA-Z0-9_\-]+/", "", $username);

                    $_SESSION['username'] = $username;
                    $_SESSION['login_string'] = hash('sha512', $password . $user_browser);
                } else {
                    // Password is not correct 
                    // We record this attempt in the database 
                    $now = time();
                    if (!$mysqli->query("INSERT INTO login_attempts(user_id, time) 
                                    VALUES ('$user_id', '$now')")) {
                        header("Location: ../error?err=Database error: login_attempts");
                        exit();
                    }

                    return false;
                }
            }
        } else {
            // No user exists. 
            return false;
        }
    } else {
        // Could not create a prepared statement
        header("Location: ../error?err=Database error: cannot prepare statement");
        exit();
    }
}

I assume that where I added active to the $mysqli->prepare statement is correct. 我假设在$ mysqli-> prepare语句中添加active的位置是正确的。 What I want to do is if the user got their password correct I would query the MySQL table to see if his account is active(1) or not active(0). 我想做的是,如果用户输入的密码正确,我将查询MySQL表以查看其帐户是active(1)还是非active(0)。 If it is set to 0 it logs in with no error. 如果将其设置为0,则无错误登录。 However in my process_login.php file it logs the user in if it is (0) but with index.php?err=1 但是在我的process_login.php文件中,如果是(0)却使用index.php?err = 1,它将用户登录

<?php

include_once 'db_connect.php';
include_once 'functions.php';

sec_session_start(); // Our custom secure way of starting a PHP session.

if (isset($_POST['email'], $_POST['p'])) {
    $email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL);
    $password = $_POST['p']; // The hashed password.

    if (login($email, $password, $mysqli) == true) {

        // Login success 
        header("Location: ../protected_page.php");

        exit();
    } else {
        // Login failed 
        header('Location: ../index.php?error=1');
        echo $active;
        exit();
    }
} else {
    // The correct POST variables were not sent to this page. 
    header('Location: ../error.php?err=Could not process login');
    exit();
}

When I try to echo the variable $active it returns nothing. 当我尝试回显变量$ active时,它什么也不返回。 Any help is appreciated in advance. 任何帮助都需要提前感谢。

Posting this as a community wiki; 将其发布为社区Wiki; I don't want rep for it, nor should there be any made from it. 我不想代表它,也不应该有任何代表。

A: You did not follow that tutorial exactly as it was written. 答:您没有完全按照编写的教程进行操作。

since it is obvious that that is where that code comes from; 因为很明显那是代码的来源; I know it all too well. 我太清楚了

You modified some parts of the code and left some out also. 您修改了代码的某些部分,还省略了一些。

Go back to the tutorial, and follow it " to a T " . 返回本教程,然后按照至T ”进行操作 You may also have to clear out your present hashes and start over. 您可能还需要清除当前的哈希值并重新开始。

Make sure that the table creation was done exactly as shown. 确保表创建完全按照所示完成。 If you failed to make the right columns and their proper lengths, then that will fail "silently" on you. 如果您无法创建正确的列及其正确的长度,那将对您“无声地”失败。

Consult the comments I left under the question also. 也请参考我在问题下留下的评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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