PHP 5 Advanced: Visual QuickPro Guide/Page 3
Security Techniques: Part 2
Authentication with PEAR Auth
One of the more common elements in today's Web sites is an authentication system: users register with a site, they log in to gain access to some parts, and restricted pages allow or deny access accordingly. Such systems aren't hard to implement—I've done so in some of my other books—but here I'd like to look at what PEAR has to offer.
The PEAR Auth package provides a really easy, yet customizable authentication system. To show it off, I'll start with one very simple example. This will mostly demonstrate its basic usage. Then I'll show how to customize the authentication system to fit it into a larger application. For both examples, you'll need to install the PEAR Auth package. Because the authentication information is stored in a database, the PEAR DB package must also be installed. If you're not familiar with PEAR and its installation, see Chapter 12, "Using PEAR," or http://pear.php.net.TIP
- For these examples I will put both the authentication code and the restricted page data in the same file. In a larger Web site, you'll likely want to separate the authentication code into its own file, which is then included by any file that requires authentication.
This first, simple authentication example shows how easily you can implement authentication in a page. I'll run through the syntax and concepts first, and then create a script that executes it all.
To begin, require the Auth class:
Next, you'll need to define a function that creates a login form. This function will be called when an unauthorized user is trying to access a page. The form should use the POST method and have inputs called username and password.
Then, for database-driven authentication, which is the norm, you'll need to create a "DSN" within an options array. DSN stands for data source name. It's just a string of information that indicates the type of database application being used, the username, password, and hostname to connect as, and the database to select. That code might be:
Now that those two things have been defined—the function that makes the login form and the DSN—you can create an object of
Auth type. Provide this object three arguments: the type of authentication back end to use (e.g., database or file), the options (that correspond to the authentication type), and the name of the login function:
The DB option tells Auth to use the PEAR DB package. If you wanted to use a file system instead, you would use File as the first argument and the name of the file as the second.
Now, start the authentication process:
From there, you can check if a user is authenticated by calling the
And that's simple authentication in a nutshell! This next example will implement all this. It will also invoke the
addUser() method to add a new authenticated user, which can then be used for logging in. One last note: this example will make use of a database called
auth, which must be created prior to writing this script. It should have a table called
auth, defined like so:
Be certain that you've created this database and table (Figure 4.8), and that you have created a MySQL user that has access to them, prior to going any further.
Figure 4.8 Creating the database and table required by the simple authentication example.
To perform simple authentication
- Begin a new PHP script in your text editor or IDE (Script 4.3).
Because Auth relies on sessions (it'll start the sessions for you), it's best to do as much as you can before sending any HTML to the Web browser. So I'll write most of the authentication code, and only then begin the HTML page.
Script 4.3. Using PEAR Auth and a MySQLtable, this script enforces authentication.
- Include the Auth class.
If you haven't installed PEAR Auth yet, do so now. See the PEAR manual for instructions.
- Define the function that creates the login form.
- Create the options array.
This code says that a connection should be made to a MySQL database called auth, using username as the username, password as the password, and localhost as the host.
- Create the
Add a new user and complete the PHP section.
addUser()functions takes the username as its first argument and the password as the second. This record will be added to the database as soon as the script is first run (Figure 4.9). Because the
usernamecolumn in the table is defined as a primary key, MySQL will never allow a second user with the name of me to be added.
Figure 4.9: One user has been added to the table. The password is encrypted using the
In a real application, you'd have a registration process that would just end up calling this function in the end.
- Add the initial HTML code.
- Start the authentication.
- Display different messages based upon the authentication status.
When a user first comes to this page, and
$auth->checkAuth()is false, they'll see the login form plus this second message (Figure 4.10). After logging in with a valid username/password combination, they'll see this first message (Figure 4.11)
Figure 4.10: When first arriving at this page, or after an unsuccessful login attempt, a user sees this.
Figure 4.11: The result after successfully logging in.
- Complete the page.
Save the file as
login.php, place it in your Web directory, and test in your Web browser.
Use me as the username and mypass as the password.
If some of your Web site's pages do not require authentication but could still acknowledge logged-in users, that's an option with Auth, too. To make authentication optional, add a fourth parameter when creating the
To limit aspects of a page to authenticated users, invoke the