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.

402 Chapter 17 Implementing Authentication with <strong>PHP</strong> <strong>and</strong> <strong>MySQL</strong><br />

In this case, the user will see an interface somewhat different from the previous listings.This<br />

script does not provide an HTML form for login information.The user’s<br />

browser presents her with a dialog box. Some people see this as an improvement; others<br />

would prefer to have complete control over the visual aspects of the interface. A sample<br />

dialog box, in this instance provided from Firefox, is shown in Figure 17.4.<br />

Because the authentication is being assisted by features built into the browser, the<br />

browser chooses to exercise some discretion in how failed authorization attempts are<br />

h<strong>and</strong>led. Internet Explorer lets the user try to authenticate three times before displaying<br />

the rejection page. Firefox lets the user try an unlimited number of times, popping up a<br />

dialog box to ask,“Authorization failed. Retry?” between attempts. Firefox displays the<br />

rejection page only if the user clicks Cancel.<br />

As with the code given in Listings 17.1 <strong>and</strong> 17.2, you could include this code in<br />

pages you wanted to protect or automatically prepend it to every file in a directory.<br />

Figure 17.4<br />

The user’s browser is responsible for the appearance of the dialog<br />

box when using HTTP authentication.<br />

Using Basic Authentication with Apache’s<br />

.htaccess Files<br />

You can achieve similar results to the script in Listing 17.4 without writing a <strong>PHP</strong><br />

script.<br />

The Apache web server contains a number of different authentication modules that<br />

can be used to decide the validity of data entered by a user.The easiest to use is<br />

mod_auth, which compares name-password pairs to lines in a text file on the server.

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

Saved successfully!

Ooh no, something went wrong!