Hi there,
I've been working on a PHP/Flash message board - I got everything working perfectly, the only problem was the new entries were written at the end of the file as apposed to the start. This meant that the user had to scroll down to see the most recent entries.
This where we got to:
<?
if (!isset($name) || !isset($email) || !isset($message) || empty($name) || empty($email) || empty($message)) {
print "&result=Fail";
print "&errorMsg=" . urlencode("Input required for all fields.");
exit;
}
$email = strtolower($email);
addentry($name, $email, $message);
function addentry($name, $email, $message) {
$posted = strftime("%D %I:%M %p");
$message = stripslashes($message);
$file = fopen('entry.txt', 'r+');
flock($file, LOCK_EX); // tell flock what it should do
$old = fread($file, filesize($file)); // read in old data first
fseek($file, 21, 0);
if (!$file) {
print "&result=Fail";
print "&errorMsg=" . urlencode("Could not open entry.txt file. Change CHMOD levels to 666."); // 766 makes no sense for text files
exit;
}
fputs($file, "<font color=\"#000000\">Name:</font> $name\n<font color=\"#000000\">Email:</font> <font color=\"#000000\"><u><A href=\"mailto:$email\">$email</A></u></font><br>\n<font color=\"#000000\">Posted:</font> $posted\n<font color=\"#000000\">Message:</font> $message\n\n");
fputs($file, $old); // write back old data
// flock($file, LOCK_UN); // this would unlock the file after use
fclose($file); // but closing it will unlock it anyway
// Send admin an email when new entry occurs
mailAdmin($name, $email, $message);
}
function mailAdmin($name, $email, $message) {
$mailTo = "my@mail.co.uk";
$mailFrom = "From: <my@mail.co.uk>";
$mailSubject = "New Guestbook Entry";
$mailBody = "A visitor to your site has left the following information in your guestbook:\n
Name: $name
Email: $email
The visitor commented:
------------------------------
$message
------------------------------
You can view the message at:
http://www.mysite.com";
mail($mailTo, $mailSubject, $mailBody, $mailFrom);
}
print "&result=okay";
exit;
?>
We were trying to open the file, read 21 characters in (this has to remain the same for readback in Flash), then copy all the current info, paste in the new entry, then paste in the old info afterwards.
I think we're nearly there but all this script seems to do is simply delete the old entry and write the new one.
Any help would be much appreciated as it's the last little hurdle on quite a big site I've been working on!
Many thanks!
Matt Faulkner