Specifications
?>
</body>
</html>
Lets’ go through the interesting parts of this script.
First, we take the URL and apply the parse_url() function to it. This function returns an
associative array of the different parts of an URL. The available pieces of information are the
scheme, user, pass, host, port, path, query, and fragment. Typically, you aren’t going to
need all of these, but here’s an example of how they make up an URL.
Given an URL such as
http://nobody:secret@bigcompany.com:80/script.php?variable=value#anchor
the values of each of the parts of the array would be
• scheme: http://
• user: nobody
• pass: secret
• host: bigcompany.com
• port: 80
• path: script.php
• query: variable=value
• fragment: anchor
In our script, we only want the host information, so we pull it out of the array as follows:
$url = parse_url($url);
$host = $url[host];
After we’ve done this, we can get the IP address of that host, if it is in the DNS. We can do
this using the gethostbyname() function, which will return the IP if there is one, or false
if not:
$ip = gethostbyname($host)
You can also go the other way using the gethostbyaddr() function, which takes an IP as para-
meter and returns the hostname. If you call these functions in succession, you might well end
up with a different hostname from the one you began with. This can mean that a site is using a
virtual hosting service.
Using Network and Protocol Functions
C
HAPTER 17
17
USING NETWORK
AND
PROTOCOL
FUNCTIONS
377
LISTING 17.3 Continued
22 7842 CH17 3/6/01 3:39 PM Page 377










