user1481850
user1481850

Reputation: 248

Check if traffic is coming from specific URL?

I want to make a count of visits to my website from referal websites. I know there are many programs such as Google analytics but there will show you that my taffic is coming from www.facebook.com for example. I want to check if the traffic is coming from some specific urls that I specify such as www.facebook.com/myfanpage.

Befor I think about php I tried several methods with javascript that they did not seem to function the way I wanted to. For my search for php I only found this function. Any Ideas ?

$_SERVER['HTTP_REFERER']

Upvotes: 3

Views: 8411

Answers (4)

user849137
user849137

Reputation:

$_SERVER['HTTP_REFERER'] Will do exactly what you need.

if (strstr($_SERVER['HTTP_REFERER'], 'facebook.com') !== false) {
    // Facebook brought me to this page.
}

elseif (strstr($_SERVER['HTTP_REFERER'], 'google.com') !== false ) {
    // Google brought me to this page.
}

Upvotes: 9

ggdx
ggdx

Reputation: 3064

Sorry, I know this is 6 months late but surely if the url was http://mydomain.com/?p=facebook.com then this would also be true? a better way would be to explode the referrers url based on / then extract the 4th section i.e.

$refererUrl = $_SERVER['HTTP_REFERER'];
$Exploded_URL = explode("/",$refererUrl);
$urlToCheck = $Exploded_URL[3].'.'.$Exploded_URL[4];
if($urlToCheck == 'facebook.com'){
/* From Facebook */
} elseif ($urlToCheck == 'google.com'){
/* From Google */
}

Upvotes: 3

sundar
sundar

Reputation: 1760

You will be able to check only if the HTTP Request has referer which is actually accessible in PHP using HTTP_REFERER. So its solely responsible from the referring website.

Get original URL referer with PHP?

The above post also will help you.

Upvotes: 0

Trott
Trott

Reputation: 70115

$_SERVER['HTTP_REFERER'] should contain the URL that the user is coming from to get to your page. It's not a function. It's simply a value. So you can use it for this purpose.

Do note, however, that the value is easily spoofed. (It's taken from the HTTP request header, and the user can send whatever they want.) It should be acceptably reliable if you're just collecting stats for your own interest or whatever. But if you're trying to use it to secure the page (e.g., only show certain content if the visitor came from a certain URL), forget it.

Upvotes: 1

Related Questions