PDA

View Full Version : writing a guest book


briintex1
01-28-2004, 07:18 AM
is there a way to write a guest book in php but not use a database. I know that is not the logical way of doing things, but I think I am to a limited amount of space on the server. I do know that in other languages it is possible, but it is not recommended. But I did know if I could write a guest book and let people display their things into an outfile like a notepad I put on the server. With this it is then read everytime the guest book is looked at and also displayed...sorry to write so much :o

Mhtml
01-28-2004, 01:42 PM
Please don't double post! Notify a moderator to remove a thread or move a thread if you posted in the wrong forum!

Celtboy
01-29-2004, 04:21 AM
Uhm. A database wouldn't take much more space than the flat file would. But you could do something like this:


<?php

$gb_file = "guestbook.txt";


print "<html>";
print " <head><title>My GuestBook</title></head>";
print " <body>";

if ( (isset($_POST["view"])) || (isset($_GET["view"]))) {
$my_file = file($gb_file);
foreach ($my_file as $line) {
str_replace($line,"-------","<br/><hr width=\"200\">\n");
print $line;
print "<br/>";
}
print "</body></html>";
exit();
} elseif (isset($_POST["thanks"])) {
print "Thank you for submitting your entry! View it <a href=\"gb.php?view=yes\">here</a></body></html>";
post_entry($gb_file);
exit();
} else {
print "Add your own entry to the guest book!:<br/><br/>\n\n";
print "<form action=\"gb.php\" method=\"POST\">\n";
print "<input type=\"hidden\" name=\"thanks\" value=\"1\">\n";
print "<table width=\"500\">\n";
print "<tr>\n";
print " <td>Your Name:</td>\n";
print " <td><input type=\"text\" name=\"user_name\"/></td>\n";
print "</tr>\n";
print "<tr>\n";
print " <td>Your Email:</td>\n";
print " <td><input type=\"text\" name=\"user_email\"/></td>\n";
print "</tr>\n";
print "<tr>\n";
print " <td>Your Website:</td>\n";
print " <td><input type=\"text\" name=\"user_website\" value=\"http://\" /></td>\n";
print "</tr>\n";
print "<tr>\n";
print " <td>Your Message</td>\n";
print " <td><textarea name=\"user_comments\"/ rows=\"5\" cols=\"40\"></textarea></td>\n";
print "</tr>\n";
print "<tr>\n";
print " <td>&nbsp;</td>\n";
print " <td><input type=\"submit\" value=\"Add to Guestbook!\"/></td>\n";
print "</tr>\n";
print "</table>\n";
print "</form>\n\n";
print "</body>\n</html>";
}

function post_entry($gb_file) {
$my_file = fopen($gb_file,"a");

fputs ($my_file,"Name: " . $_POST["user_name"] . "\r\n",4096);
fputs ($my_file,"Email: " . $_POST["user_email"] . "\r\n",4096);
fputs ($my_file,"Website: " . $_POST["user_website"] . "\r\n",4096);
fputs ($my_file,"Message: " . $_POST["user_comments"] . "\r\n",4096);

fputs ($my_file,"-------" . "\r\n",4096);
fclose($my_file);
return;
}


?>

no test, not sure how it'll work. But uhm....it's code?

Celtboy
01-29-2004, 04:24 AM
I should also point out that the above code is missing a lot of things->
-Error Handling (both functionally, and user_entered data
-HTML blocking
-Format customization
-Number of posts
-Should use <DIV>'s instead of table to layout elements

as it stands, the code is pretty ugly, but I'm in a really bad mood and that's the best I can muster at the moment.

MPCODER
01-29-2004, 05:21 PM
I think it's better to let the script know that $name = $_POST["name"] and let it log $name. That way you can add a stripslashes tag. Like $stripslashes = stripslashes($name);
And don't let it log stripslashes. That way you can add smillies and such without advanced UBB code. It's not really necessary to block HTML i think. Good luck writing.

Celtboy
01-29-2004, 05:31 PM
blocking html is essential! at least at the current point in the code. Otherwise an unclosed "<marquee>" tag could wreck the whole page....

besides, if the code works....it's good enuf for 10 minutes of coding ;)

Celtboy
01-29-2004, 05:43 PM
in addition, each field needs to be wrapped in "blocks."

The script should then read in the blocks. That would be just one more safety mechanism to ensure data integrity. Imagine someone finishing their message with "-------" oh the bad things that could happen...

but you're right. stripslashes should also be implemented. :)