13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

458 Chapter 20 Using Network <strong>and</strong> Protocol Functions<br />

Listing 20.3<br />

Continued<br />

echo ‘Thank you for submitting your site.’<br />

.’It will be visited by one of our staff members soon.’<br />

// In real case, add to db of waiting sites...<br />

?><br />

<br />

<br />

Let’s go through the interesting parts of this script.<br />

First, you take the URL <strong>and</strong> apply the parse_url() function to it.This function<br />

returns an associative array of the different parts of an URL.The available pieces of<br />

information are the scheme, user, pass, host, port, path, query, <strong>and</strong> fragment.Typically, you<br />

don’t need all these pieces, but here’s an example of how they make up an URL.<br />

Consider the following URL at http://nobody:secret@example.com:80/<br />

script.php?variable=value#anchor.<br />

The values of each of the parts of the array are<br />

n scheme: http<br />

n user: nobody<br />

n pass: secret<br />

n host: example.com<br />

n port: 80<br />

n path: /script.php<br />

n query: variable=value<br />

n fragment: anchor<br />

In the directory_submit.php script, you want only the host information, so you pull it<br />

out of the array as follows:<br />

$url = parse_url($url);<br />

$host = $url[‘host’];<br />

After you’ve done this, you can get the IP address of that host, if it is in the domain<br />

name service (DNS).You can do this by using the gethostbyname() function, which<br />

returns the IP if there is one or false if not:<br />

$ip = gethostbyname($host);<br />

You can also go the other way by using the gethostbyaddr() function, which takes an<br />

IP as a parameter <strong>and</strong> returns the hostname. If you call these functions in succession, you<br />

might well end up with a different hostname from the one you began with.This can<br />

mean that a site is using a virtual hosting service where one physical machine <strong>and</strong> IP<br />

address host more than one domain name.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!