02.06.2013 Views

Pro PHP and jQuery by Jason Lengstorf.pdf - Computer Science ...

Pro PHP and jQuery by Jason Lengstorf.pdf - Computer Science ...

Pro PHP and jQuery by Jason Lengstorf.pdf - Computer Science ...

SHOW MORE
SHOW LESS

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

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

}<br />

try<br />

{<br />

WHERE<br />

`user_name` = :uname<br />

LIMIT 1";<br />

$stmt = $this->db->prepare($sql);<br />

$stmt->bindParam(':uname', $uname, PDO::PARAM_STR);<br />

$stmt->execute();<br />

$user = array_shift($stmt->fetchAll());<br />

$stmt->closeCursor();<br />

}<br />

catch ( Exception $e )<br />

{<br />

die ( $e->getMessage() );<br />

}<br />

/*<br />

* Fails if username doesn't match a DB entry<br />

*/<br />

if ( !isset($user) )<br />

{<br />

return "Your username or password is invalid.";<br />

}<br />

// finish processing...<br />

CHAPTER 6 ■ PASSWORD PROTECTION SENSITIVE ACTIONS AND AREAS<br />

Now the user’s data is stored in the variable $user (or the method failed because no match was<br />

found for the supplied username in the users table).<br />

Finishing Steps 5-7 completes the method; do this <strong>by</strong> adding the following bold code:<br />

public function processLoginForm()<br />

{<br />

/*<br />

* Fails if the proper action was not submitted<br />

*/<br />

if ( $_POST['action']!='user_login' )<br />

{<br />

return "Invalid action supplied for processLoginForm.";<br />

}<br />

/*<br />

* Escapes the user input for security<br />

*/<br />

$uname = htmlentities($_POST['uname'], ENT_QUOTES);<br />

$pword = htmlentities($_POST['pword'], ENT_QUOTES);<br />

/*<br />

* Retrieves the matching info from the DB if it exists<br />

*/<br />

$sql = "SELECT<br />

205

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

Saved successfully!

Ooh no, something went wrong!