Microsoft Word - Sam's Teach Yourself MySQL in 21 Days - SAMS.doc

(singke) #1

Logging in a User


The next part you're going to tackle is how to handle a user logging in. The task here is to get the username
and password and somehow retain this identity throughout a user's session. There are several ways you
could handle this. You could use a cookie to pass this value on to each page that needs the log in variables.
There are a couple of gotchas here. What if the user has cookies turned off? Then you're stuck. The best
way to handle this is to pass these values along using a field in a Web page. Because users have
to log in to get to a certain page, you could dynamically create that page, passing along their information in


fields. For example, a valid customer clicks the Post Ad link. This link takes him or her to the login
Web page. A Perl Script runs that validates his or her username and password against the MySQL database
and then creates the PostAd Web page. In the HTML of this Web page, you could have two
fields—one for the username and one for the password. These fields are just like any of the other fields in a
form. When the customer is done creating his or her ad, he or she submits the form to the Web server. You
use the hidden fields to gain access to the MySQL database. This may seem a little confusing, so look at the
code to help clear things up.
A person wants to post an ad. According to your business rules, this person must be a member. To
check his or her ID, he or she must log in. Your Web page people created the LogIn.html page
shown in Figure 21.4.

Figure 21.4 The login form.
Listing 21.3 shows code that created the LogIn.html Web page.
Listing 21.3 Logging in a User




Please Log In





<table border="0" width="294" height="21"

Free download pdf