dsfsdf7852 2016-04-29 10:26 采纳率: 100%
浏览 52
已采纳

pdo连接不能在php函数中工作

I have this function which is for throttling login, but I have problem, the pdo connections do not work inside the function, it gives me error for "undefined $conn or call to a member query function on null", if I'm correct it is due to the scope, is there any work around this?

<?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;
                }
            }
        }
    }
}
?>
  • 写回答

2条回答 默认 最新

  • duanci8209 2016-04-29 10:44
    关注

    You could try to make your check Function receive an Argument like so:

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

    Now you can pass the $conn to your Function like so:

    <?php
        check($conn);
    

    Hope this helps...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测