PHP connection_aborted () not working on page change
I am trying to serve an image by adding a MySQL row every second the image was viewed.
I serve it in 1024 bit chunks (total image size is 20KB)
The problem is that if I load the page that displays the image and then close the window or click a link that takes me to another page, the script will work and will not work die
as it should.
ignore_user_abort(false);
$file = 'a.jpg';
header('Content-Type: image/jpeg');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
$conn = mysql_connect("localhost","user","pass");
mysql_select_db("mydb",$conn);
$fp=fopen($file,"rb");
$i=0;
while (!feof($fp)) {
print(fread($fp,1024));
sleep(1);
mysql_query("INSERT INTO table (VIEWTIME) VALUES ('$i')");
$i++;
flush();
ob_flush();
if (connection_aborted()) {
die();
}
}
I am trying to find only a "server side" solution as I have some technical limitations that prevent me from using any JS or any client side languages.
+3
source to share