Wednesday, 11 September 2013

Fatal error using session_start inside foreach (after migration)

Fatal error using session_start inside foreach (after migration)

When did the problem start?
My hosting provider had to change my site to another server like one week
ago. Since then, one of my scripts is throwing an error. As everything was
working before the changes I'm assuming that the code was initially
correct so I'm totally confused why can this happen and I would like you
to try getting me in the good direction.
The code
The code that is giving problems is the following:
session_start();
$items=$_SESSION['items'];
$_SESSION['numItems']=0;
session_write_close();
foreach($items as $num => $currentItem){
//here I work with $currentItem...
session_start();//starting error line
$_SESSION['numItems']++;
session_write_close();
}
session_start();
unset($_SESSION['numItems']);
session_write_close();
The error
If I run the code I get a PHP Fatal error: Maximum execution time of 30
seconds exceeded in the line commented as starting error line.
Now, the weirdest thing is that if I check the value of
$_SESSION['numItems'] it can arrive to 12500 (or more) when the array
$items cannot have more than 250 elements. So something is creating kind
of an infinite loop!
Things to have into account
If I comment out the three session lines inside the foreach structure (or
I move them outside) everything works fine.
If I comment out only the second of those lines, the problem persists.
The same code structure but with a while instead of a foreach is working
in another script on the same server.
My hosting provider cannot say me which PHP version and configuration did
I have. Now, the version is 5.2.17.
Thank you for your help!

No comments:

Post a Comment