franzag
franzag

Reputation: 17

Update database on session destory

first, I'm a Laravel programmer trying to understand this old codeginiter system. I need to avoid the same user to log in more than once. I juts need to once logged-in nobody can login with that account.

I just managed to do this modifying the function logout() in application/controllers/backend/Auth.php

public function logout() {
    $logout = $this->ion_auth->logout();

    // 20200117 2355 : agrego |--
    $user_x = $this->codegen_model->row('users', '*', "id='" . $this->session->userdata('user_id') . "'");
    if( !is_null($user_x) ){
        $data = array(
        'ip_address' => (binary)''
    );
    $this->codegen_model->edit( 'users', $data, 'id', $user_x->id );
    }
    // --|

    redirect(base_url() . 'web_ctrl', 'refresh');
} 

and the function username_check in application/models/ion_auth_model.php

/**
  * Checks username
  *
  * @return bool
  * @author Mathew
**/
public function username_check($username = '')
{
    $this->trigger_events('username_check');

    if (empty($username))
    {
    return FALSE;
    }

    // 20200117 2355 : agrego |--
    $user_x = $this->codegen_model->row('users', '*', "username='" . $username . "'");
    if (!is_null($user_x)) {
        if( $user_x->ip_address != (binary)'' )
        return FALSE;
    else {
        $data = array(
            'ip_address' => (binary) inet_pton($this->input->ip_address())
        );
        $this->codegen_model->edit('users', $data, 'id', $user_x->id);
    }
    }
    // --|

    $this->trigger_events('extra_where');
    return $this->db->where('username', $username)->count_all_results($this->tables['users']) > 0;
    } 

I just simply left the user to login if "ip_address" in table users is empty and block (return FALSE) in username_check if not.

This works but fails on session expiration (application/controllers/backend/Auth.php logout() is not triggered) instead libraries/Session/MY_Session sess_destroy() is trigger but I can't modify the database from there:

// 20200117 2355 : agrego |--

class MY_Session extends CI_Session{

    public function __construct() {
        parent::__construct();
    }

    function sess_destroy() {

        $data = array(
            'ip_address' => (string) ''
        );
        $this->db->update($this->tables['users'], $data, array('id' => $this->session->userdata('user_id')));

        //call the parent
        parent::sess_destroy();
    }

}

// --|

Says $this-->db is null ...

So, how i cant modify the database on session expiration ?

Upvotes: 0

Views: 59

Answers (1)

its look like you are using ion_auth script...try to put your scrip inside the ion auth library under logout function...it depend on how you install ion_auth script..if you install it as third party app than you can find it in third_party folder...look for library folder than you can find ion auth library in ther...open it and find logout functin an than put your script before it call sess_destroy()

Upvotes: 1

Related Questions