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.

426 Chapter 18 Implementing Secure Transactions with <strong>PHP</strong> <strong>and</strong> <strong>MySQL</strong><br />

We used the function tempnam() to create a unique temporary filename.You can<br />

specify both the directory <strong>and</strong> a filename prefix.You are going to create <strong>and</strong> delete these<br />

files in around 1 second, so it’s not very important what you call them as long as they are<br />

unique.We specified a prefix of ‘pgp’ but let <strong>PHP</strong> use the system temporary directory.<br />

The statement<br />

$comm<strong>and</strong> = "/usr/local/bin/gpg -a \\<br />

--recipient 'Luke Welling ' \\<br />

--encrypt -o $outfile $infile";<br />

sets up the comm<strong>and</strong> <strong>and</strong> parameters that will be used to call GPG.You need to modify<br />

this statement to suit your situation. As when you used it on the comm<strong>and</strong> line, you<br />

need to tell GPG which key to use to encrypt the message.<br />

The statement<br />

system($comm<strong>and</strong>, $result);<br />

executes the instructions stored in $comm<strong>and</strong> <strong>and</strong> stores the return value in $result.You<br />

could ignore the return value, but it lets you have an if statement <strong>and</strong> tells the user that<br />

something went wrong.<br />

After you finish with the temporary files you use, you delete them using the<br />

unlink() function.This means that your user’s unencrypted email is stored on the server<br />

for a short time. If the server fails during execution, it is even possible that the file could<br />

be left on the server.<br />

While you are thinking about the security of your script, it is important to consider<br />

all flows of information within your system. GPG encrypts your email <strong>and</strong> allows your<br />

recipient to decrypt it, but how does the information originally come from the sender?<br />

If you are providing a web interface to send GPG-encrypted mail, the flow of information<br />

will look something like Figure 18.5.<br />

Sender’s<br />

Browser<br />

1<br />

<strong>Web</strong><br />

Server<br />

2<br />

Recipient’s<br />

Mail<br />

Server<br />

3<br />

Recipient’s<br />

Mail<br />

Client<br />

Figure 18.5<br />

In the encrypted email application, the message is sent via the<br />

Internet three times.<br />

In this figure, each arrow represents the message being sent from one machine to another.<br />

Each time the message is sent, it travels through the Internet <strong>and</strong> might pass through<br />

a number of intermediary networks <strong>and</strong> machines.

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

Saved successfully!

Ooh no, something went wrong!