【问题标题】:update database on session destroy在会话销毁时更新数据库
【发布时间】:2020-01-20 13:16:33
【问题描述】:

我需要在会话到期(或关闭)时更新表,但此代码显示 $this->dbnull? 我如何在会话销毁中更新数据库??

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

}

【问题讨论】:

    标签: php codeigniter session continuous-integration


    【解决方案1】:

    Session 类中似乎没有 DB 类。您可能需要通过 setter 方法注入一个

    https://github.com/bcit-ci/CodeIgniter/blob/develop/system/libraries/Session/Session.php

    或者,如果您使用任何类型的观察者模式,请添加一个事件侦听器并从这里触发事件。

    【讨论】:

      【解决方案2】:

      试试下面的代码

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

      【讨论】:

        猜你喜欢
        • 2016-11-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-25
        • 1970-01-01
        相关资源
        最近更新 更多