Page 1 of 2

can i delete session table data

Posted: Sat Dec 23, 2017 8:24 pm
by sandeepmishra83
Hi i am using opencart 3.0.2.0.. what is use of session table data. can i delete this data.
will it create any issue further or old orders/customer details will be changed.

Re: can i delete session table data

Posted: Sat Dec 23, 2017 10:39 pm
by straightlight
I would not suggest to manually truncate the session data. There's already an hourly set interval period to handle the sessions from the core.

[UNSUPPORTED] Re: can i delete session table data

Posted: Sun Feb 18, 2018 12:18 pm
by fairmount
Hi,

Can I ask which part of the code handles this hourly clearance? OR is there a setting in Admin? The number of records in my OC3 session table is over 100,000 and I get only three or four visitors per day and this is over a span of just 3 months.

The site is really slow with just OC3, Journal, Openstock and vqmod installed.

Thanks.

Re: can i delete session table data

Posted: Sun Feb 18, 2018 10:53 pm
by straightlight
Use TextCrawler to track lines of codes in OC. You don't need to ask the forum for these types of inquiry.

Re: can i delete session table data

Posted: Mon Feb 19, 2018 6:32 am
by fairmount
Hi Straightlight,

Thanks for the response. If I know which code to search for, I would have. I do not know which part of code within OC manages the session data database to search and hence the question. Apologies if this seems obvious, just a newbie here.

I am presuming that some part of php code within OC will go through the session table in the database and clear all the old ones. Correct me if wrong. I want to know which file within OC, Or part of the code within OC handles this os that I can check if that bit is correct in my setup.

I use use GrepWin for searching within OC, is textcrawler better than this? Will give it a go anyway.

Regards,
KG

Re: can i delete session table data

Posted: Fri May 11, 2018 7:59 am
by d3z1gnr
How did you go KG? Was looking at this subject myself this morning.

fairmount wrote:
Mon Feb 19, 2018 6:32 am
Hi Straightlight,

Thanks for the response. If I know which code to search for, I would have. I do not know which part of code within OC manages the session data database to search and hence the question. Apologies if this seems obvious, just a newbie here.

I am presuming that some part of php code within OC will go through the session table in the database and clear all the old ones. Correct me if wrong. I want to know which file within OC, Or part of the code within OC handles this os that I can check if that bit is correct in my setup.

I use use GrepWin for searching within OC, is textcrawler better than this? Will give it a go anyway.

Regards,
KG

Re: can i delete session table data

Posted: Thu Jun 28, 2018 11:44 pm
by exit15
Running 3.0.2.0 I had 450,000 records in one website and 1 MM lines in another. So I made a copy and truncated the table just to see what happens . Everything is working just fine. The question is weather the hourly cleanup process is in fact working. Clearly there are expired session records in the table. Any Idea??

Re: can i delete session table data

Posted: Mon Dec 17, 2018 4:18 am
by nongetjie
fairmount wrote:
Sun Feb 18, 2018 12:18 pm
Hi,

Can I ask which part of the code handles this hourly clearance? OR is there a setting in Admin? The number of records in my OC3 session table is over 100,000 and I get only three or four visitors per day and this is over a span of just 3 months.

The site is really slow with just OC3, Journal, Openstock and vqmod installed.

Thanks.
Hi, did you ever find a solution for this? I'm experiencing this right now :-(

Re: can i delete session table data

Posted: Mon Dec 17, 2018 4:18 am
by nongetjie
exit15 wrote:
Thu Jun 28, 2018 11:44 pm
Running 3.0.2.0 I had 450,000 records in one website and 1 MM lines in another. So I made a copy and truncated the table just to see what happens . Everything is working just fine. The question is weather the hourly cleanup process is in fact working. Clearly there are expired session records in the table. Any Idea??
Hi did you find a solution for this?

Re: can i delete session table data

Posted: Sun Dec 30, 2018 3:51 am
by exit15
I did not. If fact it is a bug (check github). The system simply does not delete old session and as a result there are thousands of OC databases inflated with millions of expired records in the 'session' table. For the time being every week or so I manually delete an avg of 150,000 records with this query:

"delete from `session` where `session`.expire < DATE_SUB(NOW(), INTERVAL 1 DAY);" (Removes everything older than 1 DAY, you can write '3 HOURS' if you prefer)

Daniel suggested a few fixes when alerted to the issue, but they did not work for me because the code on github is very different than the official distribution and every-time I copied a file that he changed to resolve this problem, it created a new problem. I am not a programmer, but i wish we could just plug the above query somewhere and simply delete old sessions

Re: can i delete session table data

Posted: Sun Jan 13, 2019 6:56 pm
by rpmb
exit15 wrote:
Sun Dec 30, 2018 3:51 am
I did not. If fact it is a bug (check github). The system simply does not delete old session and as a result there are thousands of OC databases inflated with millions of expired records in the 'session' table. For the time being every week or so I manually delete an avg of 150,000 records with this query:

"delete from `session` where `session`.expire < DATE_SUB(NOW(), INTERVAL 1 DAY);" (Removes everything older than 1 DAY, you can write '3 HOURS' if you prefer)

Daniel suggested a few fixes when alerted to the issue, but they did not work for me because the code on github is very different than the official distribution and every-time I copied a file that he changed to resolve this problem, it created a new problem. I am not a programmer, but i wish we could just plug the above query somewhere and simply delete old sessions
Thanks for this. I used the sql query you posted and used it to create a php script out of the public_html directory and then set a cron job to run it daily removing old sessions. Seemed to be the easiest automated way to prune the sessions.

Re: can i delete session table data

Posted: Sat Mar 02, 2019 1:46 am
by thbr02
rpmb wrote:
Sun Jan 13, 2019 6:56 pm
exit15 wrote:
Sun Dec 30, 2018 3:51 am
I did not. If fact it is a bug (check github). The system simply does not delete old session and as a result there are thousands of OC databases inflated with millions of expired records in the 'session' table. For the time being every week or so I manually delete an avg of 150,000 records with this query:

"delete from `session` where `session`.expire < DATE_SUB(NOW(), INTERVAL 1 DAY);" (Removes everything older than 1 DAY, you can write '3 HOURS' if you prefer)

Daniel suggested a few fixes when alerted to the issue, but they did not work for me because the code on github is very different than the official distribution and every-time I copied a file that he changed to resolve this problem, it created a new problem. I am not a programmer, but i wish we could just plug the above query somewhere and simply delete old sessions
Thanks for this. I used the sql query you posted and used it to create a php script out of the public_html directory and then set a cron job to run it daily removing old sessions. Seemed to be the easiest automated way to prune the sessions.
Would you mind share the script? I'm in the same situation and it would be nice solving it with cron.

Re: can i delete session table data

Posted: Sat Nov 02, 2019 4:45 pm
by pm-netti
This is big bug from Opencart.

My friend was 2.5 million lines in table over two years. Very few online retailers have the skill to use cron.
This should be set on the admin page where you can set the duration of the session.
-peku-

Re: can i delete session table data

Posted: Sat Nov 02, 2019 5:29 pm
by letxobnav
just use files to store sessions and php will handle it in stead of OC.

Re: can i delete session table data

Posted: Sat Nov 02, 2019 7:07 pm
by pm-netti
letxobnav wrote:
Sat Nov 02, 2019 5:29 pm
just use files to store sessions and php will handle it in stead of OC.
File system/config/default.php:

Code: Select all

$_['session_engine']       = 'db';
Why this is default setting 'db', because that recording sessions on a data insert to database is an "unfinished project" and unstable for the functioning of the trade? Can that "eternal session" jeopardize Opencart Online Stores for hacker attacks?

Re: can i delete session table data

Posted: Sat Nov 02, 2019 7:27 pm
by letxobnav
That is set to "db" because some guy in OC development probably read some article somewhere about storing sessions in databases and thought "why the hell not? Let's see if that works. Surely we can do better than native php session handling, besides, files are boring and databases are cool!"

That is how most functionality in OC, already working perfectly, is implemented again just in a different way.

Re: can i delete session table data

Posted: Thu Oct 01, 2020 11:20 pm
by bitbo
I simply added this to "catalog/controller/startup/session.php"
This is triggered always when a new session is established (first call of website from anyone)


...
if (isset($_COOKIE[$this->config->get('session_name')])) {
$session_id = $_COOKIE[$this->config->get('session_name')];
} else {
$session_id = '';
}
$this->db->query("DELETE FROM `" . DB_PREFIX . "session` WHERE TIMESTAMPADD(DAY, 1, expire) < NOW()");

$this->session->start($session_id);
...


This deletes all session records which are expired since one DAY (replace DAY by HOUR, if you like)
A side effect for me is that I am now able to BACKUP my database without the OUT OF MEMORY issue

Comment: There may exist better solutions - but it just works for me....

Re: can i delete session table data

Posted: Thu Oct 01, 2020 11:34 pm
by letxobnav
This is triggered always when a new session is established (first call of website from anyone)
That is triggered on every page request.

Re: can i delete session table data

Posted: Mon Oct 05, 2020 10:05 am
by fegdeed
@letxobnav what is the best way to do it?

Re: can i delete session table data

Posted: Mon Oct 05, 2020 3:32 pm
by letxobnav
If you want to use the database I would suggest to add a __destruct function to the session db class as the current gc function does not function.

Code: Select all

	public function __destruct() {
		if (ini_get('session.gc_divisor')) $gc_divisor = ini_get('session.gc_divisor');
		else $gc_divisor = 1;
		if (ini_get('session.gc_probability')) $gc_probability = ini_get('session.gc_probability');
		else $gc_probability = 1;
		if ((rand() % $gc_divisor) < $gc_probability) {
			$expire_dt = date('Y-m-d H:i:s', time() - ini_get('session.gc_maxlifetime'));
			$this->db->query("DELETE FROM `" . DB_PREFIX . "session` WHERE expire < '".$expire_dt."'");
		}
	}
Otherwise, use the session file class which already has a __destruct function for session garbage collection.

we store sessions in files and in addition identify bots (public and covert) and delete the session assigned to them immediately after script end as bots do not retain sessions and as such a new session is generated for bots on each page request which are subsequently useless, use space and make session clashes more likely.