Page 1 of 1

Guest Book

Posted: Mon Apr 09, 2007 5:26 am
by AluminX
Hi, i'm trying to make a simple guest book.
i already built the sign part now i want to build a function that allows the user to view the guest book.
this is what i have so far:
sign guest book:

Code: Select all

<?php
if(!$_POST["userName"]=="" && !$_POST["body"]==""){
	$entry = $_POST["userName"] ."#". $_POST["body"]."\n";
	if(!$guestBook = fopen("guestBook.txt","a")){
		echo "Error, could not open guest book.";
		exit;
	}
	if(!fwrite($guestBook, $entry)){
		echo "Error, unable to write on file.";
		exit;
	}
	echo "Thank you for signing. <a href=viewBook.php>Click here</a> to view guest book";
	fclose($guestBook);
}else echo "Invalid Name and/or Comment.";

?>
view guest book:

Code: Select all

<?php
$guestBook=fopen("guestBook.txt","r");
if(!$guestBook){
	echo "Error, could not read file";
	exit;
}
while(!feof($guestBook)){
    	if(fgetc($guestBook) != "\n"){
	   echo fgetc($guestBook);
        } 

}
fclose($guestBook);
?>
well the view guest book doesn't work it prints some non sense like "dincmet eiSmtigEs"
what i want to do is just print every character exept \n or # (since these are gonna be my denoters).

also since we'r on the subject, how can i move the file ponter as i please?

thanks

Posted: Mon Apr 09, 2007 7:30 am
by Begby
Have you looked at the file to make sure its going to be written to correctly?

Also, do this -> if ($_POST['var'] != '')
instead of -> if( !$_POST['var'] == '')

Posted: Mon Apr 09, 2007 1:45 pm
by AluminX
the file is written correctly the only problem i'm having is on the line,

Code: Select all

if(fgetc($guestBook) != "\n"){
           echo fgetc($guestBook);
        }
basicly if the character is not a newline than print it on the page

Posted: Mon Apr 09, 2007 1:48 pm
by RobertGonzalez
What kind of server are you on? Sometimes the \n char can be \r\n or even \r.

Posted: Mon Apr 09, 2007 3:44 pm
by AluminX
freehostia.com but i dont think thats the problem i tryed to make ^ char as the denoter instead of \n and it still doesn't
work correctly, i think there is something wrong with the syntax i'm using, not sure.
here are my files.
http://adriansdesigns.freehostia.com/ch ... guestBook/
check code.txt for the source code of php files

Posted: Tue Apr 10, 2007 12:23 am
by AluminX
apperantly i'm not making myself clear.
here is one last time. how would i go and do this in php?
i would like to go through a file.txt character by character, if the characters equals to "%" than print "% found"

inside file.txt should be this string "this is a string with % sign in it."

can anyone help me with this one please?

thanks

Posted: Tue Apr 10, 2007 12:49 am
by Kieran Huggins
I would store each message on it's own line encoded with base64_encode()

Then read the file with file() and decode/output each entry in the array

I have a feeling this is how the php manual comments are stored as well (believe it or not!!)

Posted: Tue Apr 10, 2007 3:31 am
by AluminX
ohh thank you i actually did a while loop untill eof doing $fileintostr .= fgetc($file) than parsed the string through a function
here is what i got so far.
http://adriansdesigns.freehostia.com/ch ... /Sign.html
also you can view all the directory of
http://adriansdesigns.freehostia.com/ch ... guestBook/

the code is not well formed obviously i'm just getting my feet wet in php and there are many ways to abuse the system so i ask please don't.
look through the code and give me your opinion as well as what else can i do to improve the current code. you can even post it on the guest book :)