Entry
How can I extract just the domain name out of HTTP_REFERER?
Feb 29th, 2008 09:40
dman, Shashank Tripathi, Philip Olson, Dan Thies, http://www.ttnr.org
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