Using the HTML <form> Environment

The final script in our gift registry application is the first the user sees. It shows the guest a login <form> in which he can enter his username and password. The source of the script is shown in Example 11-5, and its output is shown in Figure 11-2.

When the guest has entered his credentials and clicks on the Log In button, the script is re-requested to validate the credentials. If the username already exists in the people table, the password is validated. If the password matches, the session variable $user is registered with the guest’s people_id as its value. Then the header( ) function with the Location: parameter is used to redirect the browser to the presents.php script, along with a welcome message. If the password doesn’t match, then the script is re-requested, and an error message is shown.

The initial login screen of the gift registry
Figure 11-2. The initial login screen of the gift registry

If the username doesn’t exist, it is added to the people table along with the user-supplied password. This allows a new guest to use the system, decide on his own username and password, and log in. For applications in which security is important, the usernames and passwords would be added to the people table by an administrator.

Passwords are encrypted. The PHP library function crypt( ) is a one-way encryption function that takes two parameters: the string to be encrypted and a salt. The salt ...

Get Managing & Using MySQL, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.