Note that there are some explanatory texts on larger screens.

plurals
  1. POFatal error using session_start inside foreach (after migration)
    text
    copied!<h2>When did the problem start?</h2> <p>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.</p> <h2>The code</h2> <p>The code that is giving problems is the following:</p> <pre><code>session_start(); $items=$_SESSION['items']; $_SESSION['numItems']=0; session_write_close(); foreach($items as $num =&gt; $currentItem){ //here I work with $currentItem... session_start();//starting error line $_SESSION['numItems']++; session_write_close(); } session_start(); unset($_SESSION['numItems']); session_write_close(); </code></pre> <h2>The error</h2> <p>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.</p> <p>Now, the weirdest thing is that if I check the value of <code>$_SESSION['numItems']</code> it can arrive to 12500 (or more) when the array <code>$items</code> cannot have more than 250 elements. So something is creating kind of an infinite loop!</p> <h2>Things to have into account</h2> <ol> <li><p>If I comment out the three session lines inside the <code>foreach</code> structure (or I move them outside) everything works fine.</p></li> <li><p>If I comment out only the second of those lines, the problem persists.</p></li> <li><p>The same code structure but with a <code>while</code> instead of a <code>foreach</code> is working in another script on the same server.</p></li> <li><p>My hosting provider cannot say me which PHP version and configuration did I have. Now, the version is 5.2.17.</p></li> </ol> <p>Thank you for your help!</p> <h2>Edit:</h2> <p>As it is causing a lot of confusion:</p> <p>The code structure opens and closes the session because this way I can read the session variables from a second file and have the user updated with the execution progress. This second file is reading <code>$_SESSION['numItems']</code> once every 2 seconds and also closes the session with <code>session_write_close();</code>.</p> <h2>Edit 2:</h2> <p>Testing the code but increasing the max execution time with set_time_limit(90) doesn't help at all. It just gives the same error (after waiting longer) and $_SESSION['numItems'] ends with a higher value (around 21000).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload