I have a script that colors the requested country on a world map with GD and PHP. The PHP requests are called with checkboxes. It kind of looks like if you call the PHP scripts too fast then it returns an “Xed out” error image. Is there a way to queue the PHP requests with setTimeout or something else, so a new checking event never fails?
Here is the Javascript called by the onClick events:
function onBoxClicked(frame, country){
var randomNumber = Math.floor(Math.random()*100000001);
if (document.getElementById(country).checked == true){
window.parent.document.getElementById('world_map').src=(country)+".php?r=" + randomNumber;
}else if (document.getElementById(country).checked == false){
window.parent.document.getElementById('world_map').src=(country)+"_unload.php?r=" + randomNumber;
}
}
Here is a typical country PHP file (I know there is some junk that can be removed):
<?php
session_cache_limiter('nocache');
$cache_limiter = session_cache_limiter();
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");// Date in the past
$ip=$_SERVER['REMOTE_ADDR'];
$oldImageName = 'world_map2_users_copy.png';
$newImageName = $ip.'.'.'world_map2_users_copy.png';
if (file_exists($newImageName)){
$im = imagecreatefrompng($newImageName);
}else{
copy($oldImageName, $newImageName);
$im = imagecreatefrompng($newImageName);
}
$syria_color = imagecolorallocate($im, 0, 158, 96);
imagefill($im, 780, 205, $syria_color);
ImagePNG($im, $newImageName);
ImagePNG($im);
ImageDestroy($im);
?>
If you do
there is the possibility of two PHP-script writing to the same file at the same time.
Why are you writing to disk anyway?
Just do:
The best solution is to generate all image files in advance, so in JavaScript you can just say:
In that case you’ll also lose the caching issues.