XML Guestbook
with PHP
by Arjen
Gosman aka Flashmatazz: 18 april 2004
The PHP script
Now that Flash has sent our updated myXML object to the PHP
script on the server, we'll have a look at how this script
works. As you could see on the previous page, the script is
named processXML.php and here is how it looks:
- <?php
- $xmlString
=
$HTTP_RAW_POST_DATA;
- if
(is_null($xmlString))
{
- print
"No data was sent";
- }
- else
{
- $file
=
fopen("guestbook.xml",
"w+")
or
die("Can't
open XML file");
- if(!fwrite($file,
$xmlString)){
- print
"Error writing to XML-file";
- }
- print
$xmlString."\n";
- fclose($file);
- }
- ?>
Explanation:
- In the first line, the data that was sent from Flash
is retrieved and assigned to the $xmlString
variable. Data sent using the XML object is not URL
encoded. Therefore PHP won't parse the data into variables
and you will need to use $HTTP_RAW_POST_DATA to access the
raw data directly. $xmlString now contains one
long string where the new guestbook entry is already
inserted.
- The if statement then uses the is_null()
function to check if the $xmlString variable really
contains a value. If for some reason the data is not
retrieved correctly or the PHP script is called directly
from the browser, we could at the end of the script end up
with an empty XML file.
- In the else statement, if $xmlString does
contain data, we use the fopen() function to open
the file guestbook.xml using the w+ mode. This
mode opens the file for reading and writing, places the
file pointer at the beginning of the file and truncates
the file to zero length, meaning that all content is
erased. If the file does not exist, it will attempt to
create it. The result of the fopen() action is
assigned to the $file variable, meaning that
$file can now be used as a pointer to our XML file.
Note that the file must be accessible to PHP, so you need
to ensure that the file permissions allow this access.
This means that you must set (CHMOD) permissions to 777,
which you can do with your FTP program for example.
- Within the following if-statement the script attempts
to write this 'xml-string' back to our guestbook.xml
file.
- After that the $xmlString variable is sent
back to Flash where it is received in our receiverXML
object.
- Finally a bit of cleaning up is done to free up the
used file descriptor: our XML file is closed using the
fclose() method.
And now we're almost done. By sending our
data back to Flash, the receiverXML.onLoad handler
is invoked. Within this handler the showXML() prototype is
called again, just as it was when the flash movie first
loaded. This function again loops through our - now updated
- XML file and shows all entries in our textfield so we can
read what we've just written in the guestbook
This leaves me with one final note taken
from Macromedia's Technotes:
|
Note |
Problem:
Loading more than 64k of data using the
LoadVars.load, loadVariables, XML.load, or
XML.sendAndLoad actions can cause poor browser
performance. Common problems can include 501errors,
"not implemented" errors, or general browser
slowness.
Solution:
Load the information in smaller packets. Using
multiple load actions and spreading the data out
over a series of frames can reduce the work the
Macromedia Flash Player needs to do to load and
parse the information. This can greatly increase
browser and loading performance. |
|
After done some testing with an XML file
exceeding 500 Kb (over 700 entries) I must say I haven't
experienced this problem however.
To wrap up this tutorial: although a mySQL database offers a
much more powerful way to create a guestbook, it is actually
possible to create a simple one using only Flash, PHP and an
XML file.
I hope this tutorial has been useful for
you. If you have any questions, feel free to post on the
forums... but first download the zipped guestbook and give
it a go ;)
Cheers!
|
|
|
|