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.

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

Listing 27.19<br />

Continued<br />

}<br />

$word = fgets($fp, 80); // skip first word as it could be partial<br />

$word = fgets($fp, 80); // the potential password<br />

}<br />

$word = trim($word); // trim the trailing \n from fgets<br />

return $word;<br />

}<br />

To work, the get_r<strong>and</strong>om_word() function needs a dictionary. If you are using a Unix<br />

system, the built-in spell checker ispell comes with a dictionary of words, typically located<br />

at /usr/dict/words, as it is here, or at /usr/share/dict/words. If you don’t find it<br />

in one of these places, on most systems you can find yours by typing<br />

$ locate dict/words<br />

If you are using some other system or do not want to install ispell, don’t worry! You can<br />

download word lists as used by ispell from http://wordlist.sourceforge.net/.<br />

This site also has dictionaries in many other languages, so if you would like a r<strong>and</strong>om,<br />

say, Norwegian or Esperanto word, you can download one of those dictionaries instead.<br />

These files are formatted with each word on a separate line, separated by newlines.<br />

To get a r<strong>and</strong>om word from this file, you pick a r<strong>and</strong>om location between 0 <strong>and</strong> the<br />

filesize, <strong>and</strong> read from the file there. If you read from the r<strong>and</strong>om location to the next<br />

newline, you will most likely get only a partial word, so you skip the line you open the<br />

file to <strong>and</strong> take the next word as your word by calling fgets() twice.<br />

The function has two clever bits.The first is that, if you reach the end of the file<br />

while looking for a word, you go back to the beginning:<br />

if (feof($fp)) {<br />

fseek($fp, 0);<br />

}<br />

// if at end, go to start<br />

The second is that you can seek for a word of a particular length:You check each word<br />

that you pull from the dictionary, <strong>and</strong>, if it is not between $min_length <strong>and</strong><br />

$max_length, you keep searching. At the same time, you also dump words with apostrophes<br />

(single quotation marks) in them.You could escape them out when using the word,<br />

but just getting the next word is easier.<br />

Back in reset_password(), after you have generated a new password, you update the<br />

database to reflect this <strong>and</strong> return the new password to the main script.This is then<br />

passed on to notify_password(), which emails it to the user.The notify_password()<br />

function is shown in Listing 27.20.

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

Saved successfully!

Ooh no, something went wrong!