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.

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

tells Apache what document to display for visitors who fail to authenticate (HTTP error<br />

number 401).You can use other ErrorDocument directives to provide your own pages<br />

for other HTTP errors such as 404.The syntax is<br />

ErrorDocument error_number URL<br />

For a page to h<strong>and</strong>le error 401, it is important that the URL given is publicly available.<br />

It would not be very useful in providing a customized error page to tell people that their<br />

authorization failed if the page is locked in a directory in which they need to successfully<br />

authenticate to see.<br />

The line<br />

AuthUserFile /home/book/.htpass<br />

tells Apache where to find the file that contains authorized users’ passwords.This file is<br />

often named .htpass, but you can give it any name you prefer. It is not important what<br />

you call this file, but it is important where you store it. It should not be stored within<br />

the web tree—somewhere that people can download it via the web server.The sample<br />

.htpass file is shown in Listing 17.8.<br />

As well as specifying individual users who are authorized, it is possible to specify that<br />

only authorized users who fall into specific groups may access resources.We chose not<br />

to, so the line<br />

AuthGroupFile /dev/null<br />

sets the AuthGroupFile to point to /dev/null, a special file on Unix systems that is<br />

guaranteed to be null.<br />

Like the <strong>PHP</strong> example, to use HTTP authentication, you need to name the realm as<br />

follows:<br />

AuthName “Realm-Name”<br />

You can choose any realm name you prefer, but bear in mind that the name will be<br />

shown to your visitors.To make it obvious that the name in the example should be<br />

changed, we named ours “Realm-Name”.<br />

Because a number of different authentication methods are supported, you need to<br />

specify which authentication method you are using. Here, you use Basic authentication,<br />

as specified by this directive:<br />

AuthType Basic<br />

You also need to specify who is allowed access.You could specify particular users, particular<br />

groups, or as we have done, simply allow any authenticated user access.The line<br />

require valid-user<br />

specifies that any valid user is to be allowed access.

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

Saved successfully!

Ooh no, something went wrong!