13.09.2016 Views

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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Using Network Lookup Functions<br />

455<br />

The best use of this approach is to combine information from different sources to add<br />

some value.You can see one good example of this approach in Philip Greenspun’s infamous<br />

script that produces the Bill Gates Wealth Clock at http://philip.greenspun.com/<br />

WealthClock.<br />

This page takes information from two sources. It obtains the current U.S. population<br />

from the U.S. Census Bureau’s site. It also looks up the current value of a Microsoft share<br />

<strong>and</strong> combines these two pieces of information, adds a healthy dose of the author’s opinion,<br />

<strong>and</strong> produces new information—an estimate of Bill Gates’ current worth.<br />

One side note: If you’re using an outside information source such as this for a commercial<br />

purpose, it’s a good idea to check with the source or take legal advice first.You<br />

might need to consider intellectual property issues in some cases.<br />

If you’re building a script like this, you might want to pass through some data. For<br />

example, if you’re connecting to an outside URL, you might like to pass some parameters<br />

that would normally be typed in by the user. If you’re doing this, it’s a good idea to<br />

use the urlencode() function.This function takes a string <strong>and</strong> converts it to the proper<br />

format for an URL; for example, transforming spaces into plus signs.You can call it like<br />

this:<br />

$encodedparameter = urlencode($parameter);<br />

One problem with this overall approach is that the site you’re getting the information<br />

from may change its data format, which will stop your script from working.<br />

Using Network Lookup Functions<br />

<strong>PHP</strong> offers a set of “lookup” functions that can be used to check information about<br />

hostnames, IP addresses, <strong>and</strong> mail exchanges. For example, if you were setting up a directory<br />

site such as Yahoo! when new URLs were submitted, you might like to automatically<br />

check that the host of an URL <strong>and</strong> the contact information for that site are valid.<br />

This way, you can save some overhead further down the track when a reviewer comes to<br />

look at a site <strong>and</strong> finds that it doesn’t exist or that the email address isn’t valid.<br />

Listing 20.2 shows the HTML for a submission form for a directory like this.<br />

Listing 20.2 directory_submit.html—HTML for the Submission Form<br />

<br />

<br />

Submit your site<br />

<br />

<br />

Submit site<br />

<br />

URL: <br />

Email contact: <br />

<br />

<br />

<br />

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

Saved successfully!

Ooh no, something went wrong!