Jump to content

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

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.