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.

Implementing User Authentication<br />

581<br />

Listing 27.7 filled_out() Function from data_valid_fns.php—This Function<br />

Checks That the Form Has Been Filled Out<br />

function filled_out($form_vars) {<br />

// test that each variable has a value<br />

foreach ($form_vars as $key => $value) {<br />

if ((!isset($key)) || ($value == '')) {<br />

return false;<br />

}<br />

}<br />

return true;<br />

}<br />

Listing 27.8 valid_email() Function from data_valid_fns.php—This Function<br />

Checks Whether an Email Address Is Valid<br />

function valid_email($address) {<br />

// check an email address is possibly valid<br />

if (ereg('^[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$', $address)) {<br />

return true;<br />

} else {<br />

return false;<br />

}<br />

}<br />

The function filled_out() expects to be passed an array of variables; in general, this is<br />

the $_POST or $_GET array. It checks whether the form fields are all filled out, <strong>and</strong><br />

returns true if they are <strong>and</strong> false if they are not.<br />

The valid_email() function uses a slightly more complex regular expression than the<br />

one developed in Chapter 4,“String Manipulation <strong>and</strong> Regular Expressions,” for validating<br />

email addresses. It returns true if an address appears valid <strong>and</strong> false if it does not.<br />

After you’ve validated the input data, you can actually try to register the user. If you<br />

look back at Listing 27.6, you can see that you do this as follows:<br />

register($username, $email, $passwd);<br />

// register session variable<br />

$_SESSION['valid_user'] = $username;<br />

// provide link to members page<br />

do_html_header('Registration successful');<br />

echo 'Your registration was successful. Go to the members page to start<br />

setting up your bookmarks!';<br />

do_html_url('member.php', 'Go to members page');<br />

// end page<br />

do_html_footer();

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

Saved successfully!

Ooh no, something went wrong!