PHP Referrer – HTTP Referrer

by Hiroshi on June 16, 2008

in Redirection, Referrer, URL

Suppose user submits a form and post data to the other page. We can find out its referrer URL (From where he came?) for redirection or other purpose.
Referrer can be used in following ways.

  • Redirection (Redirect back from where the form was posted, lets say after mailing…)
  • To referrer a web page to a friend.

HTTP Referrer

@$HTTP_REFERER;

Printing HTTP Referrer

<?php
$ref=@$HTTP_REFERER;
echo "$ref";
?>

Redirection With HTTP Referrer

<?php
$ref = @$HTTP_REFERER;
// print $ref;
header ("location:".$ref);
?>

*** Redirection With HTTP Referrer While Globals Are Off ***

<?php
$ref=getenv('HTTP_REFERER');
header ("location:".$ref);
?>

For more reference on this proceed here.

Variable $ref contains the value of referring page.

Consider Redirection After Mail Script

<?php
if ($fromEmail)
{
 
$ref=@$HTTP_REFERER;
 
if ($fromName=='')
$subject= "Your friend has referred you this link from www.sitename.com";
else
$subject= "$fromName has referred you this link from www.sitename.com";
 
$message = "
Dear $toName,
Your friend $fromName has referred this link to you.
Please visit this link by clicking $ref
";
$from = "From: $fromEmail\r\n";
mail($toEmail, $subject, $message, $from);
 
header ("location:".$ref);
 
}
?>

Download HTTP Referrer Script

Related Posts

Previous post:

Next post: