Jump to content

Weird Happenings


Joshua F

Recommended Posts

My problem is if you access the file with the code below directly it will add 2 to `hits` in the table, but if you use image tags and it is displayed on a webpage it does what it should do, which is just add 1 to `hits`. I can not figure out the reason why. I am using a custom database class which uses PDO.

 

<?php
if (is_file("./imgs/{$_GET['image']}.png")) {
$file = "./imgs/{$_GET['image']}.png";
} else {
header( "HTTP/1.0 404 File not found" );
require_once '../errors/404.error';
exit;
}

require_once '../classes/Database.php';
$db = new Database(array("host" => "127.0.0.1", "user" => "root", "pass" => "", "name" => "test"));

$filesize = filesize($file);
$filetype = getimagesize($file);
$filetype = $filetype['mime'];

$db->query("INSERT INTO `snaps` (`image_id`, hits, `first_view`) VALUES (?, 1, ?) ON DUPLICATE KEY UPDATE `hits` = hits+1;")->bind(1, $_GET['image'])->bind(2, time())->execute();

header('Content-Type: ' . $filetype);
header('Content-Length: ' . $filesize);
readfile($file);

Link to comment
https://forums.phpfreaks.com/topic/272283-weird-happenings/
Share on other sites

When you load an image directly, some browsers may end up making the request twice for various reasons.  For instance firefox will request it twice under certain conditions to use the image as an icon for the window on the task bar.

 

edit: https://bugzilla.mozilla.org/show_bug.cgi?id=583351 - The firefox bug

Link to comment
https://forums.phpfreaks.com/topic/272283-weird-happenings/#findComment-1400872
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.