pg.
pg.

Reputation: 2531

FInding out referring page (php)

At my work I often need to figure out where our traffic comes from. We buy google ads and that traffic gets identified by a query string in the url. (mywebsite.com/?x="google_ad_group_4").

On every page I include some sessions stuff that sets $_SESSION['x'] to $_GET['x'] if $_GET['x'] is there. If there is no $_GET['x'] I go through some other options to see where they came from and set that in $_SESSION['x']:

$refurl = parse_url($_SERVER['HTTP_REFERER']);
$query = $refurl['query'];
parse_str($query, $result);

if (isset($result['q'])&& strstr($_SERVER['HTTP_REFERER'],'google')) {
    $_SESSION['x'] = 'G-'.str_replace('\\"',"X",$result['q']);
}elseif (isset($result['p'])&& strstr($_SERVER['HTTP_REFERER'],'yahoo')) {
    $_SESSION['x'] = 'Y-'.$result['p'];

//took out bing, aol, ask etc in the name of brevity

}else{
    if ($refurl['host']){
        $_SESSION['x'] = $_SESSION['x'].'_ref-'.$refurl['host'];
    }
}

This way I can append the search query that brought the user to the site and what search engine they used. I log the incoming $_SESSION['x']'s.

Many users are coming in with $_SESSION['x']'s of "_ref-mywebsite.com" which doesn't make sense, if they were coming from my own domain, they'd have already had a $_SESSION['x'] set on whatever page they'd been on. Is this because they have their browser's security turned up high or something?

Am I missing something obvious? Is there a smarter way to do this?

Upvotes: 2

Views: 7925

Answers (3)

Eugene
Eugene

Reputation: 3375

I think that a possible scenario is:

  • A new visitor comes to the website with normal referrer;
  • He closes his browser(this clears his session cookie) with the website's tab opened;
  • Reopens the browser with the website restored in old tab;
  • Clicks on any link on the page and gets to another page with referrer from same domain and clean session.

Upvotes: 0

misakm
misakm

Reputation: 122

Unless the client (the browser) passes you the "HTTP_REFERER" in the heading, you won't get it. And that depends on the site they come from.

I don't know what your workflow is like, but one thing you can do is get it with JavaScript and pass it to your PHP script. Hope this helps.

Upvotes: 0

Starx
Starx

Reputation: 78991

You can get the referrer like this

echo $_SERVER['HTTP_REFERER'];

But as mentioned in comment, it can easily be manipulated.

Upvotes: 5

Related Questions