Online Shopping : Computers : Programming : Languages : PHP

+ Search
Add Entry AlertManage Folder Edit Entry Add page to
Did You Find This Entry Useful?

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


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:
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