With that done we now need to build up the PHP script to handle
the requests from the flash movie. We’I'll use a lot of
functions in this code to keep everything easily understandable.
Before we kick into the PHP script, we need to think about
how we’re going to store the user data in our text files.
Since the file function will load each line of the given file
into in array element it seems sensible to have one line per
subscriber in the file. However, we need to store more than
a single piece of data about each subscriber. The information
we’ll want to store is:
• Name
• E-mail Address
• Date Subscribed
So we need a way of fitting this all on to one line whilst
still being able to get at the individual bits when we need
to. The answer is to use some kind of separator and then use
the explode function to split the string apart when we want
to get at the individual bits.
W e need to make sure that the separator we choose isn’t
likely to crop up in any of the data fields we want to store,
or else it’ll cause problems when we come to explode the
string. A commonLy used separator is the “|” character
and this is the one we’ll be using in this application.
Just so you’re sure about what’s going on here,
take a look at a few entries from my text subscriber file:
Rob Higgins| rhiggins@shout.net|998254219
Robert| robhiggins@robhiggins.com|998297924
Administrator| admin@faxswitch.com|998299352
Here is how the different data fields are separated. The number
at the end is the UNIX time stamp.
Here is the code
<?
// maillist.php
//The first thing we do is check that the required data has
been sent.
if (!isset($email) || !isset($name) || empty($email) || empty($name))
{
fail("Both a name and an email address are required");
}
//we convert the email address to all lower case
$email = strtolower($email);
//we use a switch statement to check the valuse of the $action
variable
switch($action) {
case "subscribe":
subscribe($name, $email);
break;
case "unsubscribe":
unsubscribe($email);
break;
default:
fail("Unknown action: $action");
}
//here is the function subscribe options for various errors
function subscribe($name, $email) {
if (isSubscribed($email)) {
fail("$email already subscribed to mailing list");
}
$joinDate = time();
//here we open the subscriber dat file
$file = fopen('subscriber.dat', 'a+');
if (!$file) {
fail("Error: Couldn't open subscriber file");
}
if (!$file) {
fail("Couldn't remove subscriber from file");
}
foreach($subscribers as $subscriber) {
fwrite($file, $subscriber);
}
fclose($file);
success();
}
//here is the email function
function sendEmail($email, $name) {
$mailTo = "$name <$email>";
$mailFrom = "From: Rob Higgins Mailing List <rhiggins@shout.net>";
$mailSubject = "Your registration confirmation...";
$mailBody = "Dear $name,\n\nYour details have been added
to the mailing list. Details of the latest scripts will be mailed
to you at $email. You can also check out the latest developments
at http://www.robhiggins.com\n\nIf you wish to remove yourself
from this mailing list at any time use the following link:\nhttp://www.robhiggins.com/maillist.php?Action=unsubscribe&Email=$email\n\nThank
you!\n\nRob Higgins Team";
mail($mailTo, $mailSubject, $mailBody, $mailFrom);
}
//here is the email notification function
function mailAdmin($email, $name) {
$mailTo = "Rob Higgins<rhiggins@shout.net>";
$mailFrom = "From: Rob Higgins Mailing List <robhiggins@robhiggins.com>";
$mailSubject = "New Subscriber Alert...";
$mailBody = "$name ($email) has just signed up";
mail($mailTo, $mailSubject, $mailBody, $mailFrom);
}
function isSubscribed($email) {
$matchFound = false;
$subscribers = file('subscriber.dat');
if ($subscribers) {
foreach($subscribers as $count => $subscriber) {
$info = explode('|', $subscriber);
// If supplied username/password do not match above
if ($inUsername != $adminUsername || $inPassword != $adminPassword)
{
// Output error information and quit
print "Invalid username or password";
exit;
}
// Decide on what action we need to take
switch($action) {
// Fetch list of subscribers
case "List Subscribers":
fetchList();
break;
// Send email to subscribers
case "Send Email":
sendEmail($mailSubject, $mailBody);
break;
default:
print "Unknown action: $action";
exit;
}
function fetchList() {
// Register global variables
global $subsFile;
// Attempt to open subscriber file
$subscribers = file($subsFile);
if (!$subscribers) {
// Output error information and quit
print "Couldn't open subscriber file or no subscribers
listed";
exit;
}
// For each subscriber line...
foreach($subscribers as $count => $subscriber) {
// split subscriber info into array
$info = explode('|', $subscriber);
// Assign array to meaningful variable name
$name = $info[0];
$email = $info[1];
$joined = $info[2];
// Create a readable joined date out of timestamp
$joined = strftime("%D", $joined);
// Output information for each subscriber
print "<b>Subscriber $count</b><br>";
print "Name: $name<br>\n";
print "Email: $email<br>\n";
print "Joined: $joined<br>\n";
print "<br>\n";
}
}
function sendEmail ($mailSubject, $mailBody) {
// Register global variables
global $subsFile;
// Set up reply address for mailing list
$mailFrom = "Mailing List <you@youremail.com";
// Ensure that subject and body of email have
// automatically inserted escape slashes removed
$mailSubject = stripslashes($mailSubject);
$mailBody = stripslashes($mailBody);
// Attempt to read subscriber file
$subscribers = file($subsFile);
// If file open failed...
if (!$subscribers) {
// Output error information and quit
print "Couldn't open subscriber file or no subscribers
listed";
exit;
}
// For each subscriber line...
foreach($subscribers as $subscriber) {
// split subscriber info into array
$info = explode('|', $subscriber);
// Assign array to meaningful variable name
$name = $info[0];
$email = $info[1];
$joined = $info[2];
// Build to address including subscriber name
$mailTo = "$name <$email>";