1

I'm using this code wanting to display the url used to access this page:

<?php
$referer = $_SERVER['HTTP_REFERER'];
echo ($referer);
?>

So when I've got this code in file index.php, put it onder www.mysite.com/index.php and I go to www.mysite.com, it should display 'www.mysite.com', shouldn't it?

When I use it though is displays nothing. Not locally on Mamp and also not online. What am I doing wrong?

Glitch Desire
  • 14,632
  • 7
  • 43
  • 55
jiggy1965
  • 157
  • 1
  • 2
  • 14
  • Only if you're coming from another page. If you load it directly in the address bar or from a bookmark it will be empty. – j08691 May 30 '13 at 16:41
  • because you have no referer. Set it inside another file. – Funk Forty Niner May 30 '13 at 16:41
  • 1
    possible duplicate of [In what cases will HTTP\_REFERER be empty](http://stackoverflow.com/questions/6880659/in-what-cases-will-http-referer-be-empty) – Robert May 30 '13 at 16:41
  • Take a look [here](http://stackoverflow.com/questions/6880659/in-what-cases-will-http-referer-be-empty). Are you clicking a link to go from the first page to the second? – dKen May 30 '13 at 16:42
  • @j08691 if I am not mistaken, it won't be blank, instead it won't even be set. – Get Off My Lawn May 30 '13 at 16:42
  • Aha, I understand. The reason I wanted to use it was as a Flash mail form php script. So when I've got a Flash mail form on www.mysite.com, fill it in and click on send it accesses this php script. But that php script can only be used when the data was send at www.mysite.com (so where the Flash file is). So I can't use $_SERVER['HTTP_REFERER']; in this case? – jiggy1965 May 30 '13 at 16:48
  • Already fount it: $_SERVER['SERVER_NAME'] – jiggy1965 May 30 '13 at 16:52
  • @jiggy1965 - See my response. Also, `$_SERVER['SERVER_NAME']` may not work as you want it to on nginx. It will always show the first defined alias for a given virtual host, regardless of which was used to connect. – Glitch Desire May 30 '13 at 17:00

2 Answers2

3

The referer header is an optional header the browser sends when requesting a page, informing the server of the previous page it came from. So for the first page you type into the browser, there will be no referer. After you click on a link from one page to another, there may be a referer set.

deceze
  • 510,633
  • 85
  • 743
  • 889
1

$_SERVER['HTTP_REFERER'] tells you where you came from, not the page you're loading. If you want to show www.mysite.com, you're probably looking for $_SERVER['HTTP_HOST'].

If you want the full URL used to access the page, you're probably after $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'].

To make it easier to understand, these would be the variable values^ if you clicked a link from http://www.mysite.com/index.php to http://www.mysite.com/anotherpage.php:

$_SERVER['HTTP_REFERER'] = "http://www.mysite.com/index.php"
$_SERVER['HTTP_HOST'] = "www.mysite.com"
$_SERVER['REQUEST_URI'] = "/anotherpage.php"
$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'] = "www.mysite.com/anotherpage.php"

Hope this helps, the full documentation on PHP $_SERVER reserved variables may help you more.

^ Not all browsers set the HTTP_REFERER variable. It is optional in the RFC and as such you shouldn't rely on it being there to do any functionality.

Glitch Desire
  • 14,632
  • 7
  • 43
  • 55