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.

Backing Up or Mirroring a File<br />

459<br />

If the URL is valid, you then go on to check the email address. First, you split it into<br />

username <strong>and</strong> hostname with a call to explode():<br />

$email = explode(‘@’, $email);<br />

$emailhost = $email[1];<br />

When you have the host part of the address, you can check to see whether there is a<br />

place for that mail to go by using the dns_get_mx() function:<br />

dns_get_mx($emailhost, $mxhostsarr);<br />

This function returns the set of Mail Exchange (MX) records for an address in the array<br />

you supply at $mxhostsarr.<br />

An MX record is stored at the DNS <strong>and</strong> is looked up like a hostname.The machine<br />

listed in the MX record isn’t necessarily the machine where the email will eventually<br />

end up. Instead, it’s a machine that knows where to route that email. (There can be more<br />

than one; hence, this function returns an array rather than a hostname string.) If you<br />

don’t have an MX record in the DNS, there’s nowhere for the mail to go.<br />

Note that the dns_get_mx() function is not implemented in Windows versions of<br />

<strong>PHP</strong>. If you are using Windows, you should look into the PEAR::Net_DNS package,<br />

which will work for you (http://pear.php.net/package/NET_DNS).<br />

If all these checks are okay, you can put this form data in a database for later review<br />

by a staff member.<br />

In addition to the functions you just used, you can use the more generic function<br />

checkdnsrr(), which takes a hostname <strong>and</strong> returns true if any record of it appears in<br />

the DNS.<br />

Backing Up or Mirroring a File<br />

File Transfer Protocol, or FTP, is used to transfer files between hosts on a network. Using<br />

<strong>PHP</strong>, you can use fopen() <strong>and</strong> the various file functions with FTP as you can with<br />

HTTP connections, to connect to <strong>and</strong> transfer files to <strong>and</strong> from an FTP server.<br />

However, a set of FTP-specific functions also comes with the st<strong>and</strong>ard <strong>PHP</strong> install.<br />

These functions are not built into the st<strong>and</strong>ard install by default.To use them under<br />

Unix, you need to run the <strong>PHP</strong> configure program with the --enable-ftp option <strong>and</strong><br />

then rerun make. If you are using the st<strong>and</strong>ard Windows install, FTP functions are<br />

enabled automatically.<br />

(For more details on configuring <strong>PHP</strong>, see Appendix A,“Installing <strong>PHP</strong> <strong>and</strong> <strong>MySQL</strong>.”)<br />

Using FTP to Back Up or Mirror a File<br />

The FTP functions are useful for moving <strong>and</strong> copying files from <strong>and</strong> to other hosts. One<br />

common use you might make of this capability is to back up your website or mirror files<br />

at another location. Let’s look at a simple example using the FTP functions to mirror a<br />

file.This script is shown in Listing 20.4.

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

Saved successfully!

Ooh no, something went wrong!