$_SESSION variable not updating every iteration
<p>So I have this piece of code that is executed each iteration. The iterations are not ordinary loops but part of a series of ajax request iterations. The first ajax request determines a filename that is assigned to e.g $_SESSION['filename'] and then this variable is pushed into and array etc. Now the problem occurs when i have multiple files being processed.</p>

<p>It happens often that one of the filenames that is generated gets duplicated and inserted into the database which it should not.</p>

<p>First callback function:</p>

<pre><code>    case 'upload':
...
$_SESSION['upload_filename'] = random_filename($alphas).'.'.$fileExt;
...
break;
</code></pre>

<p>second callback function:</p>

<pre><code>    case 'insert_db':
array_push($someGlobalArray, $_SESSION['upload_filename']);
insert($_SESSION['upload_filename']);
break;
</code></pre>

<p>So as I said a new filename is generated properly like 70% of the time. I'm not sure why the old filename from the previous "iteration" lingers and $_SESSION['filename'] does not contain a new filename. I've tried unset the filename after insert() in the second callback function, but that causes a problem where no filename is available to be either pushed into the array or inserted into the database since what I think is that it gets unset before? I've also tried sleep(2) after the insert() and then unset but does not help.</p>

<p>Here is more of the code: <a href="http://pastebin.com/g4fereQx" rel="nofollow">http://pastebin.com/g4fereQx</a></p>
