Go Back   CodingForums.com > :: Server side development > PHP > Post a PHP snippet

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rating: Thread Rating: 30 votes, 3.40 average.
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-24-2007, 11:20 AM   PM User | #1
GSimpson
Regular Coder

 
GSimpson's Avatar
 
Join Date: Aug 2006
Location: New Zealand
Posts: 268
Thanks: 9
Thanked 0 Times in 0 Posts
GSimpson is an unknown quantity at this point
Guest Book - No Database

I found this at hotscripts and it was about 10 files and a whole lotta includes and requires so I thoug I would bring it down to 3 files, 2 which you don't even see.

create a file on you directory named data.php and save it with NO CODING inside it.

create index.php and this code:

PHP Code:
 <html>
  <head>
   <style> @import "stylesheet.css"; </style>
   <title> My Guest Book </title>
  </head>
  <body>

   <div class="thread">
    <?php include "data.php"?>
   </div>

   <div class="add">
   <b>Add entry</b> <br/> <br/>

   <form action="insert.php" method="post">
    <input type="hidden" name="posted" value="yes">
    Nickname: <input type="text" name="name"> <br/> <br/>
    Post:<br/>
    <textarea cols="50" rows="5" name="post">Enter Any Comment Here...</textarea> <br/>
    <input type="submit" value="Submit">
   </form> 
  </div>

  </body>
 </html>
And that will have your form and display your comments and finally all you need is the 2nd page you won't see.

insert.php
PHP Code:
<?php
$allowed_html_tags 
"";
$addtotop "1"// This determines the order to display it. Leave for newest comments on top or change to 0 for oldest to newest.

// Checks if the user wants to view the form or add a entry
if (
$_SERVER['REQUEST_METHOD'] == 'POST' ) {

             
// Make sure the script works if register_globals is off
             
$name $HTTP_POST_VARS['name'];
             
$post $HTTP_POST_VARS['post'];

             
// Process the Information Entered and Remove Stuff
             
$post strip_tags($post$allowed_html_tags); // Strip HTML
             
$post stripslashes($post); // Strip Slashes

             
$time date("F jS Y, h:iA");  

             if (
$addtotop == "0" ) {

                          
// Writes the user's entry to a file
                          
$fp fopen("data.php",  "a");  
                          
fputs($fp"<p><b>Posted by:</b> " $name "</p><p>" $post "</p><p><b>Time: </b>" $time "</p>");
                          
fclose($fp);

             }

             if (
$addtotop == "1" ) {

                          
// Get all the current entries and put it in a string
                          
$att1 "data.php";
                          
$att2 fopen ($att1"rb");
                          
$currententries fread ($att2filesize ($att1));
                          
fclose ($att2);

                          
// Writes the user's post to a file
                          
$fp fopen("data.php",  "w+");  
                          
fputs($fp"<p><b>Posted by:</b> " $name "</p><p>" $post "</p><p><b>Time: </b>" $time "</p>" $currententries);
                          
fclose($fp);

             }

header("Location: index.php?error=false");

} else {
header("Location: index.php?error=true");
}
?>
feel free to add to the script. I can't say otherwise.
GSimpson is offline   Reply With Quote
Old 07-10-2007, 02:32 PM   PM User | #2
Jutlander
Regular Coder

 
Jutlander's Avatar
 
Join Date: Jun 2007
Location: In my own sick little world :P
Posts: 425
Thanks: 1
Thanked 12 Times in 12 Posts
Jutlander is on a distinguished road
This is a great script. Is there anyone who could take the time and make it spamproof? So you had to include a word in the comment, that is not used by spammers? If the word is included in the comment, the comment will get posted and if not, it will be left out of course. And if the word is in the comment, it will get hidden...
__________________
.
.
Jutlander is offline   Reply With Quote
Old 07-10-2007, 02:49 PM   PM User | #3
Jutlander
Regular Coder

 
Jutlander's Avatar
 
Join Date: Jun 2007
Location: In my own sick little world :P
Posts: 425
Thanks: 1
Thanked 12 Times in 12 Posts
Jutlander is on a distinguished road
Also, I get this error the first time I try to post:

Warning: fread(): Length parameter must be greater than 0. in c:\programmer\easyphp1-8\www\insert.php on line 33

Warning: Cannot modify header information - headers already sent by (output started at c:\programmer\easyphp1-8\www\insert.php:33) in c:\programmer\easyphp1-8\www\insert.php on line 43

Then reload and suddenly there's two comments of the same.
__________________
.
.
Jutlander is offline   Reply With Quote
Old 07-19-2007, 10:33 PM   PM User | #4
GSimpson
Regular Coder

 
GSimpson's Avatar
 
Join Date: Aug 2006
Location: New Zealand
Posts: 268
Thanks: 9
Thanked 0 Times in 0 Posts
GSimpson is an unknown quantity at this point
That's server problem, can't help you. I could make it spam proof, like a cookie for every 5 minutes you comment.
GSimpson is offline   Reply With Quote
Old 09-05-2007, 01:00 PM   PM User | #5
idalatob
Regular Coder

 
Join Date: Sep 2007
Location: Grahamstown, South Africa
Posts: 237
Thanks: 6
Thanked 17 Times in 17 Posts
idalatob is on a distinguished road
Often what I find is a in a comments page people will strip_tags on a variable then send it straight to the database. This means that if a guy puts a link (<a href="#">naughty angels</a> you will still have that entry stored in the database. Here is a small php snippet to stop that from happening.

PHP Code:
<?php 
$message 
"What ever you want it to be.";//From a form, whatever.
$message1 strip_tags($message);
if ((
strlen($message)) > (strlen($message1))){
    
$error true;
        echo(
"No tags allowed, fool");
        exit;
    } else {
    
//proceed with database addition, or whatever
    
}
?>

Last edited by idalatob; 09-05-2007 at 01:01 PM.. Reason: Add php tags
idalatob is offline   Reply With Quote
Old 09-05-2007, 01:05 PM   PM User | #6
Lightro
New Coder

 
Join Date: Mar 2007
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Lightro is an unknown quantity at this point
lol a guestbook only needs one file...

Just seems beond basic to me since I am developing a forums :P
Lightro is offline   Reply With Quote
Old 09-05-2007, 01:27 PM   PM User | #7
rafiki
Senior Coder

 
rafiki's Avatar
 
Join Date: Aug 2006
Location: Floating around somewhere...
Posts: 2,034
Thanks: 18
Thanked 42 Times in 42 Posts
rafiki will become famous soon enough
what? why? how?
this thread was made in april what was the point in bringing it back to life?
__________________
Get Firefox Now
rafiki is offline   Reply With Quote
Old 09-14-2007, 03:51 AM   PM User | #8
Bhodio
New to the CF scene

 
Join Date: Sep 2007
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Bhodio is an unknown quantity at this point
im using that at my site thanks so much
Bhodio is offline   Reply With Quote
Old 02-26-2008, 11:34 PM   PM User | #9
zeplin
New to the CF scene

 
Join Date: Feb 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
zeplin is an unknown quantity at this point
I noticed that at the bottom you made a reference to header index.php.

I am wanting to add the guest book to all my article pages I want the comments and form and article all to be on one page. Is this possible? thanks
zeplin is offline   Reply With Quote
Old 02-27-2008, 08:25 AM   PM User | #10
FlyFree
New to the CF scene

 
Join Date: Feb 2008
Posts: 8
Thanks: 2
Thanked 0 Times in 0 Posts
FlyFree is an unknown quantity at this point
I have a problem here. I wonder if anyone can solve it? When i run the script on localhost, saving all files as a php extension where data.php has no codings and the rest of the files has it into a directory called example.php (directory as in a folder and folder.php :O) When i type Nickname as ASD and posted ASD text, there isn't any content displayed other than the time and posted by seems empty too! As well, i opened the data.php file, not a folder. I used Dreamweaver to save it as a php file and it seems to be able to display nothing except the time, etc!
FlyFree is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:14 PM.


Advertisement
Log in to turn off these ads.