11.07.2015 Views

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

www.it-ebooks.infoCHAPTER 14 • AUTHENTICATING YOUR USERSListing 14-4. Authenticating a User Against a Flat File Login Repository// If not authorized, display authentication prompt or 401 errorif (! $authorized) {header('WWW-Authenticate: Basic Realm="Secret Stash"');header('HTTP/1.0 401 Unauthorized');print('You must provide the proper credentials!');exit;}// restricted material goes here...Although the file-based authentication system works well for relatively small, static authenticationlists, this strategy can quickly become inconvenient when you’re handling a large number of users; whenusers are regularly being added, deleted, and modified; or when you need to incorporate anauthentication scheme into a larger information infrastructure such as a preexisting user table. Suchrequirements are better satisfied by implementing a database-based solution. The following sectiondemonstrates just such a solution, using a database to store authentication pairs.Database-Based AuthenticationOf all the various authentication methodologies discussed in this chapter, implementing a databasedrivensolution is the most powerful because it not only enhances administrative convenience andscalability, but also can be integrated into a larger database infrastructure. For purposes of this example,the data store is limited to three fields: a primary key, a username, and a password. These columns areplaced into a table called logins, shown in Listing 14-5.■ Note If you’re unfamiliar with <strong>MySQL</strong> and are confused by the syntax found in this example, consider reviewingthe material found in Chapter 30.309

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

Saved successfully!

Ooh no, something went wrong!