简体   繁体   中英

How do you make a count that counts the number of page-views? (PHP)

For each ad on my site, I would like a box that shows the number of views it has (similar to this site).

How would I capture the number of page views an ad has? Is there a function that does this in PHP?

Well, no there is no function that does this magically : you'll have to do a bit of work -- not that hard, though ;-)

There are two possible things you can count.


For the first one, number of times an ad is displayed , the basic idea is :

  • Your are displaying an add -- you already know how to do that
  • When displaying it, you'll update a counter, probably in your database :
    • Your SQL query will look like update ad_counters set counter = counter + 1 where ad_id = 123
    • 123 being replaced by the identifier of your ad, of course
  • And, when displaying the ad, you'll have to select that counter, and display it alongside the ad.


For the second one, number of times an ad is clicked , the basic idea is generally :

  • Not have the ad be a direct link to the page of the product
  • Instead, the link of the ad will look like http://yoursite.com/ad.php?id=123
  • And, when someone load that page, it will :
    • increment the counter of clicks : update ad_clicks_counter set counter = counter + 1 where ad_id = 123
    • redirect the user to the real page of the ad, or display it directly.

In fact, this is precisely what's done on SO :

  • An ad has a link such as http://ads.stackoverflow.com/a.aspx?Task=Click&ZoneID=4&CampaignID=785&AdvertiserID=161&BannerID=1123&SiteID=1&RandomNumber=384213225&Keywords=php%2ccounter%2cx-user-highrep%2cx-user-registered
  • And when you click on it, you are redirected to the real page of the ad, which can be such as http://www.xpolog.com/home/solutions/landing.jsp


Of course, those two counters can be in the same table -- or even in the table in which you have the list of all ads :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM