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 Basic Authentication<br />

399<br />

Looking back at Listing 17.3, you can see that we created one user (‘username’)<br />

with an unencrypted password <strong>and</strong> another user with an encrypted one (‘testuser’) to<br />

illustrate the two possible approaches.<br />

Protecting Multiple Pages<br />

Making a script like the ones in Listings 17.1 <strong>and</strong> 17.2 protect more than one page is a<br />

little harder. Because HTTP is stateless, there is no automatic link or association between<br />

subsequent requests from the same person.This makes it harder to have data, such as<br />

authentication information that a user has entered, carry across from page to page.<br />

The easiest way to protect multiple pages is to use the access control mechanisms<br />

provided by your web server.We look at these mechanisms shortly.<br />

To create this functionality yourself, you could include parts of the script shown in<br />

Listing 17.1 in every page that you want to protect. Using auto_prepend_file <strong>and</strong><br />

auto_append_file, you can automatically prepend <strong>and</strong> append the code required to<br />

every file in particular directories.The use of these directives was discussed in Chapter 5,<br />

“Reusing Code <strong>and</strong> Writing Functions.”<br />

If you use this approach, what happens when your visitors go to multiple pages within<br />

your site? Requiring them to re-enter their names <strong>and</strong> passwords for every page they<br />

want to view would not be acceptable.<br />

You could append the details the users entered to every hyperlink on the page.<br />

Because they might have spaces or other characters that are not allowed in URLs, you<br />

should use the function urlencode() to safely encode these characters.<br />

This approach still has a few problems, though. Because the data would be included<br />

in web pages sent to the users <strong>and</strong> the URLs they visit, the protected pages they visit<br />

will be visible to anybody who uses the same computer <strong>and</strong> steps back through cached<br />

pages or looks at the browser’s history list. Because you are sending the password back<br />

<strong>and</strong> forth to the browser with every page requested or delivered, this sensitive information<br />

is being transmitted more often than necessary.<br />

There are two good ways to tackle these problems: HTTP basic authentication <strong>and</strong><br />

sessions. Basic authentication overcomes the caching problem, but the browser still sends<br />

the password to the server with every request. Session control overcomes both of these<br />

problems.We look at HTTP basic authentication now <strong>and</strong> examine session control in<br />

Chapter 23,“Using Session Control in <strong>PHP</strong>,” <strong>and</strong> in more detail in Chapter 27,<br />

“Building User Authentication <strong>and</strong> Personalization.”<br />

Using Basic Authentication<br />

Fortunately, authenticating users is a common task, so authentication facilities are built<br />

into HTTP. Scripts or web servers can request authentication from a web browser.The<br />

web browser is then responsible for displaying a dialog box or similar device to obtain<br />

required information from the user.

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

Saved successfully!

Ooh no, something went wrong!