Get and match referer domain name to referer?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Get and match referer domain name to referer?

Post 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!
User avatar
louie35
Forum Contributor
Posts: 144
Joined: Fri Jan 26, 2007 8:40 am
Location: Dublin
Contact:

Post 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) {}
?>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Post by JAB Creations »

Thanks, that works! :)
Post Reply