Go4Expert

Go4Expert (http://www.go4expert.com/)
-   PHP (http://www.go4expert.com/articles/php-tutorials/)
-   -   Guest book system without a database (http://www.go4expert.com/articles/guest-book-database-t119/)

vishal sharma 16Aug2004 12:15

Guest book system without a database
 
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 Code:

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 Code:

$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 Code:

$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 Code:

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 Code:

<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 Code:

<?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....


All times are GMT +5.5. The time now is 06:55.