Page 1 of 1

Get and match referer domain name to referer?

Posted: Tue Mar 13, 2007 4:58 pm
by JAB Creations
This gets the file name...

Code: Select all

$referer = basename($_SERVER['HTTP_REFERER']);
Though I would like to get the domain name alone instead of the file name.

So if the referer is http://www.example.com/example1 the script would echo example.com.

I also want to match the domain string of the referer to $_SERVER['HTTP_HOST'] (current domain name). This would let me avoid having to fill in the domain name as a variable when porting the script to other projects.

I've tried something along the following...

Code: Select all

$domain = $_SERVER['HTTP_HOST'];
$referer = $_SERVER['HTTP_REFERER'];
if ($referer == $domain) {}
But again I need to figure out how to get the domain name out of the referer first. Thanks!

Posted: Tue Mar 13, 2007 5:17 pm
by louie35
try this

Code: Select all

<?php
$ref = $_SERVER['HTTP_REFERER'];
$ref = preg_replace("/http:\/\//i", "", $ref);
$ref = preg_replace("/^www\./i", "", $ref );
$ref = preg_replace("/\/.*/i", "", $ref );


$domain = $_SERVER['HTTP_HOST']; 
$referer = $ref ;
if ($referer == $domain) {}
?>

Posted: Tue Mar 13, 2007 5:26 pm
by volka

Posted: Tue Mar 13, 2007 6:17 pm
by JAB Creations
Thanks, that works! :)