faqts : Computers : Programming : Languages : PHP

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

16 of 41 people (39%) answered Yes
Recently 8 of 10 people (80%) answered Yes

Entry

How can I extract just the domain name out of HTTP_REFERER?

Nov 27th, 2008 14:36
healthy man, marine Jone, dman, Shashank Tripathi, Philip Olson, Dan Thies,


The parse_url() function shown below will give the entire domain, with
the host name. So these will be extracted separately: 
a.domain.com
b.domain.com
c.domain.com
If you, like me, need to extract only the domain name, without the host,
then try this code:
preg_match('/[^.]+\.[^.]+$/', $_SERVER['HTTP_REFERER']), $matches);
echo "Domain name is: {$matches[0]}\n";
-----
The parse_url() function is useful for this, consider this code:
if (isset($_SERVER['HTTP_REFERER'])) {
    $ref = parse_url($_SERVER['HTTP_REFERER']);
    echo $ref['host']; // the domain name
}
See the PHP manual for details on what $ref would contain in the above 
example:
  http://www.php.net/function.parse-url
http://viajesmaldivas.miblogguia.com
http://www.healthbyhealth.com
http://www.healthment.com
http://www.mesotheliomadown.com
http://www.mesotheliomaout.com
http://www.pickarticle.com