About
Kooty's TechnoBabble is a blog by Brennan Kootnekoff, and is about the interesting day-to-day life of a multi-platform systems engineer/administrator. From time to time, he will post useful tidbits of information here that may save hours of time, and prevent premature gray hairs and aging.
Search
Categories
Other
FortiScan Certified
April 19th, 2008. comments are open 3 commentsTwo PHP Snippets for URL Parsing

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

3 Responses to “Two PHP Snippets for URL Parsing”

  1. Whookam Says:

    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.

  2. kooty Says:

    @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.

  3. kooty Says:

    @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.

Leave a Comment

Trackback this post  |  Subscribe to the comments via RSS Feed