I have been testing a simple long poll on my website, and for some reason a chunk of my server-side code is being executed despite the variable that triggers it ($init
) being false
.
I have a small hunch that the problem lies within the client-side code, but I can't seem to figure out what it is.
Code
Client Side - JavaScript:
window._Poll: {
listen: function(init){
$.ajax({
url: "/poll.php",
method: "post",
data: { init: init },
success: function(res){
console.log(res);
/* set the init variable to false in the next run */
_Poll.listen(false);
}
});
}, init: function(){
/* set the init variable to true in the first run */
this.listen(true);
}
}
/* on page load */
_Poll.init();
Server Side - PHP:
set_time_limit(0);
session_write_close();
if(isset($_POST["init"]) && ($_POST["init"] == true || $_POST["init"] == false)){
/* the first time this script is called, this variable is true - but for the
* second time and onwards it is false (like it should be) */
$init = $_POST["init"];
echo $init;
/* therefore this block should only be firing once as init is only true once */
if($init){
if(/* some more database checking */){
die("true");
}
} else {
die("false");
}
}
The console output for this the second time and onwards is
false true
When in reality it should be
false false
I have been really stuck on this, and from what I could find, nothing seems to be able to help me. So any help is appreciated,
Cheers.