Guest Book

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
AluminX
Forum Newbie
Posts: 19
Joined: Sat Jul 17, 2004 9:53 am

Guest Book

Post 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
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post 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'] == '')
AluminX
Forum Newbie
Posts: 19
Joined: Sat Jul 17, 2004 9:53 am

Post 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
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

What kind of server are you on? Sometimes the \n char can be \r\n or even \r.
AluminX
Forum Newbie
Posts: 19
Joined: Sat Jul 17, 2004 9:53 am

Post 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
AluminX
Forum Newbie
Posts: 19
Joined: Sat Jul 17, 2004 9:53 am

Post 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
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post 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!!)
AluminX
Forum Newbie
Posts: 19
Joined: Sat Jul 17, 2004 9:53 am

Post 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 :)
Post Reply