我发现一个函数是为使用 PDO 而编写的,我将其修改为使用 codeigniters 活动记录数据库类。除非我将代码放在这样的函数中,否则一切正常:
function login_attempt_count() {
$seconds = 10;
// First we delete old attempts from the table
$oldest1 = strtotime(date('Y-m-d H:i:s').' - '.$seconds.' seconds');
$oldest2 = date('Y-m-d H:i:s',$oldest1);
$del_data = $oldest2;
$this->db->where('when <', $del_data);
$this->db->delete('Login_Attempts');
// Next we insert this attempt into the table
$data = array(
'ip' => $_SERVER['REMOTE_ADDR'],
'when' => date("Y-m-d H:i:s"));
$this->db->insert('Login_Attempts', $data);
// Finally we count the number of recent attempts from this ip address
$count = 'SELECT count(*) as number FROM Login_Attempts WHERE ip = ?';
$num = $this->db->query($count, $_SERVER['REMOTE_ADDR']);
if ($num->num_rows() > 0){
foreach($num->result() as $attempt){
$attempts = $attempt->number;
return $attempts;
}
}
}
像这样使用它:
$max_attempts = 3;
if(login_attempt_count() <= $max_attempts) {
echo 'login';
}else{
echo 'To many attempts';
}
或这个:
$a = login_attempt_count();
导致页面的其余部分无法加载。所以这表明一个错误。同样,如果我在函数内、函数外使用代码,它会按预期工作。
或者,如果我应该使用一种完全更好、更安全的方法,我愿意接受建议。谢谢!