maestro416
maestro416

Reputation: 924

PHP redirect/hit counter

I'm trying to figure out how to count the number of views that have been sent to my site from another website. I have a banner advertised and I want to have the banner link directed to a php script that will count the number of times people from website abc.com vistied the site.

The question is how do I go about doing this? I was thinking about setting up a table in mysql with a different row for each site and then have that specific row increment the count by one.

Problem is I'm not sure how to use the function i++ (if thats even right function). I am new to php, sorry if what I'm asking is a basic thing

Upvotes: 1

Views: 747

Answers (2)

Rik
Rik

Reputation: 90

Usually banners point to a specific URL on your server from the referring website so you can track where things come from.

Eg: BigSite.com has a banner for you that links to MySite.com/links/01941731.htm

Using something like .htaccess on an Apache server you can parse the incoming "01941731" part, safely check it against your database and incremement the key it relates to so that it counts against an incoming link from BigSite.com

Thats how I would do it :)

Upvotes: 0

Will I AM
Will I AM

Reputation: 220

You can get the referrer from $_SERVER['HTTP_REFERER']. It doesn't always exist so you'd need to check, and if it does then perform an upsert.

I would do something like this:
   

if(isset($_SERVER['HTTP_REFERER'])){
        $referrer = $_SERVER['HTTP_REFERER'];
        $query = "INSERT INTO 'referrals'
        ('referrer', 'count')
        VALUES
        ($referrer, 1)
        ON DUPLICATE KEY UPDATE
        'count' = 'count' + 1";
        $result = mysql_query($query);
    }

Upvotes: 2

Related Questions