GIDForums  

Go Back   GIDForums > Computer Programming Forums > MySQL / PHP Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rating: Thread Rating: 45 votes, 5.00 average.
  #1  
Old 22-Mar-2003, 21:09
conkermaniac conkermaniac is offline
Member
 
Join Date: Dec 2001
Location: China
Posts: 174
conkermaniac is on a distinguished road

Guestbook


Hi,

I'm working on a guestbook for a website, and I'm almost finished. I'm dedicated to making this the best guestbook ever, and so far, I have added tons of features. Users can edit their own entries, customize the color of their entry, add emoticons (and maybe an avatar), and of course all the basic features of any guestbook. Now, I face one problem...how do I prevent users from submitting an entry? I know that I have to track IPs and set cookies in the user's browser, but I have no clue how to do this. Could anyone help me? Thanks!
__________________
You're not supposed to be looking at this.
  #2  
Old 23-Mar-2003, 02:44
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough

Setting Cookies with PHP


I am sure you meant "how do I prevent users from submitting multiple entries?"...

I will have to assume you are using a database table or 2 to manage this Guestbook since you did not specify if this is true. If yes, just add a new column for the IP address (in the table where you store the posts). I am certain this table already includes a timestamp column - so you can use that.

What you want the script to do when someone posts is to:
  1. Check if a cookie for the script exists.
  2. Failing that, check if the poster's IP is matched within your table in the last 24 hours, for example. (24 hours, 1 week, 1 month, it all depends how frequent you want to see entries by the same 'person').
  3. Failing that, add the post to the db and set a cookie!

Setting a cookie with PHP
Let's assume a few things first:
Guestbook script is named BestGB.
URL / folder to the script is http://www.example.net/gb/

PHP Code:

<?php

// Setting a cookie when the user has successfully submitted an entry
// into our Guestbook.

setcookie( 'BGB_Data[ts]', $thetime, time()+60*60*24, '/gb/', '.example.net', 0 ); 

// 'BGB_Data[ts]' = the cookie name as an array; so you may add more data at a later stage
//                  if you want.
// $thetime = some data / value to create, verify or modify (depending how you want to
//            manage cookies)
// time()+60*60*24 = Cookie expires in 1 day.
// '/gb/' = the path, iow the cookie is available only within this folder and subfolders
//          therein.
// '.example.net' = so that the cookie is available regardless of whether the user uses
//                  http://www.example.net/gb/ or http://example.net/gb/ to get to your
//                  script.

?>


Verify a cookie with PHP
To verify if a cookie contains valid data or if it was even set (you can do either), code like this will help.
PHP Code:

<?php

// Before we add this guestbook entry to our db, we
// verify that a cookie already exist (easy)
if( isset($_COOKIE['BGB_Data']['ts']) ):
  echo '<h1>Our records show that you already posted recently</h1>';
  exit;
endif;

// or verify that a cookie and the data we seek was already set (just a little less easy)
if( isset($_COOKIE['BGB_Data']['ts']) && $_COOKIE['BGB_Data']['ts'] >= time()-60*60*24 ):
  echo '<h1>Our records show that you already posted recently</h1>';
  exit;
endif;
?>


If you find yourself having problems setting a cookie midway in your script, put the following line right at the top of your script; I am not sure if it would work but let me know...

PHP Code:

<?php
ob_start();

// the rest of your script below
?>


The IP is straight forward using the $_SERVER['REMOTE_ADDR'] to put into the db and to verify (together with the timestamp) later on...

Off topic: If you allow Guestbook entries to be edited by your readers, you will be creating a monster. Better to allow edits only within a span of 1 hour from the time of the initial post, for example.
  #3  
Old 23-Mar-2003, 03:38
jrobbio's Avatar
jrobbio jrobbio is offline
Regular Member
 
Join Date: Jan 2003
Location: Loughborough, England
Posts: 840
jrobbio will become famous soon enough
This really is a helpful forum
  #4  
Old 25-Mar-2003, 03:25
conkermaniac conkermaniac is offline
Member
 
Join Date: Dec 2001
Location: China
Posts: 174
conkermaniac is on a distinguished road
WOW! Thank you very much, JdS. I will try it as soon as I get home.

I think you should post another copy of that post in the Code database.

(Oops...I did mean to write "multiple entries". Of course I want the viewers to submit entries. )
__________________
You're not supposed to be looking at this.
  #5  
Old 25-Mar-2003, 09:04
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough
Quote:
Originally posted by conkermaniac
I think you should post another copy of that post in the Code database.

It is my hope that the popular Search Engines will take care of it for us... incase nothing appears next month in the SERP to this thread then I will make some changes like you suggest (no point confusing the bots by having duplicate pages until then).
 
 

Recent GIDBlogInstall Adobe Flash - Without Administrator Rights by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Guestbook error BobbyDouglas Web Design Forum 1 16-Oct-2003 22:17

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 10:39.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.