php函数中的pdo连接不起作用


pdo connection not working in php function

我有这个函数,它是用来限制登录的,但我有问题,pdo连接在函数内不起作用,它给了我"未定义$conn或在null上调用成员查询函数"的错误,如果我是正确的,那是由于作用域的原因,有什么解决办法吗?

<?php
function check(){
    function get_multiple_rows($getfailed) {
        $rows = array();
        while($row = $getfailed->fetch(PDO::FETCH_ASSOC)) {
            $rows[] = $row;
        }
        return $rows;
    }
    $throttle = array(1 => 1, 5 => 2, 30 => 10);
    if ($getfailed = $conn->query("SELECT MAX(attempted) AS attempted FROM failed_logins")){
        $rows = get_multiple_rows($getfailed);
        $getfailed->closeCursor();
        $latest_attempt = (int) date('U', strtotime($rows[0]['attempted']));
        if ($getfailed = $conn->query("SELECT COUNT(1) AS failed FROM failed_logins WHERE attempted > DATE_SUB(NOW(), INTERVAL 15 minute)")){
            $rows = get_multiple_rows($getfailed);
            $getfailed->closeCursor();
            $failed_attempts = (int) $rows[0]['failed'];
            krsort($throttle);
            foreach ($throttle as $attempts => $delay){
                if ($failed_attempts > $attempts) {
                    $remaining_delay = (time() - $latest_attempt) - $delay;
                    if ($remaining_delay < 0){
                        echo "You have exceeded the login attempts limit";
                    }
                    return false;
                    break;
                }else{
                    return true;
                }
            }
        }
    }
}
?>

您可以尝试让您的check函数接收一个这样的参数:

<?php 
    function check(PDO $conn){
        function get_multiple_rows(PDOStatement $getfailed) {
            $rows = array();
            while($row = $getfailed->fetch(PDO::FETCH_ASSOC)) {
                $rows[] = $row;
            }
            return $rows;
        }
        $throttle = array(1 => 1, 5 => 2, 30 => 10);
        if ($getfailed = $conn->query("SELECT MAX(attempted) AS attempted FROM failed_logins")){
            $rows = get_multiple_rows($getfailed);
            $getfailed->closeCursor();
            $latest_attempt = (int) date('U', strtotime($rows[0]['attempted']));
            if ($getfailed = $conn->query("SELECT COUNT(1) AS failed FROM failed_logins WHERE attempted > DATE_SUB(NOW(), INTERVAL 15 minute)")){
                $rows = get_multiple_rows($getfailed);
                $getfailed->closeCursor();
                $failed_attempts = (int) $rows[0]['failed'];
                krsort($throttle);
                foreach ($throttle as $attempts => $delay){
                    if ($failed_attempts > $attempts) {
                        $remaining_delay = (time() - $latest_attempt) - $delay;
                        if ($remaining_delay < 0){
                            echo "You have exceeded the login attempts limit";
                        }
                        return false;
                        break;
                    }else{
                        return true;
                    }
                }
            }
        }
    }

现在您可以将$conn传递给您的函数,如下所示:

<?php
    check($conn);

希望这能帮助。。。

显然,您尚未在函数中定义$conn。您必须在本地函数中定义$conn,以便能够通过它发送请求,或者考虑阅读此答案,以便在web应用程序中全局正确设置PDO连接。