Specifications

The function has two clever bits. The first is that, if we reach the end of the file while looking
for a word, we go back to the beginning:
if (feof($fp))
fseek($fp, 0); // if at end, go to start
The second is that we can seek for a word of a particular lengthwe check each word that we
pull from the dictionary, and, if it is not between $min_length and $max_length, we keep
searching.
Back in reset_password(), after we have generated a new password, we update the database
to reflect this, and return the new password back to the main script. This will then be passed on
to notify_password(), which will email it to the user.
Lets have a look at the notify_password() function, shown in Listing 24.20.
LISTING 24.20 The notify_password() Function from user_auth_fns.phpThis Function
Emails a Reset Password to a User
function notify_password($username, $password)
// notify the user that their password has been changed
{
if (!($conn = db_connect()))
return false;
$result = mysql_query(“select email from user
where username=’$username’”);
if (!$result)
return false; // not changed
else if (mysql_num_rows($result)==0)
return false; // username not in db
else
{
$email = mysql_result($result, 0, “email”);
$from = “From: support@phpbookmark \r\n”;
$mesg = “Your PHPBookmark password has been changed to $password \r\n”
.”Please change it next time you log in. \r\n”;
if (mail($email, “PHPBookmark login information”, $mesg, $from))
return true;
else
return false;
}
}
In this function, given a username and new password, we simply look up the email address for
that user in the database, and use PHPs mail() function to send it to her.
Building User Authentication and Personalization
C
HAPTER 24
24
AUTHENTICATION
AND
PERSONALIZATION
525
30 7842 ch24 3/6/01 3:34 PM Page 525