Jump to content

Can Anyone tell me why this isnt working?


DanielHardy

Recommended Posts

Hi all,

 

I have a simple guestbook code that writes to and reads froma txt file. It reads from the file ok but will not add to it.

 

Here is the code

<?php
// Save filename to variable.
$guestbook = "messages.txt";

// This will run if someone sends user input.
if (isset($_POST['button']))
{
	// Check whether any fields are empty or not.
	if (!empty($_POST['name'])  && !empty($_POST['message']))
	{
		// Bad characters in E-Mail string.
		$badSearch = array("@", ".");
		// Goods characters in E-Mail string.
		$goodSearch = array(" @ ", " . ");
		// Grab name from the form and add a newline afterwards.
		$string .= "<b>" . $_POST['name'] . "  ";
		// Grab email and replace the @ and . in the string.
		$string .= str_replace($badSearch, $goodSearch, $_POST['email']) . "</b>\n";
		// Grab the message and save it in $string.
		$string .= "<font color=#2766c5>" . $_POST['message'] . "</font>\n<hr>";

		// Open messages.txt and append more posts to it.
		$file = fopen($guestbook, "a");
		// Write $string to the text file and replace bad characters to avoid XSS attacks.
		fwrite($file, nl2br(strip_tags($string, "<b><hr><font>")));
		// Close the file we opened.
		fclose($file);
		echo '<script>alert("Your comment has been added ")</script>';
	} else {
		// Of the form fields are empty will there popup a message.
		echo '<script>alert("Please fill out the whole form")</script>';
	}
}

$readfile = fopen($guestbook, "r");
   echo @fread($readfile, filesize($guestbook));
   fclose($readfile);

?>

 

Any help is greatly appreciated.

 

Thanks

 

Dan

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.