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 Login<br />

705<br />

Listing 30.4<br />

}<br />

}<br />

}<br />

Continued<br />

return true;<br />

} else {<br />

return false;<br />

}<br />

} else {<br />

echo "Could not store new account.";<br />

return false;<br />

}<br />

This function first checks that the user has filled in the required details. If this is okay, the<br />

function will then either create a new user or update the account details if the user<br />

already exists. A user can update only the account details of the user he is logged in as.<br />

The logged-in user’s identity is checked using the get_email() function, which<br />

retrieves the email address of the user who is currently logged in.We return to this function<br />

later because it uses session variables that are set up when the user logs in.<br />

Logging In<br />

If a user fills in the login form you saw in Figure 30.4 <strong>and</strong> clicks on the Log In button,<br />

she will enter the index.php script with the email <strong>and</strong> password variables set.This activates<br />

the login code, which is in the preprocessing stage of the script, as follows:<br />

// need to process log in or out requests before anything else<br />

if(($_POST['email']) && ($_POST['password'])) {<br />

$login = login($_POST['email'], $_POST['password']);<br />

if($login == 'admin') {<br />

$status .= "<br />

".get_real_name($_POST['email'])."<br />

logged in successfully as<br />

Administrator.";<br />

$_SESSION['admin_user'] = $_POST['email'];<br />

} else if($login == 'normal') {<br />

$status .= "<br />

".get_real_name($_POST['email'])."<br />

logged in successfully.";<br />

$_SESSION['normal_user'] = $_POST['email'];<br />

} else {<br />

$status .= "Sorry, we could

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

Saved successfully!

Ooh no, something went wrong!