Forum Moderators: coopster

Message Too Old, No Replies

How Do I increment hit counter upon .jpg load?

incrementing hit counter upon load of .jpg

         

webcreations

10:36 pm on Jul 5, 2011 (gmt 0)

10+ Year Member



Hello,
This is my first post so thanks in advance for your help. I am trying to get my site to increment a hit counter on the server side when a .jpg loads on an external site such as craigslist, ebay etc.

For instance if my website html code call to display www.samplesite.com/39392/1.jpg it would trigger the hit counter for that item (39392) on the server side. No additional info would be loaded to such places as ebay or craiglist. is this possible? We use php on our site. It has the be a 1.jpg output on craigslist or ebay. Thank you.

webcreations

topr8

7:02 am on Jul 6, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



if you want to see how many times the image has been called why don't you just look in your log files?

good luck here at WebmasterWorld

webcreations

9:26 am on Jul 6, 2011 (gmt 0)

10+ Year Member



Hello Topr8, thx for your reply. I have customers who have hit counters within their user panels that need to have the counters trigger upon 1.jpg load. Trying to minimize craigslist / ebay html foot print but also trigger customers hit counters at the same time.

topr8

10:47 am on Jul 6, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



you can use .htaccess to redirect ebayimage.jpg to process.php

then when process.php is called use it to increment the value in the database, before then returning the image (ensuring you have set the mime types correctly)

then on their control panel show the value from the database for their hit counter.
alternatively i'm pretty sure you can write code to search the log file for that particular image/s and then write a routine to count the number of occurances - which would be your hit counter value.

rocknbil

4:19 pm on Jul 6, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Or, if you can't do .htaccess, or don't know how,

<img src="https:example.com/img-counter.php?f=myimage" alt="">

where img-counter.php takes $_GET['f'] and does what topr8 describes.

webcreations

12:56 am on Jul 7, 2011 (gmt 0)

10+ Year Member



Hey Rocknbil, Do you think this would work completely on the server side? In other words, we cannot have any reference to .php, $_GET['f'] or counters on the craigslist side of the html. Please advise. thanks to Topr8 as well for the suggestions, please keep them coming.

rocknbil

3:47 pm on Jul 7, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You mean Cragslist won't let you or you don't want to show it? Well, in either case, yeah, you'll have to do it at the server level as topr8 suggested if you want to capture the plain jpg request.

wheel

3:57 pm on Jul 7, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes the above method of using .htaccess to run a php script that increments the counter then displays the image works. I'm using it to track pageviews on articles published on external sites. They embed a 1X1 image in the article, every time it gets called a mysql database gets updated.

wheel

4:29 pm on Jul 7, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Actually a point of clarification. I've got the following .htaccess in my images directory:
ErrorDocument 404 /404.php

Because in the articles that get published each image has a unique filename (that identifies the article), like 134690.jpg. We don't actually have a 134690.jpg file though, just a single blank.jpg file.

THe 404.php script determines if a .jpg file is being called. if so, it uses the filename of the jpg to update the database record for the appropriate article, then serves the blank.jpg file. Otherwise, it serves a standard 404.

In your case, you'd want the .htaccess in the root, and have the 404.php script to check for 1.jpg, if so, then parse the URL being called to grab the item number for the URL and then do the incrementing - then serve a .jpg file. You'd have to make sure you didn't actually have a 1.jpg file though, otherwise the 404.php script won't get called.

webcreations

9:46 pm on Jul 7, 2011 (gmt 0)

10+ Year Member



Wheel, I will try this and see if it works. Thanks for all of your help and will keep you posted. All of your efforts are appreciated.

webcreations

7:24 pm on Jul 8, 2011 (gmt 0)

10+ Year Member



Hey Wheel and Rocknbil and others,
I tried what wheel suggested in wheel's 4:29 pm on July 7, 2011 reply two posts up. For some reason its not able to call 404.php upon
load on server, but if we browse the url in firefox directly then it will call 404.php. This still didn't solve
the as you are aware, we are not going to load image url in browser rather we need it called from within HTML page without the call code being reflected on the html page. Any ideas? thank you. WC

Thank you.