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

Phát triển web với PHP và MySQL - p 72 ppt

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 (673.7 KB, 10 trang )

LISTING 28.8 Continued
$info = load_list_info($listid);
if($info)
{
echo “<h2>”.pretty($info[listname]).”</h2>”;
echo ‘<p>’.pretty($info[blurb]);
echo ‘<p>Number of subscribers:’ . $info[subscribers];
echo ‘<p>Number of messages in archive:’ . $info[archive];
}
}
The display_information() function uses two other functions to help it achieve its Web task:
the load_list_info() function and the pretty() function. The load_list_info() function
actually retrieves the data from the database. The pretty() function simply formats the data
from the database by stripping out slashes, turning newlines into HTML line breaks, and so on.
Let’s look briefly at the load_list_info() function. This function is in the mlm_fns.php
function library. The code for it is shown in Listing 28.9.
LISTING 28.9 load_list_info() Function from mlm_fns.php—This Function Builds an Array
of List Information
function load_list_info($listid)
{
if(!$listid)
return false;
if(!db_connect())
return false;
$query = “select listname, blurb from lists where listid = $listid”;
$result = mysql_query($query);
if(!$result)
{
echo “Cannot retrieve this list”;
return false;
}


$info = mysql_fetch_array($result);
$query = “select count(*) from sub_lists where listid = $listid”;
$result = mysql_query($query);
if($result)
{
$info[‘subscribers’] = mysql_result($result, 0, 0);
}
Building a Mailing List Manager
C
HAPTER 28
28
BUILDING A
MAILING LIST
MANAGER
685
34 7842 CH28 3/6/01 3:46 PM Page 685
LISTING 28.9 Continued
$query = “select count(*) from mail where listid = $listid
and status = ‘SENT’”;
$result = mysql_query($query);
if($result)
{
$info[‘archive’] = mysql_result($result, 0, 0);
}
return $info;
}
This function runs three database queries to collect the name and blurb for a list from the
lists table; the number of subscribers from the sub_lists table; and the number of newslet-
ters sent from the mail table.
Viewing List Archives

In addition to viewing the list blurb, users can look at all the mail that has been sent to a mail-
ing list by clicking on the Show Archive button. This activates the show-archive action, which
triggers the following code:
case ‘show-archive’ :
{
display_items(“Archive For “.get_list_name($id),
get_archive($id), ‘view-html’, ‘view-text’, ‘’);
break;
}
Again, this function uses the display_items() function to list out the various items of mail
that have been sent to the list. These items are retrieved using the
get_archive() function
from mlm_fns.php. This function is shown in Listing 28.10.
L
ISTING 28.10 get_archive() Function from mlm_fns.php—This Function Builds an Array
of Archived Newsletters for a Given List
function get_archive($listid)
{
//returns an array of the archived mail for this list
//array has rows like (mailid, subject)
$list = array();
$listname = get_list_name($listid);
Building Practical PHP and MySQL Projects
P
ART V
686
34 7842 CH28 3/6/01 3:46 PM Page 686
LISTING 28.10 Continued
$query = “select mailid, subject, listid from mail
where listid = $listid and status = ‘SENT’ order by sent”;

if(db_connect())
{
$result = mysql_query($query);
if(!$result)
{
echo “<p>Unable to get list from database - $query.”;
return false;
}
$num = mysql_numrows($result);
for($i = 0; $i<$num; $i++)
{
$row = array(mysql_result($result, $i, 0),
mysql_result($result, $i, 1), $listname, $listid);
array_push($list, $row);
}
}
return $list;
}
Again, this function gets the required information—in this case, the details of mail that has
been sent—from the database and builds an array suitable for passing to the display_items()
function.
Subscribing and Unsubscribing
On the list of mailing lists shown in Figure 28.7, each list has a button that enables users to
subscribe to it. Similarly, if users use the Show My Lists option to see the lists to which they
are already subscribed, they will see an Unsubscribe button next to each list.
These buttons activate the subscribe and unsubscribe actions, which trigger the following two
pieces of code, respectively:
case ‘subscribe’ :
{
subscribe(get_email(), $id);

display_items(“Subscribed Lists”, get_subscribed_lists(get_email()),
‘information’, ‘show-archive’, ‘unsubscribe’);
break;
}
case ‘unsubscribe’ :
{
Building a Mailing List Manager
C
HAPTER 28
28
BUILDING A
MAILING LIST
MANAGER
687
34 7842 CH28 3/6/01 3:46 PM Page 687
unsubscribe(get_email(), $id);
display_items(“Subscribed Lists”, get_subscribed_lists(get_email()),
‘information’, ‘show-archive’, ‘unsubscribe’);
break;
}
In each case, we call a function (subscribe() or unsubscribe()) and then redisplay a list of
mailing lists the user is now subscribed to using the display_items() function again.
The subscribe() and unsubscribe() functions are shown in Listing 28.11.
LISTING 28.11 subscribe() and unsubscribe() Functions from mlm_fns.php—These
Functions Add and Remove Subscriptions for a User
function subscribe($email, $listid)
{
if(!$email||!$listid||!list_exists($listid)||!subscriber_exists($email))
return false;
//if already subscribed exit

if(subscribed($email, $listid))
return false;
if(!db_connect())
return false;
$query = “insert into sub_lists values (‘$email’, $listid)”;
$result = mysql_query($query);
return $result;
}
function unsubscribe($email, $listid)
{
if(!$email||!$listid)
return false;
if(!db_connect())
return false;
$query = “delete from sub_lists where email = ‘$email’ and listid = $listid”;
Building Practical PHP and MySQL Projects
P
ART V
688
34 7842 CH28 3/6/01 3:46 PM Page 688
LISTING 28.11 Continued
$result = mysql_query($query);
return $result;
}
The subscribe() function adds a row to the sub_lists table corresponding to the subscrip-
tion; the unsubscribe() function deletes this row.
Changing Account Settings
The Account Settings button, when clicked, activates the account-settings action. The code for
this action is as follows:
case ‘account-settings’ :

{
display_account_form($normal_user, $admin_user, get_email(),
get_real_name(get_email()), get_mimetype(get_email()));
break;
}
As you can see, we are reusing the display_account_form() function that we used to create
the account in the first place. However, this time we are passing in the user’s current details,
which will be displayed in the form for easy editing. When the user clicks on the submit button
in this form, the store-account action is activated as discussed previously.
Changing Passwords
Clicking on the Change Password button activates the change-password action, which triggers
the following code:
case ‘change-password’ :
{
display_password_form();
break;
}
The display_password_form() function (from the output_fns.php library) simply displays a
form for the user to change his password. This form is shown in Figure 28.9.
Building a Mailing List Manager
C
HAPTER 28
28
BUILDING A
MAILING LIST
MANAGER
689
34 7842 CH28 3/6/01 3:46 PM Page 689
FIGURE 28.9
The display_password_form() function enables users to change their passwords.

When a user clicks on the Change Password button at the bottom of this form, the
store-change-password action will be activated. The code for this is as follows:
case ‘store-change-password’ :
{
if(change_password(get_email(), $old_passwd,
$new_passwd, $new_passwd2))
{
echo “<p>OK: Password changed.<br><br><br><br><br><br>”;
}
else
{
echo “<p>Sorry, your password could not be changed.”;
display_password_form();
}
break;
}
As you can see, this code tries to change the password using the change_password() function
and reports success or failure to the user. The
change_password() function can be found in the
user_auth_fns.php function library. The code for this function is shown in Listing 28.12.
Building Practical PHP and MySQL Projects
P
ART V
690
34 7842 CH28 3/6/01 3:46 PM Page 690
LISTING 28.12 change_password() Function from user_auth_fns.php—This Function
Validates and Updates a User’s Password
function change_password($email, $old_password, $new_password,
$new_password_conf)
// change password for email/old_password to new_password

// return true or false
{
// if the old password is right
// change their password to new_password and return true
// else return false
if (login($email, $old_password))
{
if($new_password==$new_password_conf)
{
if (!($conn = db_connect()))
return false;
$query = “update subscribers
set password = password(‘$new_password’)
where email = ‘$email’”;
$result = mysql_query($query);
return $result;
}
else
echo “<p> Your passwords do not match.”;
}
else
echo “<p> Your old password is incorrect.”;
return false; // old password was wrong
}
This function is similar to other password setting and changing functions we have looked at.
It compares the two new passwords entered by the user to make sure they are the same, and if
they are, tries to update the user’s password in the database.
Logging Out
When a user clicks on the Log Out button, it triggers the log-out action. The code executed by
this action in the main script is actually in the preprocessing section of the script, as follows:

if($action == ‘log-out’)
{
session_destroy();
unset($action);
Building a Mailing List Manager
C
HAPTER 28
28
BUILDING A
MAILING LIST
MANAGER
691
34 7842 CH28 3/6/01 3:46 PM Page 691
unset($normal_user);
unset($admin_user);
}
This snippet of code disposes of the session variables and destroys the session. Notice that it
also unsets the $action variable—this means that we enter the main case statement without an
action, triggering the following code:
case ‘’:
{
if(!check_logged_in())
display_login_form($action);
break;
}
This will allow another user to log in, or allow the user to log in as someone else.
Implementing Administrative Functions
If someone logs in as an administrator, she will get some additional menu options, which can
be seen in Figure 28.10.
Building Practical PHP and MySQL Projects

P
ART V
692
FIGURE 28.10
The administrator menu allows for mailing list creation and maintenance.
34 7842 CH28 3/6/01 3:46 PM Page 692
The extra options they have are Create List (create a new mailing list), Create Mail (create a
new newsletter), and View Mail (view and send created newsletters that have not yet been
sent). We will look at each of these in turn.
Creating a New List
If the administrator chooses to set up a new list by clicking on the Create List button, she will
activate the create-list action, which is associated with the following code:
case ‘create-list’ :
{
display_list_form(get_email());
break;
}
The display_list_form() function displays a form that enables the administrator to enter the
details of a new list. It can be found in the output_fns.php library. It just outputs HTML, so we
will not go through it here. The output of this function is shown in Figure 28.11.
Building a Mailing List Manager
C
HAPTER 28
28
BUILDING A
MAILING LIST
MANAGER
693
FIGURE 28.11
The Create List option requires the administrator to enter a name and description (or blurb) for the new list.

When the administrator clicks on the Save List button, this activates the store-list action,
which triggers the following code in index.php:
34 7842 CH28 3/6/01 3:46 PM Page 693
case ‘store-list’ :
{
if(store_list($admin_user, $HTTP_POST_VARS))
{
echo “<p>New list added<br>”;
display_items(“All Lists”, get_all_lists(), ‘information’,
‘show-archive’,’’);
}
else
echo “<p>List could not be stored, please try “
.”again.<br><br><br><br><br>”;
break;
}
As you can see, the code tries to store the new list details and then displays the new list of lists.
The list details are stored with the
store_list() function. The code for this function is shown
in Listing 28.13.
L
ISTING 28.13 store_list() Function from mlm_fns.php—This Function Inserts a New
Mailing List into the Database
function store_list($admin_user, $details)
{
if(!filled_out($details))
{
echo “All fields must be filled in. Try again.<br><br>”;
return false;
}

else
{
if(!check_admin_user($admin_user))
return false;
// how did this function get called by somebody not logged in as admin?
if(!db_connect())
{
return false;
}
$query = “select count(*) from lists where listname = ‘$details[name]’”;
$result = mysql_query($query);
if(mysql_result($result, 0, 0) > 0)
{
echo “Sorry, there is already a list with this name.”;
return false;
}
Building Practical PHP and MySQL Projects
P
ART V
694
34 7842 CH28 3/6/01 3:46 PM Page 694

×