bcit-ci / CodeIgniter

Open Source PHP Framework (originally from EllisLab)
https://codeigniter.com/
MIT License
18.27k stars 7.63k forks source link

Codeigniter3 and session_gc #5981

Open wadealer opened 3 years ago

wadealer commented 3 years ago

As as CI documentation says

Under CLI, the Session library will automatically halt itself, as this is a concept based entirely on the HTTP protocol.

But PHP documentations says:

Probability based GC works somewhat but it has few problems. 1) Low traffic sites' session data may not be deleted within the preferred duration. 2) High traffic sites' GC may be too frequent GC. 3) GC is performed on the user's request and the user will experience a GC delay. Therefore, it is recommended to execute GC periodically for production systems using, e.g., "cron" for UNIX-like systems. Make sure to disable probability based GC by setting session.gc_probability to 0.

And now we faced the probleam. We cann't use CRON because session class doesn't work with CLI. If we try to use anothe example from PHP docs:

// Need active session to initialize session data storage access.
session_start();

// Executes GC immediately
session_gc();

// Clean up session ID created by session_gc()
session_destroy();

it is do nothing, because handleers are not inited properly. Any ideas?

lemil commented 3 years ago

Can you write that code inside a controller (like a webhook) and call it with wget within cron ?

[controller/Session.php] ... public function gc(){ //Do some sanity check first // Need active session to initialize session data storage access. session_start(); // Executes GC immediately session_gc(); // Clean up session ID created by session_gc() session_destroy(); }

[...]

[crontab]

[...]

on cron?

SolinSM commented 3 years ago

Hello, I fork this to start solving this problem. I have a solution to this issue. It works done in my apps and websites.

I'll explain the steps of my work and the topic in a few days.