开发者

Referral URL in php

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; 

?>
开发者_StackOverflow

(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 :)


Have you tried accessing through the $_SERVER superglobal?

print $_SERVER["HTTP_REFERER"];


$_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.


Maybe a stupid remark, but $_SERVER["HTTP_REFERER"] only works if you enter the page using a hyperlink. e.g.

/goto.html

<a href="refer.php">go to refer</a>

/refer.php

<?php
print "You entered using a link on ".$_SERVER["HTTP_REFERER"];
?>

HTTP_REFERER doesn't work if you enter the link location directly in your browser.


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


<?php
  echo $_SERVER['HTTP_REFERER'];
?>

The above code works! However, many of my students find it hard, at first, to grasp that $_SERVER['HTTP_REFERER'] requires arriving from a link.

I give them the below (tested) code (or "web page") to demonstrate. The above code is at the bottom.

show-referer.php

<?php
  if ( isset( $_SERVER['HTTP_REFERER'] ) ) {
    $referer = $_SERVER['HTTP_REFERER'];
  } else {
    $referer = 'No Link - No Referer - Direct URL Entry';
  }
  echo $referer;
?>

<p>See the referer in action
  <a href="show-referer.php">from this page!</a>
</p>

<?php
  echo $_SERVER['HTTP_REFERER'];
?>

The show-referer.php page links to itself when you click the link, which should cause the browser to generate an HTTP_REFERER.


$ref = $_SERVER['HTTP_REFERER'];

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


If you compute all these answers, you end up with something looking like :

<?php
if isset($_SERVER['HTTP_REFERER']) {
    $ref = $_SERVER['HTTP_REFERER'];
}
else {
    $ref = "Direct Entry";
}
?>


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜