13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

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

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

Implementing Authentication with Session Control<br />

517<br />

Table 23.1 Continued<br />

Option Name Default Effect<br />

session.name <strong>PHP</strong>SESSID Sets the name of the session that is used as<br />

the cookie name on a user’s system.<br />

session.save_h<strong>and</strong>ler files Defines where session data is stored.You can<br />

set this option to point to a database, but<br />

you have to write your own functions.<br />

session.save_path "" Sets the path where session data is stored.<br />

More generally, sets the argument passed<br />

to the save h<strong>and</strong>led <strong>and</strong> defined by<br />

session.save_h<strong>and</strong>ler.<br />

session.use_cookies 1 (enabled) Configures sessions to use cookies on the<br />

client side.<br />

session.cookie_secure 0 (disabled) Specifies whether cookies should only be<br />

sent over secure connections.<br />

session.hash_function 0 (MD5) Allows you to specify the hash algorithm<br />

used to generate the session IDs.“0” means<br />

MD5 (128 bits) <strong>and</strong> ‘l’ means SHA-1 (160<br />

bits). This configuration setting was introduced<br />

in <strong>PHP</strong> 5.<br />

Implementing Authentication with Session<br />

Control<br />

Finally, we look at a more substantial example using session control.<br />

Possibly the most common use of session control is to keep track of users after they<br />

have been authenticated via a login mechanism. In this example, you combine authentication<br />

from a <strong>MySQL</strong> database with use of sessions to provide this functionality.This<br />

functionality forms the basis of the project in Chapter 27,“Building User Authentication<br />

<strong>and</strong> Personalization,” <strong>and</strong> will be reused in the other projects.You will reuse the authentication<br />

database you set up in Chapter 17,“Implementing Authentication with <strong>PHP</strong> <strong>and</strong><br />

<strong>MySQL</strong>.”You can check Listing 17.3 in that chapter for details of the database.<br />

The example consists of three simple scripts.The first, authmain.php, provides a login<br />

form <strong>and</strong> authentication for members of the website.The second, members_only.php,<br />

displays information only to members who have logged in successfully.The third,<br />

logout.php, logs out a member.<br />

To underst<strong>and</strong> how this example works, look at Figure 23.4, which shows the initial<br />

page displayed by authmain.php.

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

Saved successfully!

Ooh no, something went wrong!