pdo 连接在 php 函数中不起作用
pdo connection not working in php function
我有这个用于限制登录的函数,但我有问题,pdo 连接在函数内部不起作用,它给我错误 "undefined $conn or call to a member query function on 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;
}
}
}
}
}
?>
显然您没有在函数中定义 $conn
。您必须在本地函数中定义 $conn
才能通过它发送请求,或者考虑阅读 this answer 以在您的 Web 应用程序中全局正确设置 PDO 连接。
您可以尝试让您的检查函数接收这样的参数:
<?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);
希望这对您有所帮助...
我有这个用于限制登录的函数,但我有问题,pdo 连接在函数内部不起作用,它给我错误 "undefined $conn or call to a member query function on 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;
}
}
}
}
}
?>
显然您没有在函数中定义 $conn
。您必须在本地函数中定义 $conn
才能通过它发送请求,或者考虑阅读 this answer 以在您的 Web 应用程序中全局正确设置 PDO 连接。
您可以尝试让您的检查函数接收这样的参数:
<?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);
希望这对您有所帮助...