Simple GuestBook

By mfk on Apr 20, 2005

Is a simple guestbook for any site, just create 3 documents: addguestbook.php guestbook.txt index.php

Just chmod 777 guestbook.txt and enter the code in each file, enjoy :D

------ addguestbook.php code ------

<HTML>
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><style type="text/css">
<!--
body,td,th {
    font-size: 12px;
    color: #000000;
}
a:link {
    text-decoration: none;
    color: #0066FF;
}
a:visited {
    text-decoration: none;
    color: #0066FF;
}
a:hover {
    text-decoration: underline;
    color: #0066FF;
}
a:active {
    text-decoration: none;
}
body {
    background-color: #CCCCCC;
    SCROLLBAR-FACE-COLOR: #0066FF; MARGIN: 0px; SCROLLBAR-HIGHLIGHT-COLOR: #0066FF; SCROLLBAR-SHADOW-COLOR: #0066FF; SCROLLBAR-3DLIGHT-COLOR: #0066FF; SCROLLBAR-ARROW-COLOR: #51B0F2; SCROLLBAR-TRACK-COLOR: #51B0F2; Courier New; SCROLLBAR-DARKSHADOW-COLOR: #000000
}
-->
</style>
</HEAD>
<BODY>
<?php
$file           = "guestbook.txt";
$name       = $_POST['name'];
$email      = $_POST['email'];
$site       = $_POST['site'];
$msg        = $_POST['msg'];
$site   = stripslashes($site);
$msg  = stripslashes($msg);
$email  = stripslashes($email);
$name = stripslashes($name);
$msg = str_replace ("<","&lt;",$msg);
$msg = str_replace ("\n","<br>",$msg);
$site = str_replace ("<","&lt;",$site);
$site = str_replace ("\n","<br>",$site);
$email = str_replace ("<","&lt;",$email);
$email = str_replace ("\n","<br>",$email);
$name = str_replace ("<","&lt;",$name);
$name = str_replace ("\n","<br>",$name);

if(empty($email) || empty($name) || empty($msg)) {
    echo "<h3>Sorry all fields are required</h3>";
} else {

$fp = fopen($file,"a");

fwrite($fp, '
<font size="3">
<BR><BR>
Name: '.$name.'<BR>
Email: <a href="mailto:'.$email.'">'.$email.'</a><BR>
Home Page: <a href="'.$site.'">'.$site.'</a><BR>Message: '.$msg.'<BR>
');

fclose($fp);

echo '<font size="3"><p align="center">Thank you '.$name.' for singing my guestbook</p></font>'; 
}
?>
<div align="center"></div>
<div align="center"></div>
<div align="center"></div>
<div align="center"></div>
<p align="center"><a href="YOU`R INDEX.PHP LINK HERE">Back to GuestBook</a></p>
<div align="center"></div>
</BODY>
</HTML>

---- finish -----

---- index.php code -----

<HTML>
<HEAD>
<style type="text/css">
<!--
.style1 {font-size: 12px}
body,td,th {
    font-size: 14px;
}
a {
    font-size: 12px;
}
body {
    background-color: #CCCCCC;
    SCROLLBAR-FACE-COLOR: #0066FF; MARGIN: 0px; SCROLLBAR-HIGHLIGHT-COLOR: #0066FF; SCROLLBAR-SHADOW-COLOR: #0066FF; SCROLLBAR-3DLIGHT-COLOR: #0066FF; SCROLLBAR-ARROW-COLOR: #51B0F2; SCROLLBAR-TRACK-COLOR: #51B0F2; Courier New; SCROLLBAR-DARKSHADOW-COLOR: #000000
}
a:link {
    color: #0066FF;
    text-decoration: none;
}
a:visited {
    text-decoration: none;
    color: #0066FF;
}
a:hover {
    text-decoration: underline;
    color: #0066FF;
}
a:active {
    text-decoration: none;
}
-->
</style>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><BODY>
<form method="post" action="addguestbook.php">
<div align="center"></div>
<div align="center"></div>
<div align="center" class="style1"></div>
<div align="center"><span class="style1">Name:</span><br />
  <input name="name" type="text" size="30" maxlength="40" />
  <br />
  <span class="style1"><br />
  Email:</span><br />
<input name="email" type="text" size="30" maxlength="40" />
<br />
<br />
<span class="style1">Home Page:</span><br />
<input name="site" type="text" size="30" value="http://" maxlength="40" />
<br />
<br />
<span class="style1">Message:</span><br />
<textarea name="msg" cols="22" rows="6"></textarea>
<br />
<br />
<input type="reset" value="Reset" />
&nbsp;
<input type="submit" value="Send" />
</div>
<div align="center">
  <p>&nbsp;</p>
<?php
$file = "guestbook.txt";

if (fopen($file, "r")) {
$fil = "guestbook.txt";
$fo = fopen ($fil, "r");
$con = fread ($fo,filesize ($fil));
fclose ($fo);
echo "$con";
} else {
echo "<h3>Sorry there was an error please contact us now<h3>";
}
?></div>
</BODY>
</HTML>

---- finish -----

Comments

Sign in to comment.
tomi   -  Jan 22, 2013

how to make the most recent post appearing first ?

F*U*R*B*Y*  -  Jan 27, 2013

on line 57 you have

$fp = fopen($file,"a");

change that to

$fp = fopen($file,"x");

and it should fix it! (I believe!)

tomi  -  Feb 03, 2013

I've changed but i get an error
Warning: fopen(guestbook.txt) [function.fopen]: failed to open stream: File exists in /home/...
Warning: fwrite(): supplied argument is not a valid stream resource in /home/...
Warning: fclose(): supplied argument is not a valid stream resource in /home/...

F*U*R*B*Y*  -  Feb 04, 2013

have you created the file?

mc_peko  -  Apr 01, 2013

I too very much need the last entry on top. I've set this up as a beta. And will try to add some security. E-mail validation, and perhaps a captcha, to avoid spam. Also I think separate pages when messages exceed , say 20, is something we must have. But I'd like to stop right about there, to keep it simple. Help very much appreciated. Thanks a lot to you coders.

mc_peko  -  Apr 01, 2013

Check http://www.php.net/manual/en/function.fopen.php

$fp = fopen($file,"c");

might work.

But not for me yet. :(

Sign in to comment

FallingDooms   -  Apr 15, 2011
<?php $file = "guestbook.txt"; $name = $_POST['name']; $email = $_POST['email']; $site = $_POST['site']; $msg = $_POST['msg']; $site = stripslashes($site); $msg = stripslashes($msg); $email = stripslashes($email); $name = stripslashes($name); $msg = str_replace ("<","<",$msg); } else { $fp = fopen($file,"a"); fwrite($fp, '

Name: '.$name.'
Email: '.$email.'
Home Page: '.$site.'
Message: '.$msg.'
'); fclose($fp); echo '

Thank you '.$name.' for singing my guestbook

'; } ?>
 Respond  
natar   -  Dec 20, 2010

I guess the guestbook.txt should be chmod 646 or 666

 Respond  
killer64   -  Mar 14, 2010

i get an error

 Respond  
F*U*R*B*Y*   -  Jan 27, 2006

how do we code chmod 777 into guestbook.txt?

mc_peko  -  Apr 01, 2013

You could use for instance CuteFTP after uploading the file to server.

Sign in to comment

cooolstar   -  May 14, 2005

this is the something same as i used before but again found here thats wat i need. its simple so its good

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.