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 User Authentication<br />

591<br />

Listing 27.16<br />

Continued<br />

set passwd = sha1('".$new_password."')<br />

where username = '".$username."'");<br />

if (!$result) {<br />

throw new Exception('Password could not be changed.');<br />

} else {<br />

return true; // changed successfully<br />

}<br />

}<br />

This function checks that the old password supplied was correct, using the login()<br />

function that you have already looked at. If it’s correct, the function will connect to the<br />

database <strong>and</strong> update the password to the new value.<br />

Resetting Forgotten Passwords<br />

In addition to changing passwords, you need to deal with the common situation in<br />

which a user has forgotten her password. On the front page, login.php, you provide a<br />

link, marked Forgotten your password?, for users in this situation.This link takes users<br />

to the script called forgot_form.php, which uses the output functions to display a form,<br />

as shown in Figure 27.8.<br />

Figure 27.8 The forgot_form.php script supplies a form in which users<br />

can ask to have their passwords reset <strong>and</strong> sent to them.

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

Saved successfully!

Ooh no, something went wrong!