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.

Reading Mail<br />

671<br />

Reading Mail<br />

After the user has set up some accounts, you can move on to the main game: connecting<br />

to these accounts <strong>and</strong> reading mail.<br />

Selecting an Account<br />

The user needs to select one of his accounts to read mail from.The currently selected<br />

account is stored in the $selected_account session variable.<br />

If the user has a single account registered in the system, it will be automatically<br />

selected when he logs in, as follows:<br />

if(number_of_accounts($_SESSION['auth_user'])==1) {<br />

$accounts = get_account_list($_SESSION['auth_user']);<br />

$_SESSION['selected_account'] = $accounts[0];<br />

}<br />

The number_of_accounts() function, from mail_fns.php, works out whether the user<br />

has more than one account; this function is shown in Listing 29.7.The<br />

get_account_list() function retrieves an array of the user’s account IDs. In this case,<br />

there is exactly one, so you can access it as the array’s 0 value.<br />

Listing 29.7 number_of_accounts() Function from mail_fns.php—Function to<br />

Work Out How Many Accounts a User Has Registered<br />

function number_of_accounts($auth_user) {<br />

// get the number of accounts that belong to this user<br />

$query = "select count(*) from accounts where<br />

username = '".$auth_user."'";<br />

if($conn=db_connect()) {<br />

$result = $conn->query($query);<br />

if($result) {<br />

$row = $result->fetch_array();<br />

return $row[0];<br />

}<br />

}<br />

return 0;<br />

}<br />

The get_account_list() function is similar to the get_accounts() function you<br />

looked at before except that it retrieves only the account names.<br />

If a user has multiple accounts registered, he will need to select one to use. In this<br />

case, the headers contain a SELECT option that lists the available mailboxes. Choosing the<br />

appropriate one automatically displays the mailbox for that account.You can see this in<br />

Figure 29.5.

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

Saved successfully!

Ooh no, something went wrong!