Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tracking image views - PHP

I'd like for my app to be able to tell if an image hasn't been viewed in the last 30 days and remove it (along with data in the DB associated with it). I know you can have PHP read and output the image dynamically but I've heard its quite taxing on the system. Is there a way to for me to track these hits even when the image is viewed directly? (would htaccess be able to do this?) Thanks in advance.

like image 843
RS7 Avatar asked Jul 13 '26 05:07

RS7


2 Answers

For .htaccess something like this...

RewriteRule ^image/(.*)$ image.php?id=$1

And for PHP...

$id = $_GET["id"]; //don't forget to sanitize
mysql_query("UPDATE images SET views = views + 1 WHERE image = '$id'"); //update no of views
header("Content-Type: image/jpeg"); //send image header
readfile($id); //output file to browser
like image 134
Dejan Marjanović Avatar answered Jul 14 '26 18:07

Dejan Marjanović


You can parse out the HTTP logs and do this analysis post-factum.

That said, I'd still recommend going with a "dynamic PHP file outputting an image" approach - note that the PHP file can simply stream out a file. Yes, it'll be slower than not going through PHP at all, but it won't be a significant performance hit on your system.

like image 40
Alex Weinstein Avatar answered Jul 14 '26 18:07

Alex Weinstein



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!