Guest book system without a database

Discussion in 'PHP' started by vishal sharma, Aug 16, 2004.

  1. vishal sharma

    vishal sharma New Member

    Joined:
    Jul 23, 2004
    Messages:
    106
    Likes Received:
    6
    Trophy Points:
    0
    Ill start by giving the code to the guest book then I will give a step by step on what each part does.
    Code:
    <?php
    if(isset($_POST['name']) && isset($_POST['message'])) 
    { 
    	if($_POST['name'] != "" && $_POST['message'] != "")
    	{
    		$F_D = getdate();
    		$FT = $F_D['mon']."/".$F_D['mday']."/".$F_D['year'];
    
    		$FP = Fopen('guestbook.html',"a");
    		fwrite($FP,"<tr><td>Date: ");
    		fwrite($FP, $FT);
    		fwrite($FP, "</tD></tr><tr><td>Name: ");
    		fwrite($FP, $_POST['name']);
    		fwrite($FP, "</td></tr><tr><td>");
    		fwrite($FP, strip_tags($_POST['message']));
    		fwrite($FP, "</td></tr><tr><td><hr width="90%" color=red></td></tr>");
    		fclose($FP);
    	}
    }
    ?> 
    <TABLE cellSpacing=3 cellPadding=2 width="65%" border=9 align=center>
    	<tr>
    		<td><center><u>Guestbook</u></center></td>
    	</tr>
    	<tr>
    		<td>
    			<TABLE cellSpacing=1 cellPadding=0  border=0 align=center style="WIDTH: 95%; HEIGHT: 54px">
    				<tr>
    					<td><form action="guestbook.php" method=post>Name: <input type=text width="90%" name="name"><br><t_extarea cols=60 rows=10 name="message"></t_extarea><br><Br><input type=submit value="Post"></form></td>
    				</tr>
    			</table>
    			<TABLE cellSpacing=3 cellPadding=2 width="65%" border=9 align=center> 
    				<?php include("guestbook.html"); ?>
    			</TABLE>
    		</td>
    	</tr>
    </TABLE> 
    That’s all there is to it! Ok I started off by checking if the message was send and if so is it empty with the isset(); function nested in an if command. The isset function checks to see if a variable is set or not if it is it will return true. So I check to see if the variables $_POST[‘name’] and $_POST[‘message’] are set. I use these two variable names because later on in my form I use a form with a method of post and in the form I have two input boxes. One named name and one message.

    PHP:
    if(isset($_POST['name']) && isset($_POST['message'])) 

        if(
    $_POST['name'] != "" && $_POST['message'] != "")
        {
            
    $F_D getdate();
            
    $FT $F_D['mon']."/".$F_D['mday']."/".$F_D['year'];
            
    $FP Fopen('guestbook.html',"a");
            
    fwrite($FP,"<tr><td>Date: ");
            
    fwrite($FP$FT);
            
    fwrite($FP"</tD></tr><tr><td>Name: ");
            
    fwrite($FP$_POST['name']);
            
    fwrite($FP"</td></tr><tr><td>");
            
    fwrite($FPstrip_tags($_POST['message']));
            
    fwrite($FP"</td></tr><tr><td><hr width="90%" color=red></td></tr>");
            
    fclose($FP);
        }
    }
    So I check to see if the variables are set and if they aren’t empty. If all is well then the script proceeds to the code. I start out by getting the current date array then setting it to a readable form with:

    PHP:
    $F_D getdate();
    $FT $F_D['mon']."/".$F_D['mday']."/".$F_D['year'];
    This will output the date as a Month/Day/Year format. After I get this variable set I open up my guest book page: guestbook.html for appending

    PHP:
    $FP Fopen('guestbook.html',"a");
    After I get the file open and have a valid file resouce I write a table with the date and their name and message in it then I add a red line to show the end of the post and then close the file.
    PHP:
    fwrite($FP,"<tr><td>Date: ");
    fwrite($FP$FT);
    fwrite($FP"</tD></tr><tr><td>Name: ");
    fwrite($FP$_POST['name']);
    fwrite($FP"</td></tr><tr><td>");
    fwrite($FPstrip_tags($_POST['message']));
    fwrite($FP"</td></tr><tr><td><hr width="90%" color=red></td></tr>");
    fclose($FP);
    This really is all the php that you need except for one more thing a include(“guestbook.html”);. I wrote up a nice little table and everything for the design but you don’t have to. It just makes it look a little better.
    HTML:
    <TABLE cellSpacing=3 cellPadding=2 width="65%" border=9 align=center>
    	<tr>
    		<td><center><u>Guestbook</u></center></td>
    	</tr>
    	<tr>
    		<td>
    			<TABLE cellSpacing=1 cellPadding=0  border=0 align=center style="WIDTH: 95%; HEIGHT: 54px">
    				<tr>
    					<td><form action="guestbook.php" method=post>Name: <input type=text width="90%" name="name"><br><t_extarea cols=60 rows=10 name="message"></t_extarea><br><Br><input type=submit value="Post"></form></td>
    				</tr>
    			</table>
    			<TABLE cellSpacing=3 cellPadding=2 width="65%" border=9 align=center> 
    				<?php include("guestbook.html"); ?>
    			</TABLE>
    		</td>
    	</tr>
    </TABLE> 
    Here I make a table for the whole guest book then I create a table for the submit a message form. After this I make a new table for the messages. Then comes the most important part:

    PHP:
    <?php include("guestbook.html"); ?> 
    This will take the file guestbook.html and insert the code from it into the code of the page that called it. If you remember guestbook.html is where we keep all of the messages. After I include it I end the tables and then Im done!

    This guestbook is just a basic foundation that you should use. If you decide to employ this be careful because it is HIGHLY susceptible to XSS attacks.

    more later....
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice