Re: can i delete session table data
Posted: Tue Oct 06, 2020 7:49 am
@letxobnav Thank you .
OpenCart Community Forum - Discuss shopping cart and e-commerce solutions.
https://forum.opencart.com/
Code: Select all
public function __construct($registry) {
$this->db = $registry->get('db');
// session expire using php.ini, defaults to 1 hour
$this->expire = ini_get('session.gc_maxlifetime') ?? 3600;
// session garbage collection using php.ini, defaults to 1% probability
$gc_probability = ini_get('session.gc_probability') ?? 1;
$gc_divisor = ini_get('session.gc_divisor') ?? 100;
if ((rand() % $gc_divisor) < $gc_probability) $this->gc();
}
Code: Select all
public function gc() {
$dt = date('Y-m-d H:i:s',((int)time()));
$this->db->query("DELETE FROM " . DB_PREFIX . "session WHERE expire < '".$dt."'");
}
Code: Select all
public function read($session_id) {
$query = $this->db->query("SELECT data FROM " . DB_PREFIX . "session WHERE session_id = '" . $this->db->escape($session_id) . "'");
if ($query->num_rows) return json_decode($query->row['data'], true);
else return false;
}
There was a bug in that the session garbage collection was never called. This was fixed in 3.0.3.7.svdumitrescu wrote: ↑Mon Jan 31, 2022 10:25 pmHi. Found this topic after an "opencart session table" Google search. I was too puzzled why more than 300 pages by 25 records each in the oc_session table, with very little activity on that store. To the point most of those here made, I see there's an "expire" datetime field, and many values are long overdue. I think there must naturally be a piece of native script somewhere in the OC code, to delete the records at their expire time. Wondering why isn't it working. Otherwise, even if such a script was and worked, how could the admin adjust the time of the session, is there any setting anywhere? And finally, I don't really see the use of the session records since at least in the OC standard installation, the session table doesn't store anything else but language and currency. And here comes my point, and the gentle request I extend to this community, is there any mod, extension out there, to have the session record other data, like mainly the originating IP?