0

I have a page with some user selectable options and a button that, when clicked, runs a PHP script and then refreshes a div with another PHP file that uses a session variable that is created at the end of the first PHP script. If the user presses the button again, with different options selected, the div is updated using the newly replaced session variable. The problem is that sometimes, perhaps 1 in 10 times or so, the old session variable data is loaded. I suspect that the second PHP file is catching the variable too early, before it has been updated, but I tried unsetting the session variable at various points with out any luck.

First PHP file:

session_start();
$needle = array();
foreach($_POST['checkboxes'] as $key => $value){
$needle[] = "$value";
}
// code that processes the values from needle and outputs $data
unset($_SESSION['data']);
$_SESSION['data']=$data;

Second PHP file:

session_start();
echo $_SESSION['data'];

Javascript:

$(".userdata").click(function() {
   $.post("first.php", $("form#checkboxes").serialize());
});
$(function() {
   $("#button").click(function() {
   $("#div").load('second.php')
   })
})
Nimantha
  • 6,405
  • 6
  • 28
  • 69
  • 1
    Show code please. It's hard to tell what may be happening otherwise. – Mahn Jun 25 '12 at 03:12
  • is the session stored in database? – David Cheung Jun 25 '12 at 03:16
  • What happens if you use [session_destroy](http://us.php.net/manual/en/function.session-destroy.php)? – thisgeek Jun 25 '12 at 03:45
  • Could you post the involved javascript? Assuming you are using Ajax (based on this question's tags), you should make sure that the PHP script has run completely run before you the javascript that updates the div. Concretely, you should make sure that the javascript code that updates the div is part of the 'success' ajax callback that you link to the code that calls the first PHP script. – Joris Jun 25 '12 at 03:49

2 Answers2

0

The problem is that in some cases the first PHP script has not finished running before you click the button that loads the second PHP script (like I implied before in my comment). The fact that this happens is related to how scripts are scheduled by the webserver (which is a different subject entirely).

You thus need to make sure that when you click the button that runs the second script, the first script has completely finished running.

Because in my knowledge, javascript does not allow blocking/signaling on a variable (like Java does), you'll have to use a more 'dirty' technique called busy waiting.

The best way to do this, is to include an extra variable in the javascript you are using.

var wait = false;
function reloadSecond (){
  if (wait){
    setTimeout('reloadSecond()',200);
  } else {
    $("#div").load('second.php');
  }  
}

$(".userdata").click(function() {
   wait = true;
   $.post("first.php", $("form#checkboxes").serialize(), function(){
     wait = false;
   });

});
$(function() {
   $("#button").click(reloadSecond);
})

While 'busy waiting' is generally not considered the most elegant solution, I think you don't have many other options in this case (except for serverside push, which is much more complicated). Additionally, you'll probably only incur the extra 200 millisecond (or less, you can of course change this value) waiting time once or twice.

(side note: I assume that javascript is single threaded here, which is true in almost all cases: Is JavaScript guaranteed to be single-threaded?).

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Joris
  • 3,220
  • 2
  • 23
  • 19
  • I think it is. If you place the click handler that calls the div update inside the callback, it will only work after the callback is called. This means that if you click the button that calls the second PHP script (#button) before the callback is called, nothing will happen. This will result in the same behavior that you experienced before (the second button works in most cases, but in some cases - if server side scheduling is not chronological - it won't). – Joris Jun 25 '12 at 05:12
  • Yes, this would be a more default use case and will always work - given that no errors occur in the PHP script (FYI, you can have a look at jquery's ajax error callbacks that cope with that - not mandatory though). – Joris Jun 25 '12 at 05:25
0

Can it be a case where the browser or proxy server is caching the html data? Try setting the headers to tell them not to cache. See the examples in http://php.net/manual/en/function.header.php for what headers to set.

iWantSimpleLife
  • 1,944
  • 14
  • 22