php - pdo连接在php函数中不起作用

标签 php mysql function pdo throttling

我有这个用于限制登录的函数,但我有问题,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;
                }
            }
        }
    }
}
?>

最佳答案

您可以尝试让您的检查函数接收如下参数:

<?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);

希望这有帮助...

关于php - pdo连接在php函数中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36935883/

相关文章:

php - 不再有 PHP 5.3.x 的 VC6 版本?

mysql - 在共享主机中导入 MySQL 数据库

javascript - 点击 jQuery 不工作 FireFox - 可能是 event.preventdefault

python - 从函数访问模块变量是否被认为是不好的做法?

无法理解为什么队列函数程序第一次工作但之后不按预期工作

php - 从 HTTP GET 接收数据并将其发送到网页

javascript - 每 X 秒使用 mysql 数据更新 google java map

php - 合并包含共享关联键的数组而不相互覆盖

c# - EF6 与 MySQL - 无法添加新行

MYSQL如何在一条语句中选择总和列值和特定行条目?