PHP referrer URL of a page

$ref=@$_SERVER[HTTP_REFERER];
echo "Referrer of this page  = $ref ";
Referrer Script in PHP Referrer is the URL from where the visitor has arrived to the page. If you have reached here by clicking a link from google.com then google URL is the referrer for you in this page. We can find out the referrer by using PHP. This is useful for the webmasters to know where from the traffic to the site is coming. Which advertisement campaign is successful and which is not. We will also know the keywords used by the visitors in different search engines to arrive at the site. Here is the simple code to know the referrer in PHP
if(isset($_SERVER['HTTP_REFERER'])) {
echo "Here is your referer to this page
$_SERVER['HTTP_REFERER']
"; }else{ echo "There is no referrer information available for this visit
Return to this page by visiting other pages under the heading Related tutorial, then you can see your referrer URL. "; }
There is no referrer information available for this visit

Return to this page by visiting other pages under the heading Related tutorial, then you can see your referrer URL.

@ is used to suppress any error message is generated. You can remove it and check.

Breaking the referrer URL

Here is an example of referrer URL we can get from HTTP header. This one is from Google.
https://www.google.co.in/url?sa=t&rct=j&q=&esrc=s&source=web&cd=6&cad=rja&uact=8
&ved=0CD4QFjAF&url=http%3A%2F%2Fwww.plus2net.com%2Fphp_tutorial%2Fphp_ip.php
&ei=GC6tVL_ECYObuQSOp4KIBA&usg=AFQjCNFqBr-A1sG3F2UoqzmqnGUYVTvb9Q&bvm=bv.83134100,d.c2E
We are interested to know how many visitors are arriving from Google or from any other search engines. So we will find out the host part from this URL by using parase_url() function. This function returns us an array of elements. Here is the code to display the array.
<?Php
$url="https://www.google.co.in/url?sa=t&rct=j&q=&esrc=s&
source=web&cd=6&cad=rja&uact=8&
ved=0CD4QFjAF&url=http%3A%2F%2Fwww.plus2net.com%2Fphp_tutorial%2Fphp_ip.php&
ei=GC6tVL_ECYObuQSOp4KIBA&
usg=AFQjCNFqBr-A1sG3F2UoqzmqnGUYVTvb9Q&bvm=bv.83134100,d.c2E";
$details=parse_url($url);
//echo $details[host];
while (list ($key, $val) = each ($details)) { 
echo "$key -> $val <br>"; 
}
?>
Output is here
scheme -> http 
host -> www.google.co.in 
path -> /url 
query -> sa=t&rct=j&q=&esrc=s&source=web&
cd=6&cad=rja&uact=8&ved=0CD4QFjAF&
url=http%3A%2F%2Fwww.plus2net.com%2Fphp_tutorial%2Fphp_ip.php&
ei=GC6tVL_ECYObuQSOp4KIBA&usg=AFQjCNFqBr-A1sG3F2UoqzmqnGUYVTvb9Q&
bvm=bv.83134100,d.c2E 
We are interested in only the domain part of this so we can get the name of the site sending us the visitors like this.
echo $details[host];
We can store only this information in a table and find out the domains which are sending us the traffic.

You can create one application where we will store only the domain part of the referral site and prepare a report saying which site has send how many visitors to a page or site.

Storing referrer along with other data.

We can collect and store visitors IP address, referrer, browser details, time of visit etc in MySQL database table or in a CSV file

Google has recently changed to secured search where search words are encrypted so they are no longer passed to user sites. Before that it was easy to find out search keywords using referrer string.

Getting search query from referrer address.

If the search is not encrypted then we can get the search query from the referrer query string. For this we will use one Bing search as an example. First we will break the referred url by using parse_url function and take out the query string part. From the query string we will take out the text query part by using parse_str function.
$url="https://www.bing.com/search?q=php%20get%20ip%20and%20network&form=MB1078&mkt=es-ES&setlang=es-ES";
$details=parse_url($url);
$query_string=$details[query];
$query=parse_str($query_string,$output);
echo $output[q];
The output is here
php get ip and network
Same way you can find out the ip address of visitors to your site by using PHP. Click here to know the IP address by using PHP.

Getting referrer URL in ASP


Some of the browsers setting can be changed to stop the browser from sending any referrer information. Read here to know how you can change the settings for FireFox browser for referrer.
IP address and Geo Location of visitor Storing visitor details in MySQL table
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    jayanta

    10-02-2010

    Not working
    Colin

    17-02-2010

    It doesn't work for me either... but this does: $ref = $_SERVER['HTTP_REFERER'];
    Sumit Joshi

    11-03-2010

    Great...Fully Working for Me. Great help... Since long time I was looking for this. You are great... Thanks again
    jeremy

    28-03-2010

    This is very bad advise, This code will only work when the php.ini setting register_globals is set to on. Having register globals set to on is a security risk and is normally set to off on modern hosting platforms. The use of the error suppression operator '@' is also a concern. You should use somthing like $ref = (isset($_SERVER['HTTP_REFERER'])) ? $_SERVER['HTTP_REFERER'] : '';
    Wayne

    12-05-2010

    @jeremy The outer parentheses wrapping "isset" aren't necessary, are they?
    Money

    26-05-2010

    $_SERVER['HTTP_REFERER'] is working well
    Fernando

    04-06-2010

    Thanks! But, how get refereer's page title?
    bakke

    06-06-2010

    Don't take it that these variables are always "there". Use isset to avoid a PHP notice which (when many) can slow site performance down.
    will

    08-06-2010

    is there a way to format the output? sorry i have to type out the slashes cause the post thinks i am putting a link in the post... anyhow $_SERVER['HTTP_REFERER']; gives output with the h t t p:slashslash/sitethatreferred.com can you format it to be without the h t t p: and just be sitethatreferred.com only? Does anyone know how to format the output? Thanks!
    Stephin

    11-09-2010

    yes..working.........superb site for php learners...:) nice one
    Herbie

    25-01-2011

    @ will: just use $ref = substr($ref, 7);
    Khuram

    23-07-2011

    Sir,i want a function when user come into my site using any REFERER a mail will be send at to my email :)note only for REFERER not for all user
    Yuksel

    21-02-2013

    is it possible to get e-mail address when a link (to my server) in an e-mail clicked by php code?
    smo1234

    22-02-2013

    If the link contains any tracking code or email address then we can get the email address. This is what spam email links does. Even images src address submit a tracking code which says that the email address is valid one.

    Post your comments , suggestion , error , requirements etc here





    PHP video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer