首页 > 解决方案 > 更新会话销毁数据库

问题描述

首先,我是一名 Laravel 程序员,试图理解这个旧的 codeginiter 系统。我需要避免同一个用户多次登录。我只需要登录一次,没有人可以使用该帐户登录。

我只是设法在 application/controllers/backend/Auth.php 中修改函数 logout()

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');
} 

和 application/models/ion_auth_model.php 中的函数 username_check

/**
  * 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;
    } 

如果表用户中的“ip_address”为空,我只是让用户登录,如果不是,则在 username_check 中阻止(返回 FALSE)。

这有效,但在会话到期时失败(application/controllers/backend/Auth.php logout() 未触发),而是 library/Session/MY_Session sess_destroy() 被触发,但我无法从那里修改数据库:

// 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();
    }

}

// --|

说 $this-->db 为空...

那么,我如何不能在会话到期时修改数据库?

标签: phpcodeigniter

解决方案


看起来您正在使用 ion_auth 脚本...尝试将您的脚本放在注销功能下的 ion auth 库中...这取决于您如何安装 ion_auth 脚本..如果您将其安装为第三方应用程序,那么您可以找到它在第三方文件夹中...查找库文件夹,而不是在其中找到 ion auth 库...打开它并找到注销功能,然后将脚本放在调用 sess_destroy() 之前


推荐阅读