Tải bản đầy đủ (.pdf) (5 trang)

PHP and MySQL Web Development - P132 pdf

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (57.78 KB, 5 trang )

627
Script Architecture
Remember that you can execute this SQL by typing
mysql -u root -p < create_database.sql
You will need to supply your root password. (You could, of course, execute this script via
any MySQL user with the appropriate privileges; we have just used root here for sim-
plicity.) You should change the password for the mlm user and the administrator in your
script before running it.
Some of the fields in this database require a little further explanation, so let’s briefly
run through them.
The lists table contains a listid and listname. It also contains a blurb,which is a
description of what the list is about.
The subscribers table contains email addresses (email) and names (realname)of the
subscribers. It also stores their password and a flag (admin) to indicate whether or not
this user is an administrator.We will also store the type of mail they prefer to receive in
mimetype.This can be either H for HTML or T for text.
The sublists table contains email addresses (email) from the subscribers table and
listids from the lists table.
The mail table contains information about each email message that is sent through
the system. It stores a unique id (mailid), the address the mail is sent from (email), the
subject line of the email (
subject), and the listid of the list it has been sent to or will
be sent to.The actual text or HTML of the message could be a large file, so we will
store the archive of the actual messages outside the database.We will also track some
general status information: whether the message has been sent (status), when it was
sent (sent), and a timestamp to show when this record was last modified (modified).
Finally, we use the images table to track any images associated with HTML messages.
Again, these images can be large, so we will store them outside the database for efficien-
cy. Instead, we will track the mailid they are associated with, the path to the location
where the image is actually stored, and the MIME type of the image (mimetype), for
example, image/gif.


The SQL shown previously also sets up a user for PHP to connect as, and an admin-
istrative user for the system.
Script Architecture
As in the last project, we have used an event-driven approach to this project.The back-
bone of the application is in the file index.php.This script has four main segments,
which are
1. Preprocessing: Do any processing that must be done before headers can be sent.
2. Set up and send headers: Create and send the start of the HTML page.
3. Perform action: Respond to the event that has been passed in.As in our last
example, the event is contained in the $action variable.
4. Send footers.
34 525x ch28 1/24/03 2:55 PM Page 627
628
Chapter 28 Building a Mailing List Manager
Almost all of the application’s processing is done in this file.The application also uses the
function libraries listed in Table 28.1, as mentioned previously.
The full listing of the index.php script is shown in Listing 28.2.
Listing 28.2 index.php—Main Application File for Pyramid-MLM
<?php
/**********************************************************************
* Section 1 : pre-processing
*********************************************************************/
include ('include_fns.php');
session_start();
$action = $HTTP_GET_VARS['action'];
$buttons = array();
//append to this string if anything processed before header has output
$status = '';
// need to process log in or out requests before anything else
if($HTTP_POST_VARS['email']&&$HTTP_POST_VARS['password'])

{
$login = login($HTTP_POST_VARS['email'], $HTTP_POST_VARS['password']);
if($login == 'admin')
{
$status .= "<p><b>".get_real_name($HTTP_POST_VARS['email']).
"</b> logged in"." successfully as <b>Administrator</b></p>
<br /><br /><br /><br /><br />";
$HTTP_SESSION_VARS['admin_user'] = $HTTP_POST_VARS['email'];
}
else if($login == 'normal')
{
$status .= "<p><b>".get_real_name($HTTP_POST_VARS['email'])."</b> logged in"
." successfully.</p><br /><br />";
$HTTP_SESSION_VARS['normal_user'] = $HTTP_POST_VARS['email'];
}
else
{
$status .= "<p>Sorry, we could not log you in with that
email address and password.</p><br />";
}
}
if($action == 'log-out')
34 525x ch28 1/24/03 2:55 PM Page 628
629
Script Architecture
{
unset($action);
unset($HTTP_SESSION_VARS);
session_destroy();
}

/**********************************************************************
* Section 2: set up and display headers
*********************************************************************/
// set the buttons that will be on the tool bar
if(check_normal_user())
{
// if a normal user
$buttons[0] = 'change-password';
$buttons[1] = 'account-settings';
$buttons[2] = 'show-my-lists';
$buttons[3] = 'show-other-lists';
$buttons[4] = 'log-out';
}
else if(check_admin_user())
{
// if an administrator
$buttons[0] = 'change-password';
$buttons[1] = 'create-list';
$buttons[2] = 'create-mail';
$buttons[3] = 'view-mail';
$buttons[4] = 'log-out';
$buttons[5] = 'show-all-lists';
$buttons[6] = 'show-my-lists';
$buttons[7] = 'show-other-lists';
}
else
{
// if not logged in at all
$buttons[0] = 'new-account';
$buttons[1] = 'show-all-lists';

$buttons[4] = 'log-in';
}
if($action)
{
// display header with application name and description of page or action
do_html_header('Pyramid-MLM - '.format_action($action));
}
Listing 28.2 Continued
34 525x ch28 1/24/03 2:55 PM Page 629
630
Chapter 28 Building a Mailing List Manager
else
{
// display header with just application name
do_html_header('Pyramid-MLM');
}
display_toolbar($buttons);
//display any text generated by functions called before header
echo $status;
/**********************************************************************
* Section 3: perform action
*********************************************************************/
// only these actions can be done if not logged in
switch ( $action )
{
case 'new-account' :
{
// get rid of session variables
session_destroy();
display_account_form();

break;
}
case 'store-account' :
{
if (store_account($HTTP_SESSION_VARS['normal_user'],
$HTTP_SESSION_VARS['admin_user'], $HTTP_POST_VARS))
$action = '';
if(!check_logged_in())
display_login_form($action);
break;
}
case 'log-in' :
case '':
{
if(!check_logged_in())
display_login_form($action);
break;
}
case 'show-all-lists' :
{
display_items('All Lists', get_all_lists(), 'information',
'show-archive','');
break;
Listing 28.2 Continued
34 525x ch28 1/24/03 2:55 PM Page 630
631
Script Architecture
}
case 'show-archive' :
{

display_items('Archive For '.get_list_name($HTTP_GET_VARS['id']),
get_archive($HTTP_GET_VARS['id']), 'view-html',
'view-text', '');
break;
}
case 'information' :
{
display_information($HTTP_GET_VARS['id']);
break;
}
}
//all other actions require user to be logged in
if(check_logged_in())
{
switch ( $action )
{
case 'account-settings' :
{
display_account_form(get_email(),
get_real_name(get_email()), get_mimetype(get_email()));
break;
}
case 'show-other-lists' :
{
display_items('Unsubscribed Lists',
get_unsubscribed_lists(get_email()), 'information',
'show-archive', 'subscribe');
break;
}
case 'subscribe' :

{
subscribe(get_email(), $HTTP_GET_VARS['id']);
display_items('Subscribed Lists', get_subscribed_lists(get_email()),
'information', 'show-archive', 'unsubscribe');
break;
}
case 'unsubscribe' :
{
unsubscribe(get_email(), $HTTP_GET_VARS['id']);
display_items('Subscribed Lists', get_subscribed_lists(get_email()),
'information', 'show-archive', 'unsubscribe');
Listing 28.2 Continued
34 525x ch28 1/24/03 2:55 PM Page 631

×