I have a function that I found that was wrote to use PDO, I modified it to use codeigniters active record db class. Everything work EXCEPT when I place the code within a function like so:
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;
}
}
}
using it like this:
$max_attempts = 3;
if(login_attempt_count() <= $max_attempts) {
echo 'login';
}else{
echo 'To many attempts';
}
or this:
$a = login_attempt_count();
Causes the rest of the page to not load. So this indicates an error.
Again if I use the code within the function, outside the function, it works as expected.
Or if there is a completely better and more secure method that I should be using I am open for suggestions. Thanks!
Ok here I am answering my own question. 😉 The function works fine! However, testing it in a view causes an error? After placing the function within my model and calling the function within my controller works great. Using it like so: