I'm still new to PHP and Code Igniter, and developping a project based on a triple store + a small SQLite db.
I want to save some session data in persistent storage when a session is expiring, so that the users could customize the pages and keep their settings and history for new sessions. This seems more practical than using my triple store or db for each user action.
I use code igniter 3.1.5 + Session library + a small SQLite db for session data (followed CI doc for Session library with database driver : https://www.codeigniter.com/user_guide/libraries/sessions.html#database-driver and this post for SQLite settings : How can I create a CRUD with Codeigniter using SQLite?).
I didn't find any solution to retore a closed session (maybe it's possible?), so found that I could either (a) modify CI Session system library or (b) create a custom class that implements SessionHandlerInterface (cf. here : https://secure.php.net/manual/en/function.session-set-save-handler.php ;
and here : How do I save session data to a database instead of in the file system?). Both solutions consist in adding custom code in close()
or destroy()
function.
I'd like to keep Code Igniter Session library, because it's very simple to use and have already written some code for it, but I don't know how to extend or modify it properly.
So here is what I tried, could you please tell me if it's ok?
1) create a file Session_custom_driver.php in system/libraries/Session/drivers where I copy CI_Session_database_driver
code, and modify close()
(or destroy()
) function
2) edit application/config/config.php : $config['sess_driver'] = 'custom';
And next step would be to load a custom library or model from this modified close()
function :
$CI =& get_instance();
$this->CI_Instance =& $CI; <--- add this in __construct()
and
public function close() {
$this->CI_Instance->load->model("MyModelForSessions");
...
}
Thanks in advance.
EDIT : with this solution I can save session data in another place and use it when the session has expired, BUT the function is called every time I modify the data stored in $_SESSION
.
How can I save the data just once, i.e. when the session is expiring?
Another idea : I can save session id with user's persistent data, then retrieve the session data corresponding to this id when opening a new session.