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.

586 Chapter 27 Building User Authentication <strong>and</strong> Personalization<br />

The member.php script ends the page by displaying a menu with the<br />

display_user_menu() function. Some sample output as displayed by member.php is<br />

shown in Figure 27.6.<br />

Figure 27.6 The member.php script checks that a user is logged in, retrieves<br />

<strong>and</strong> displays his bookmarks, <strong>and</strong> gives him a menu of options.<br />

Let’s look at the login() <strong>and</strong> check_valid_user() functions a little more closely now.<br />

The login() function is shown in Listing 27.12.<br />

Listing 27.12 login()Function from user_auth_fns.php—This Function Checks a<br />

User’s Details Against the Database<br />

function login($username, $password) {<br />

// check username <strong>and</strong> password with db<br />

// if yes, return true<br />

// else throw exception<br />

// connect to db<br />

$conn = db_connect();<br />

// check if username is unique<br />

$result = $conn->query("select * from user<br />

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

<strong>and</strong> passwd = sha1('".$password."')");

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

Saved successfully!

Ooh no, something went wrong!