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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.