I recently was coding in PHP, and needed to get just the fully qualified domain name from the (FQDN) $_SERVER[’HTTP_REFERER’] variable. So I whipped a quick function up that may help some of you beginners out there looking to parse a URL:
function domainviaurl($url){
$parsed_url=parse_url($url);
$fqdn = $parsed_url['host'];
return $fqdn;
}
This will just spit out the domain name. The next one will take it one step further, and actually get the IP address of the referrer via the domain name.
function ipviaurl($url{
$parsed_url=parse_url($url);
$tld = $parsed_url['host'];
$ip = gethostbyname($tld);
return $ip;
}
Hope this helps out!
Brennan
July 30th, 2008 at 4:30 pm
Hey, just wanted to point out that you’re actually parsing out the FQDN (fully qualified domain name) with the ‘host’ piece of the parse_url – not the TLD. The TLD is the “.com”, “.net” etc. part of the name. I was looking for a script to parse just the TLD, for tracking purposes, and thought you might have found an easy way (something slightly more sophisticated than substr(domain, strrpos(domain, ‘.’)) since some TLD’s are like .co.uk etc), but no such luck.
July 30th, 2008 at 5:13 pm
@Whoocam
Thanks a ton for picking up on that. I wrote this post in the wee hours of the morning, so I didn’t proofread. Anyhow, I’m pretty surprised that you stumbed upon my blog just right now, because I was just finished restoring all the main content after a major crash.
July 30th, 2008 at 5:14 pm
@Whoocam
Hm, I’ll keep my eyes open for a script like that because I may be needing something like that. If I find one, I’ll give you a shout via e-mail.