Second steps in creating a Flash - PHP guestbook
Processing variables sent from Flash to PHP.
The guestbook script that process the variables is very simple. It checks to see if all the variables have a value. If one of the variables is empty, the script redirects the browser to the HTML page with the Flash movie, otherwise it prints the variables.
PHP Code:
<?php
if (($homepage == "") || ($email == "") || ($name == "") || ($comments == "")) {
header("Location: guestbook.html");
exit;
}
?>
<HTML> <HEAD> <TITLE>Guestbook Submission information</TITLE> </HEAD> <BODY BGCOLOR="#FFFFFF" TEXT="#000000"> <div align=center> <P>Thanks, <font color="red"><?php echo "$name"; ?></font>.<br> (Email: <a href="mailto:<?php echo "$email"; ?>"><?php echo "$email"; ?></a>), <br>Homepage: <a href="<?php echo "$homepage"; ?>"><?php echo "$homepage"; ?></a> <br> <P>Comments: <?php echo "$comments"; ?></p> </div> </body> </html>
See it in action. ¦ Get complete code.
Storing guestbook entries
The guestbook script only prints the values of the name, email, homepage etc. fields, it does not store them for later viewing. I decided to store the guestbook entries in a text file - guestbook.data - and create a Flash movie to display the entries from the text file.
Instead of going to the guestbook script, which only prints the values. The guestbook movie now passes the values of its fields to the guest script, which will save them to the guestbook.data text file.
Note: The guestbook.data file should be Unix mode 777 (publically readable and writable).
The guest script first opens the guestbook.data file then assigns any data in the file to a variable. The new values sent by the guestbook movie to the data file are written to the file. The old values that were assigned to a variable are then written to the file after the new values. The most recent guestbook entry will always be the first entry viewed. The script then redirects the browser to the viewbook movie which will dispay the entries.
PHP code:
<?php
$date = date( "l, F j Y, h:i a");
if ($homepage == "" ¦¦ $email == "" ¦¦ $name == "" ¦¦ $comments == "") { //error checking
header("Location: guestbook.html");
exit;
}
$message .="Name: $name \n";
$message .="Email: $email \n";
$message .="Homepage: $homepage \n";
$message .="Comments: $comments \n";
$message .="Date: $date \n";
$message .="----------------------------- \n";
if ($message){
// first read the file into a buffer, if it exists
// open in read mode only
if ($fp = @fopen ("guestbook.data", "r")){
$oldmessages = @fread($fp,filesize("guestbook.data"));
@fclose(fp);
}
// now reopen the file in write mode, with the
// pointer at the head of the file
$fp = fopen ("guestbook.data", "w");
fwrite ($fp, $message);
fwrite ($fp, $oldmessages); // adding back the old messages
fclose ($fp);
}
if (file_exists("guestbook.data")){
Header("Location: viewguestbook.html");
}
?>
See it in action. ¦ Get complete code.
The first frame of the viewguestbook Flash movie has a stop action on the frame, one dynamic text field and two buttons (view entries and add to guestbook) on the stage.
The dynamic text field:
The entries dynamic text field should have the 'Multi Line,' 'HTML,' and 'Word wrap' options selected.
The 'HTML' option is used because the entries are stored in HTML in the data file. Homepage and email values will be clickable, linking to the homepage and email of the visitor.

The View entries and Add to guestbook buttons:
The View entries button has the Loadvariables action, which retrieves the data from the guestbook.data file via the entries.php script.

The entries.php script, reads the data in the guestbook.data file and assigns it to the entries variable then prints the value.
PHP code:
<?php
// first read the file into a buffer, if it exists
// open in read mode only
if ($fp = @fopen ("guestbook.data", "r")){
$entries = @fread($fp,filesize("guestbook.data"));
@fclose(fp);
}
print "&entries=".$entries;
?>
See it in action. ¦ Get complete code.
The Add to guestbook button redirects the browser to the guestbook movie to add new entries to the guestbook.

The Up and Down buttons:
The Up button scrolls the entries dynamic text field up.

The Down button scrolls the entries dynamic text field down.

See it in action. ¦ Get complete code.
Flash Guestbook winzip file can be downloaded here.
Updated the zip archive to include a Flash movie that integrates viewing and
adding to the guest book in one file. See it in action. ¦ Download the file.