Chapter 4: String Manipulation and Regular Expressions
Chapter 5: Reusing Code and Writing Functions
Chapter 6: Object-Oriented PHP
Part II: Using MySQL
Chapter 7: Designing Your Web Database
Chapter 8: Creating Your Web Database
Chapter 9: Working with Your MySQL Database
Chapter 10: Accessing Your MySQL Database from the Web with PHP
Chapter 11: Advanced MySQL
Part III: E-commerce and Security
Chapter 12: Running an E-commerce Site
Chapter 13: E-commerce Security Issues
Chapter 14: Implementing Authentication with PHP and MySQL
Chapter 15: Implementing Secure Transactions with PHP and MySQL
Part IV: Advanced PHP Techniques
Chapter 16: Interacting with the File System and the Server
Chapter 17: Using Network and Protocol Functions
Chapter 18: Managing the Date and Time
Chapter 19: Generating Images
Chapter 20: Using Session Control in PHP
Chapter 21: Other Useful Features
Part V: Building Practical PHP and MySQL Projects
Chapter 22: Using PHP and MySQL for Large Projects
Chapter 23: Debugging
Chapter 24: Building User Authentication and Personalization
Chapter 25: Building a Shopping Cart
Chapter 26: Building a Content Management System
Chapter 27: Building a Web-Based Email Service
Chapter 28: Building a Mailing List Manager
Chapter 29: Building Web Forums
Chapter 30: Generating Personalized Documents in Portable Document Format (PDF)
Chapter 31: Connecting to Web Services with XML and SOAP
Part VI: Appendixes
Appendix A: Installing PHP and MySQL
Appendix B: Web Resources
Index
What’s On the CD-ROM?
Nội dung
602 Chapter 27 Building a Web-Based Email Service existing record.This function is in the output_fns.php library; it simply outputs HTML so we will not go through it here. The function that retrieves any existing accounts is get_accounts(),from the mail_fns.php library.This function is shown in Listing 27.4. Listing 27.4 get_accounts() Function from mail_fns.php—Function to Retrieve All the Account Details for a Particular User function get_accounts($auth_user) { $list = array(); if(db_connect()) { $query = "select * from accounts where username = '$auth_user'"; $result = mysql_query($query); if($result) { while($settings = mysql_fetch_array($result)) array_push( $list, $settings); } else return false; } return $list; } As you can see, this function connects to the database, retrieves all the accounts for a particular user, and returns them as an array. Creating a New Account If a user fills out the account form and clicks the Save Changes button, the store-set- tings action will be activated. Let’s look at the event handling code for this from index.php. In the preprocessing stage, we execute the following code: case 'store-settings' : { store_account_settings($HTTP_SESSION_VARS['auth_user'], $HTTP_POST_VARS); break; } The store_account_settings() function writes the new account details into the data- base.The code for this function is shown in Listing 27.5. Listing 27.5 store_account_settings() Function from mail_fns.php—Function to Save New Account Details for a User function store_account_settings($auth_user, $settings) { if(!filled_out($settings)) 33 525x ch27 1/24/03 2:56 PM Page 602 603 Setting Up Accounts { echo 'All fields must be filled in. Try again.<br /><br />'; return false; } else { if($settings['account']>0) $query = "update accounts set server = '$settings[server]', port = $settings[port], type = '$settings[type]', remoteuser = '$settings[remoteuser]', remotepassword = '$settings[remotepassword]' where accountid = $settings[account] and username = '$auth_user'"; else $query = "insert into accounts values ('$auth_user', '$settings[server]', $settings[port], '$settings[type]', '$settings[remoteuser]', '$settings[remotepassword]', NULL)"; if(db_connect() && mysql_query($query)) { return true; } else { echo 'could not store changes.<br /><br /><br /><br /><br /><br />'; return false; } } } As you can see, two choices within this function correspond to inserting a new account or updating an existing account.The function executes the appropriate query to save the account details. After storing the account details, we go back to index.php, to the main body stage: case 'store-settings' : case 'account-setup' : case 'delete-account' : { display_account_setup($HTTP_SESSION_VARS['auth_user']); break; } As you can see, we then execute the display_account_setup() function as before to list the user’s account details.The newly added account will now be included. Listing 27.5 Continued 33 525x ch27 1/24/03 2:56 PM Page 603 604 Chapter 27 Building a Web-Based Email Service Modifying an Existing Account The process for modifying an existing account is very similar.The user can change the account details and click the Save Changes button. Again this will trigger the store-settings action, but this time it will update the account details instead of insert- ing them. Deleting an Account To delete an account, the user can click the Delete Account button that is shown under each account listing.This activates the delete-account action. In the preprocessing section of the index.php script, we will execute the following code: case 'delete-account' : { delete_account($HTTP_SESSION_VARS['auth_user'], $account); break; } This code calls the delete_account() function.The code for this function is shown in Listing 27.6. Deleting accounts needs to be handled before the header because a choice of which account to use is inside the header.The account list needs to be updated before this can be correctly drawn. Listing 27.6 delete_account() Function from mail_fns.php—Function to Delete a Single Account’s Details function delete_account($auth_user, $accountid) { //delete one of this user's account from the DB $query = "delete from accounts where accountid='$accountid' and username ='$auth_user'"; if(db_connect()) { $result = mysql_query($query); } return $result; } After execution returns to index.php, the body stage will run the following code: case 'store-settings' : case 'account-setup' : case 'delete-account' : { display_account_setup($HTTP_SESSION_VARS['auth_user']); break; } 33 525x ch27 1/24/03 2:56 PM Page 604 605 Reading Mail Yo uwill recognize this as the same code we ran before—it just displays the list of the user’s accounts. Reading Mail After the user has set up some accounts, we can move on to the main game: connecting to these accounts and reading mail. Selecting an Account We need to select one of the user’s accounts to read mail from.The currently selected account is stored in the $selected_account session variable. If the user has a single account registered in the system, it will be automatically selected when he logs in, as follows: if(number_of_accounts($HTTP_SESSION_VARS['auth_user'])==1) { $accounts = get_account_list($HTTP_SESSION_VARS['auth_user']); $HTTP_SESSION_VARS['selected_account'] = $accounts[0]; } The number_of_accounts() function, from mail_fns.php, is used to work out whether the user has more than one account.The get_account_list() function retrieves an array of the names of the user’s accounts. In this case there is exactly one, so we can access it as the array’s 0 value. The number_of_accounts() function is shown in Listing 27.7. Listing 27.7 number_of_accounts() Function from mail_fns.php—Function to Work Out How Many Accounts a User Has Registered function number_of_accounts($auth_user) { // get the number of accounts that belong to this user $query = "select count(*) from accounts where username = '$auth_user'"; if(db_connect()) { $result = mysql_query($query); if($result) return mysql_result($result, 0, 0); } return 0; } The get_account_list() function is similar to the get_accounts() function we looked at before except that it only retrieves the account names. 33 525x ch27 1/24/03 2:56 PM Page 605 606 Chapter 27 Building a Web-Based Email Service If a user has multiple accounts registered, he will need to select one to use. In this case, the headers will contain a SELECT that lists the available mailboxes. Choosing the appro- priate one will automatically display the mailbox for that account.You can see this in Figure 27.5. Figure 27.5 After the localhost account is selected from the SELECT box, the mail from that account is downloaded and displayed. This SELECT option is generated in the do_html_header() function from output_fns.php, as shown in the following code fragment: // include the account select box only if the user has more than one account if(number_of_accounts($auth_user)>1) { echo '<form target="index.php?action=open-mailbox" method="post">'; echo '<td bgcolor="#ff6600" align="right" valign="middle">'; display_account_select($auth_user, $selected_account); echo '</td>'; echo '</form>'; } We have generally avoided discussing the HTML used in the examples in this book, but the HTML generated by the function display_account_select() bears a visit. Depending on the accounts the current user has, display_account_select() will generate HTML like this: 33 525x ch27 1/24/03 2:56 PM Page 606 . accounts for a particular user, and returns them as an array. Creating a New Account If a user fills out the account form and clicks the Save Changes button, the store-set- tings action will be activated the account details, we go back to index .php, to the main body stage: case 'store-settings' : case 'account-setup' : case 'delete-account' : { display_account_setup($HTTP_SESSION_VARS['auth_user']); break; } As. be activated. Let’s look at the event handling code for this from index .php. In the preprocessing stage, we execute the following code: case 'store-settings' : { store_account_settings($HTTP_SESSION_VARS['auth_user'],