78

I have a page (index.php) where customers can send me emails. Now I want to see from what website that visitor is coming from.

How can I get the Referrer URL with PHP? I tried with $_SERVER['HTTP_REFERER'] but it's empty all the time. What variable do I need to consider?

Here is what my $_SERVER contains; I put some ---- CENSORED ---- for privacy concerns, there are actually real values.

array(31) {
  ["DOCUMENT_ROOT"]=>
  string(33) "/home/anything/public_html/design"
  ["GATEWAY_INTERFACE"]=>
  string(7) "CGI/1.1"
  ["HTTP_ACCEPT"]=>
  string(129) "text/html, application/xml;q=0.9, application/xhtml+xml, image/png, image/webp, image/jpeg, image/gif, image/x-xbitmap, */*;q=0.1"
  ["HTTP_ACCEPT_ENCODING"]=>
  string(13) "gzip, deflate"
  ["HTTP_ACCEPT_LANGUAGE"]=>
  string(14) "en-US,en;q=0.9"
  ["HTTP_CACHE_CONTROL"]=>
  string(8) "no-cache"
  ["HTTP_CONNECTION"]=>
  string(10) "Keep-Alive"
  ["HTTP_COOKIE"]=>
  string(189) "__utma=76630272.1468291432.1367655794.1367669576.1367674157.3; __utmb=76630272.1.10.1367674157; __utmc=76630272; __utmz=76630272.1367655794.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)"
  ["HTTP_HOST"]=>
  string(25) " ---- CENSORED ---- "
  ["HTTP_USER_AGENT"]=>
  string(57) "Opera/9.80 (Windows NT 6.1) Presto/2.12.388 Version/12.15"
  ["PATH"]=>
  string(13) "/bin:/usr/bin"
  ["QUERY_STRING"]=>
  string(0) ""
  ["REDIRECT_STATUS"]=>
  string(3) "200"
  ["REMOTE_ADDR"]=>
  string(10) "5.15.68.79"
  ["REMOTE_PORT"]=>
  string(5) "57897"
  ["REQUEST_METHOD"]=>
  string(3) "GET"
  ["REQUEST_URI"]=>
  string(12) "/referer.php"
  ["SCRIPT_FILENAME"]=>
  string(45) "/home/anything/public_html/design/referer.php"
  ["SCRIPT_NAME"]=>
  string(12) "/referer.php"
  ["SERVER_ADDR"]=>
  string(13) " ---- CENSORED ---- "
  ["SERVER_ADMIN"]=>
  string(35) " ---- CENSORED ---- "
  ["SERVER_NAME"]=>
  string(25) " ---- CENSORED ---- "
  ["SERVER_PORT"]=>
  string(2) "80"
  ["SERVER_PROTOCOL"]=>
  string(8) "HTTP/1.1"
  ["SERVER_SIGNATURE"]=>
  string(189) "
Apache/2.2.19 (Unix) mod_ssl/2.2.19 OpenSSL/0.9.8e-fips-rhel5 mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635 Server at ---- CENSORED ---- Port 80

"
  ["SERVER_SOFTWARE"]=>
  string(125) "Apache/2.2.19 (Unix) mod_ssl/2.2.19 OpenSSL/0.9.8e-fips-rhel5 mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635"
  ["UNIQUE_ID"]=>
  string(24) "UYUNcUPeiDsAABkR2eYAAAAj"
  ["PHP_SELF"]=>
  string(12) "/referer.php"
  ["REQUEST_TIME"]=>
  int(1367674225)
  ["argv"]=>
  array(0) {
  }
  ["argc"]=>
  int(0)
}
inetphantom
  • 2,498
  • 4
  • 38
  • 61
sorinu26
  • 1,122
  • 2
  • 13
  • 20
  • Possible duplicate of [How can i use $\_SERVER\['HTTP\_REFERER'\] to find that user came from google?](http://stackoverflow.com/questions/10613025/how-can-i-use-serverhttp-referer-to-find-that-user-came-from-google) – Tony Hensler Jan 31 '17 at 10:20

4 Answers4

190

$_SERVER['HTTP_REFERER'] will give you the referrer page's URL if there exists any. If users use a bookmark or directly visit your site by manually typing in the URL, http_referer will be empty. Also if the users are posting to your page programatically (CURL) then they're not obliged to set the http_referer as well. You're missing all _, is that a typo?

Ejaz
  • 8,719
  • 3
  • 34
  • 49
  • What if i use http_refrer and open in new tab then is there any chance we loose http_referer value??? – Muhammad sohail Afzal Dec 05 '16 at 10:25
  • No, it doesn't affect the `HTTP_REFERER` value. – Ejaz Dec 05 '16 at 14:59
  • Please improve your description that it is the browser sending this in the HTTP request header or not. Also add the other case where a browser will not send a referrer in the request , namely when a http link is clicked on a https page. It's better explained here: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referer – ñull Oct 02 '19 at 17:25
14

Underscore. Not space.

$_SERVER['HTTP_REFERER']

bearfriend
  • 10,322
  • 3
  • 22
  • 28
3
$_SERVER['HTTP_REFERER'];

But if you run a file (that contains the above code) by directly hitting the URL in the browser then you get the following error.

Notice: Undefined index: HTTP_REFERER

habib
  • 1,454
  • 17
  • 31
  • 1
    To avoid the Undefined index, you should add an extra check to make sure if the referer url exist. Because it only contains referer url if it came/hit from the http page, otherwise it will remain empty/undefined. `code` if(isset($_SERVER['HTTP_REFERER'])){ } `code` – Hafiz Ameer Hamza Mar 21 '22 at 05:33
  • Or since PHP 7.0 you can do: `$ref=$_SERVER['HTTP_REFERER'] ?? ""` This will set the `$ref` variable to the orig variable, or empty string if it is not set. It's called null coalescing operator. https://www.tutorialspoint.com/php7/php7_coalescing_operator.htm – Marki555 Apr 19 '23 at 10:27
2

If $_SERVER['HTTP_REFERER'] variable doesn't seems to work, then you can either use Google Analytics or AddThis Analytics.

  • 1
    Analytics would clearly be a better solution here. There is obviously something weird with the server-variable here. In addition, http-referer can easily be manipulated. – OptimusCrime May 04 '13 at 13:30
  • I would have to disagree. http-referrer can be manipulated, sure... that's not what I disagree with, what I disagree with is that you should use a whole bloated analytics program (that most would have to pay for) to do one simple thing instead of using a system variable that is there for a reason. the referrer is there TO BE MANIPULATED. A good programmer will use wisely and validate that the returned results matched up with the results you were expecting. Say, if your referrer url doesn't have a specific ?customvar=323433&customvar2=43243444, that you were expecting, it would be invalid. – Gregory Bowers Jul 10 '23 at 13:51