Note that there are some explanatory texts on larger screens.

plurals
  1. POAccess to the same Database Session variables across scripts
    text
    copied!<p>I have tried to solve my problem for a day or so now and I have read as many posts on StackOverflow as I can before asking this question but I have to accept this has beaten me and I feel certain it can be solved.</p> <p>I am using Database Sessions to store variables that need to be accessed from multiple calls per page load to the same PHP file. Here is the problem in a nutshell!</p> <p>From a web page multiple calls per page load are made to my php file, each passing a different parameter, eg: domain.com?el-1, domain.com?el-2, domain.com?el-3 etc up to 20 per page load. Their job is to return an image chosen from an array.</p> <p>I am using PDO and am using "PDO::ATTR_PERSISTENT => true"</p> <p>I have chosen Database Sessions for two reasons. 1) One of the modes is to synchronise the images returned so that they all return the 2nd or 3rd image in their respective arrays, and 2) the final call is when they click the Call to Action button and we use the Session Variables and store to a data file the index of the images sent.</p> <p>I hope this makes sense? :-)</p> <p>From my research it is OK to have</p> <pre><code>$session = new Session; </code></pre> <p>in each call because if a Session is already cached it won't create a new Session, and this seems to be OK, I hope!</p> <p>The other issue is that (as you already know,) browsers make these calls in a random order so when I am attempting to synchronise the results I want to use the index from ?el=1 and let the other calls use that value. So far I have tried the following:</p> <pre><code>$session = new Session; $_SESSION['el_01'] = 0; if( $passedParameter == "1" ) { $_SESSION['el_01'] = calculatedIndex; // Always between 1 and 'n' } else { while ( $_SESSION['el_01'] == 0) { sleep(.1);} } </code></pre> <p><strong>NB: I know that the sleep() function is not an elegant solution but I am just trying to make it work first.</strong></p> <p>The result is that all of the other calls with ?el=2, ?el-3, etc... just sit and hang until PHP timesout after 30 seconds.</p> <p>When I look at the MySQL database el_01 is being stored as a valid value and not just 0, so is available for the others to read.</p> <p>How do I fix this so my variables are seen across calls.</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