Welcome Guest, Not a member yet? Register   Sign In
Login Attempts - Codeigniter - Not Working
#1

I want to workout a function such that unsuccessful login attempts of user are capped at 3 consecutive failed login attempts, then serve them a message to that effect. It is immediately executing this line:


Code:
if ($isBlocked) {
    $this->form_validation->set_message('check_user', 'Account is temporarily blocked.');


Somethings wrong on my code. Thanks in advance for the help.


Controller

Code:
<?php

class Account_login extends CI_Controller
{
  public function __construct()
  {
    parent::__construct();
  }

  public function index()
  {
    $data['title'] = 'Account Login';
    $this->load->view('account_login', $data);
  }

  public function verify()
  {
    $this->form_validation->set_rules('acc_username', 'Username', 'required');
    $this->form_validation->set_rules('acc_password', 'Password', 'required|callback_check_user');

    if ($this->form_validation->run() === TRUE) {
      echo 'Success';
    } else {
      $this->index();
    }
  }

  public function check_user()
  {
    $username = $this->input->post('acc_username');
    $password = $this->input->post('acc_password');


    $this->load->model('account_login_model');
    $login = $this->account_login_model->login($username, $password);


    if ($login) {
      return true;
    } else {
      if (isset($_SESSION['error_count'][$username])) {
        $_SESSION['error_count'][$username] += 1;
      } else {
        $_SESSION['error_count'][$username] = 1;
      }

      $isBlocked = $this->account_login_model->isBlocked($username);
      if ($isBlocked) {
        $this->form_validation->set_message('check_user', 'Account is temporarily blocked.');
      } else if (isset($_SESSION['error_count'][$username]) && $_SESSION['error_count'][$username] > 2) {
        $this->account_login_model->block($username);
        $this->form_validation->set_message('check_user', '3 consecutive failed login attempts. Account Blocked.');
      } else {
        $this->form_validation->set_message('check_user', 'Invalid Username/Password');
      }
      return false;
    }
  }
}
Model

Code:
    <?php
class account_login_model extends CI_Model
{
  public function __construct()
  {
    parent::__construct();
    $this->load->database();
  }

  public function login($username, $password)
  {
    $condition_array = array(
      'acc_username' => $username,
      'acc_password' => $password
    );
    $rs = $this->db->get_where('accounts', $condition_array);

    return $rs->row_array() ?: false;
   
  }

  public function isBlocked($username)
  {
    $condition_array = array(
      'acc_username' => $username,
      'acc_isBlocked' => 1
    );
    $rs = $this->db->get_where('accounts', $condition_array);
    $row_count = count($condition_array);

    if ($row_count > 0) {
      return true;
    } else {
      return FALSE;
    }
  }

  public function block($username)
  {
    $this->load->library('email');

    $email = $this->account_lookup($username, 'acc_email');

    $this->email->from('[email protected]', 'Yahoo.com');
    $this->email->to($email);
    $this->email->subject('Account Blocked');

    $message = $this->load->view('account_blocked', null, TRUE);

    $this->email->message($message);
    $this->email->send();

    $this->db->where('acc_username', $username);
    return $this->db->update('accounts', array('acc_isBlocked' => 1));
  }

  public function account_lookup($username, $return)
  {
    $rs = $this->db->get_where('account', array('acc_username' => $username));
    $row = $rs->row();
    return $row->$return;
  }
}
Reply
#2

PHP Code:
if (isset($_SESSION['error_count'][$username]) && $_SESSION['error_count'][$username] < 3) { 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(09-24-2020, 02:01 AM)InsiteFX Wrote:
PHP Code:
if (isset($_SESSION['error_count'][$username]) && $_SESSION['error_count'][$username] < 3) { 
Thanks a lot Smile
Reply




Theme © iAndrew 2016 - Forum software by © MyBB