0

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.

Sparky
  • 98,165
  • 25
  • 199
  • 285
vvffl
  • 73
  • 1
  • 9
  • 1
    Doesn't your proposed idea defeat the whole purpose of sessions? If you're just trying to save some user settings, then you wouldn't need to save or restore a session, just save/restore the settings. Why care about the session itself? – Sparky Mar 10 '18 at 00:51
  • Thank you Sparky for this remark. Here's what I thought: **(1)** sessions are simple to use, one can store and load data in one line ; **(2)** even if HTTP is a stateless protocol, sessions allow to store objects ; **(3)** since one can set a lifetime, I could use a function such as `session_destroy()` or `session_regenerate_id()` to trigger some 'autosave' function ; **(4)** storing data in session is more efficient than using custom code because it's a built-in functionality and data is already loaded – vvffl Mar 12 '18 at 07:29
  • And about #4 : **I thought the data were kept 'in-memory'**, in some cache or other stuff. BUT when trying to change `session_save_path()` I discovered that there were files in /var/lib/php/sessions... So I guess it isn't more efficient than using another persistent storage system. – vvffl Mar 12 '18 at 07:30

0 Answers0