Is there a way to use unserialize
with a memory/size limit?
Currently we have:
$data = unserialize($_SESSION['visits']);
and we occasionally get:
PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 17645568 bytes) in Unknown on line 0
when a visitor has had a lot of visits in a short period of time (session value stores information about each page visited).
If the length of $_SESSION['visits']
is above a million characters it causes the issue so I can do a simple check on that but is there a better solution than this:
if(strlen($_SESSION['visits']) <= 1000000) {
$data = unserialize($_SESSION['visits']);
} else {
$data = array();
}
I thought try catch
might behave better but it didn't get caught:
try{
$data = unserialize($_SESSION['vists']);
} catch(\Exception $exception){
error_log('Caught memory limit');
}
The answer to this question is not to increase the memory size.