tags:

views:

73

answers:

5

Hi,

So I am trying to get the page where a visitor came from. I inserted this code into a php file and I am trying to see the page's URL but it is not working, any suggestions?

<?php 

  $ref = getenv("HTTP_REFERER"); 
  echo $ref; 

?>

(added this after some answers) I have also tried

print $_SERVER["HTTP_REFERER"];

and that doesn't work either

it worked after i updated the website many times, not sure why was there a problem in the first place, thanks anyway :)

+4  A: 

Have you tried accessing through the $_SERVER superglobal?

print $_SERVER["HTTP_REFERER"];
Jonathan Sampson
does not work either
Are you getting an error?
Jonathan Sampson
Assuming your script is correct (as opposed to an syntax error etc.) Are you sure there's an http referrer? Check your HTTP headers, as PHP gets the referrer from there. If the client did not send a referrer, there will be none.
futureelite7
it does not return any errors, it just either prints nothing or stores nothing in the variable. I tried many different pages to see if the problem was with the website i am referring from, but nothing worked
Have you tried accessing other values in `$_SERVER`?
Jonathan Sampson
it worked after i updated the website many times, not sure why was there a problem in the first place, thanks anyway :)
+1  A: 
$ref = $_SERVER['HTTP_REFERER'];

Relevant manual page: http://php.net/manual/en/reserved.variables.server.php

Emil Vikström
tried that, does not work either
+2  A: 

getenv() is used if it's being run as a CGI script. With a SAPI you use $_SERVER["HTTP_REFERER"].

Ignacio Vazquez-Abrams
+2  A: 

$_SERVER['HTTP_REFERER'] is the best way to access this information.

Based on your comments on other responses:

  1. Are you actually coming from somewhere? If you refresh your browser this value will likely not be sent. So make sure your browser is sending the header. If you put this script on a public url, I'll be happy to check it out and verify.
  2. You should really turn on all errors. If the header is not sent and you access it anyway, PHP will emit an E_NOTICE. If you're debugging your code you should turn on all error message and make sure there are no E_NOTICE's or worse.
Evert
A: 

Again, read http://php.net/manual/en/reserved.variables.server.php: With HTTP_REFERER there is a comment:

The address of the page (if any) which referred the user agent to the current page. This is set by the user agent. Not all user agents will set this, and some provide the ability to modify HTTP_REFERER as a feature. In short, it cannot really be trusted.

f13o