Link to home
Start Free TrialLog in
Avatar of xposegrafix
xposegrafix

asked on

How many times an image has been viewed using php & mysql

How can I write a code to store the number of times an image or a page has been viewed using php & mysql. The page  is only one file  "index.php". Please let me know if you have any questions.

E.g:
http://www.domain.com/                       <!--A page {hompage}-->
http://www.domain.com/?p=journal      <!--A page-->
http://www.domain.com/?p=contact     <!--A page-->
http://www.domain.com/?p=portfolio&pid=52&page=photos&photoid=754   <!--A photos been viewed-->

Thank you
ASKER CERTIFIED SOLUTION
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Are you wanting these figures to display on the website or do you want them so you can see how your site traffic is developing and don't need to be in the public domain?

If the second,

http://www.google.com/analytics/

This will give you a very good idea of where traffic is going on your site.


Also there are also scripts that can parser your server log files.

http://awstats.sourceforge.net/
To my amazement, I do not have a teaching example of a hit counter.  It's such a common tool, that I guess I have not seen a question about it.

Here is the logic you need to implement.  Capture $_SERVER["REQUEST_URI"] and store it in a data base table in the column named "uri" -- this is the URL with the GET arguments intact.  On each page load, write this to your data base table.  You might also want to include a DATETIME column in the table.  Name it "whn."

Then you can use a variety of queries to retrieve the information from the table.  Examples include...

SELECT COUNT(*) FROM hitCount -- How many total page loads occurred?
SELECT COUNT(*) FROM hitCount WHERE uri LIKE '%photoid=754' -- How many times was photo #754 viewed?
SELECT whn FROM hitCount WHERE uri LIKE '%journal' ORDER BY whn DESC LIMIT 1 -- What was the last time someone looked at the "journal" page?

I agree about Google Analytics.  It is the lingua franca of traffic monitoring.  

Best to all, ~Ray
Avatar of xposegrafix
xposegrafix

ASKER

Thanks for your help.