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.

Using mod_auth_mysql Authentication<br />

407<br />

6. Add a line to your httpd.conf file to give mod_auth_mysql the parameters it<br />

needs to connect to <strong>MySQL</strong>.The directive will look like<br />

Auth_<strong>MySQL</strong>_Info hostname user password<br />

The easiest way to check whether your compilation worked is to see whether Apache<br />

will start.To start Apache type<br />

/usr/local/apache/bin/apachectl startssl<br />

If it starts with the Auth_<strong>MySQL</strong>_Info directive in the httpd.conf file,<br />

mod_auth_mysql was successfully added.<br />

Using mod_auth_mysql<br />

After you have successfully installed the mod_auth_mysql module, using it is no harder<br />

than using mod_auth. Listing 17.9 shows a sample .htaccess file that will authenticate<br />

users with encrypted passwords stored in the database created earlier in this chapter.<br />

Listing 17.9 .htaccess— This .htaccess File Authenticates Users Against a<br />

<strong>MySQL</strong> Database<br />

ErrorDocument 401 /chapter17/rejection.html<br />

AuthName “Realm Name”<br />

AuthType Basic<br />

Auth_<strong>MySQL</strong>_DB auth<br />

Auth_<strong>MySQL</strong>_Encryption_Types <strong>MySQL</strong><br />

Auth_<strong>MySQL</strong>_Password_Table authorized_users<br />

Auth_<strong>MySQL</strong>_Username_Field name<br />

Auth_<strong>MySQL</strong>_Password_Field password<br />

require valid-user<br />

You can see that much of Listing 17.9 is the same as Listing 17.7.You still specify an<br />

error document to display in the case of error 401 (when authentication fails).You again<br />

specify basic authentication <strong>and</strong> give a realm name. As in Listing 17.7, you allow any<br />

valid, authenticated user access.<br />

Because we are using mod_auth_mysql <strong>and</strong> did not want to use all the default settings,<br />

we used some directives to specify how this should work. Auth_<strong>MySQL</strong>_DB,<br />

Auth_<strong>MySQL</strong>_Password_Table, Auth_<strong>MySQL</strong>_Username_Field, <strong>and</strong><br />

Auth_<strong>MySQL</strong>_Password_Field specify the name of the database, the table, the username<br />

field, <strong>and</strong> the password field, respectively.

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

Saved successfully!

Ooh no, something went wrong!