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.

392 Chapter 17 Implementing Authentication with <strong>PHP</strong> <strong>and</strong> <strong>MySQL</strong><br />

Each computer connected to the Internet has a unique IP address. From a visitor’s<br />

IP address, you might be able to deduce a little about her.You can find out who owns<br />

an IP <strong>and</strong> sometimes make a reasonable guess as to a visitor’s geographic location.<br />

Some addresses are more useful than others. Generally, people with permanent Internet<br />

connections have a permanent address. Customers dialing into an ISP usually get only<br />

the temporary use of one of the ISP’s addresses.The next time you see that address, it<br />

might be used by a different computer, <strong>and</strong> the next time you see that visitor, she will<br />

likely be using a different IP address. IP addresses are not as useful for identifying people<br />

as they might at first glance seem.<br />

Fortunately for web users, none of the information that their browsers give out<br />

identifies them. If you want to know a visitor’s name or other details, you will have to<br />

ask her.<br />

Many websites provide compelling reasons to get users to provide their details.The<br />

New York Times newspaper (http://www.nytimes.com) provides its content free, but only<br />

to people willing to provide details such as name, sex, <strong>and</strong> total household income. Nerd<br />

news <strong>and</strong> discussion site Slashdot (http://www.slashdot.org) allows registered users to<br />

participate in discussions under a nickname <strong>and</strong> customize the interface they see. Most<br />

e-commerce sites record their customers’ details when they make their first order.This<br />

means that a customer is not required to type her details every time.<br />

Having asked for <strong>and</strong> received information from your visitor, you need a way to associate<br />

the information with the same user the next time she visits. If you are willing to<br />

make the assumption that only one person visits your site from a particular account on a<br />

particular machine <strong>and</strong> that each visitor uses only one machine, you could store a cookie<br />

on the user’s machine to identify the user.<br />

This arrangement is certainly not true for all users. Many people share a computer,<br />

<strong>and</strong> many people use more than one computer. At least some of the time, you need to<br />

ask a visitor who she is again. In addition to asking who a user is, you also need to ask<br />

her to provide some level of proof that she is who she claims to be.<br />

As discussed in Chapter 15,“E-commerce Security Issues,” asking a user to prove her<br />

identity is called authentication.The usual method of authentication used on websites is<br />

asking visitors to provide a unique login name <strong>and</strong> a password. Authentication is usually<br />

used to allow or disallow access to particular pages or resources, but can be optional, or<br />

used for other purposes such as personalization.<br />

Implementing Access Control<br />

Simple access control is not difficult to implement.The code shown in Listing 17.1<br />

delivers one of three possible outputs. If the file is loaded without parameters, it will display<br />

an HTML form requesting a username <strong>and</strong> password.This type of form is shown in<br />

Figure 17.1.

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

Saved successfully!

Ooh no, something went wrong!