Left Quote    I find that the harder I work, the more luck I seem to have.
- Thomas Jefferson    
Right Quote
[login] | [Register]

Comments Script

by: bs0d


This is a complete comments script. To learn more about the code, how it works, and exactly what to do to get this working on your own site - please read the corresponding tutorial to learn how its made.This script requires a mySQL database.


//connect to your database

//query comments for this page of this article
$inf "SELECT * FROM `comments` WHERE page = '".stripslashes($_SERVER['REQUEST_URI'])."' ORDER BY time ASC";
$info mysql_query($inf);
$info) die(mysql_error());

$info_rows mysql_num_rows($info);
$info_rows 0) {
'<table width="95%">';
$info2 mysql_fetch_object($info)) {    
'<td>"'.stripslashes($info2->subject).'" by: <a href="'.$info2->contact.'">'.stripslashes($info2->username).'</a>
</td> <td><div align="right"> @ '
.date('h:i:s a'$info2->time).' on '.$info2->date.'</div></td>';
'<td colspan="2"> '.stripslashes($info2->comment).' </td>';
//end while
echo '</table>';
'<hr width="95%" noshade>';
} else echo 
'No comments for this page. Feel free to be the first <br>';

$_POST['submit'])) {
addslashes($_POST['username'])) die('<u>ERROR:</u> you must enter a username to add a comment.');
addslashes($_POST['contact']))  die('<u>ERROR:</u> enter contact method in contact field.');
addslashes($_POST['subject']))  die('<u>ERROR:</u> enter a subject to your comment.');
addslashes($_POST['comment']))  die('<u>ERROR:</u> cannot add comment if you do not enter one!?');

//this is for a valid contact 
if(substr($_POST['contact'],0,7) != 'mailto:' && !strstr($_POST['contact'],'//')) {
$_POST['contact'] = "mailto:".$_POST['contact']."";
$_POST['contact'] = "http://".$_POST['contact']."";
//end valid contact

//try to prevent multiple posts and flooding...
$c "SELECT * from `comments` WHERE ip = '".$_SERVER['REMOTE_ADDR']."'";
$c2 mysql_query($c);
$c3 mysql_fetch_object($c2)) {
$difference time() - $c3->time;
$difference 300) die('<u>ALERT:</u> '.$c3->username.', You have already commented earlier; if you have a question, try the forums!<BR>');
//end while

//add comment
$q ="INSERT INTO `comments` (article_id, page, date, time, username, ip, contact, subject, comment) 
.$_GET['id']."', '".$_POST['page']."', '".$_POST['date']."','".$_POST['time']."', '".addslashes(htmlspecialchars($_POST['username']))."', 
.$_SERVER['REMOTE_ADDR']."', '".addslashes(htmlspecialchars($_POST['contact']))."',
.addslashes(htmlspecialchars($_POST['subject']))."', '".addslashes(htmlspecialchars(nl2br($_POST['comment'])))."')";

$q2 mysql_query($q);
$q2) die(mysql_error());

//refresh page so they can see new comment
header('Location: http://' $_SERVER['HTTP_HOST'] . $_POST['page'] . "#comments");

} else {  
//display form
<form name="comments" action="<? $_SERVER['PHP_SELF']; ?>" method="post">

<input type="hidden" name="page" value="<? echo($_SERVER['REQUEST_URI']); ?>">
<input type="hidden" name="date" value="<? echo(date("F j, Y.")); ?>">
<input type="hidden" name="time" value="<? echo(time()); ?>">

<table width="90%" border="0" cellspacing="0" cellpadding="0">
      <td><div align="right">Username:   </div></td> 
       <td><input name="username" type="text" size="30" value=""></td>
      <td><div align="right">Contact:   </div></td>
      <td><input type="text" name="contact" size="30" value=""> <i>(email or url)</i></td>
    <td><div align="right">Subject:   </div></td>
    <td><input type="text" name="subject" size="30" value=""></td>
      <td><div align="right">Comment:   </div></td>
      <td><textarea name="comment" cols="45" rows="5" wrap="VIRTUAL"></textarea></td>
      <td colspan="2"><input type="reset" value="Reset Fields">     
        <input type="submit" name="submit" value="Add Comment"></td>
// end else



  Subject: "database name?" Date: Sep 25 2007 at 1:38 pm    
wht is database name, which created in mysql?
  Subject: "Code seems to not work on localhost" Date: Dec 10 2007 at 5:31 pm    
What am I missing? The form will not same data once submitted. Error in PHP is in line 7. (access denied (ODBC@localhost )
  Subject: "error removed" Date: Dec 10 2007 at 5:34 pm    
Found that I needed to ad a User of ODBC to the dB, but form still does not save to dB. Why?
  Subject: "sign-in" Date: Sep 12 2008 at 9:47 pm    

would i post the full code into each page to have a comment for each page?

how would i make the comments only accessed by registered members and viewable by everyone.
  Subject: "members" Date: Sep 13 2008 at 11:11 pm    

i got comments working. i just want to connect the comments to my member login.
  Subject: "form" Date: Nov 24 2008 at 5:43 am    
that's form being 1 page with the execution?
could you explain more ...

thank you
  Subject: "nl2br error" Date: Jun 21 2009 at 1:25 am    

addslashes(html specialchars(nl 2br($_POST['co mment'])))

need to be changed to

addslashes(nl2b r(htmlspecialch ars($_POST['co mment'])))

to include submitted line breaks properly.
  Subject: "displaying comment twice" Date: Aug 25 2009 at 3:35 am    
hi i find your comments script very helpful. but i have a problem when i refresh the page twice or open the page again, it will display again the last comment i posted.

please help. thanks
  Subject: "displaying comment twice" Date: Aug 25 2009 at 3:46 am    
actually it doesn't display the comment when i posted so i need to refresh the page to display it
  Subject: "use header" Date: Aug 26 2009 at 2:15 am    

this code:
//refresh page so they can see new comment
header('Locati on: http://' . $_SERVER['HTTP _HOST'] . $_POST['page' ] . "#comment s");

should be refreshing the page after a comment is posted. Make sure you didn't comment it out or anything.
  Subject: "Two askings" Date: Sep 21 2009 at 1:12 pm    
Thanks for that script.

I would like to ask you how to make on one page displayed just for example 20 comments.

Thanks a lot
  Subject: "Paginate Comments?" Date: Sep 26 2009 at 3:00 am    

 vladin ko0 said...
< i>Thanks for that script.

I would like to ask you how to make on one page displayed just for example 20 comments.

Thanks a lot

Perhaps you're talking about paginating the comments for a single page of content? This might help:
Pagination then just limit the results to 20 /per page.
  Subject: "If logged in" Date: Nov 11 2009 at 4:23 pm    
I would like to know how to implement the code
if(isset($_SESS ION['loggedin ']))
header('Locati on: comments.php') ;
echo "You have to be logged in to post comments." ;;


into this script, provided it's accurate one, and then display only the comment form without user name, contact, subject, etc. The subject would be the id of the page I assume. I am so new to php and am really struggling. Your help would be appreciated.
  Subject: "Comment Script Tutorial" Date: Nov 14 2009 at 3:11 am    

I think the comment script tutorial covers this. Did you find the link at the top of this page?
  Subject: "need help please" Date: Aug 05 2011 at 8:58 am    

can u give a full page script for this ./.i mean the "no error outcome of this script" the comment.php.. . ..ty
  Subject: "re: need help please" Date: Aug 08 2011 at 2:14 am    

Everything you need is here.
You Must be signed in or a member to comment.

Code Stats

Code Stats

17 Total Comments
4 Rating of 5 ( Votes)


Code Options

· Login to Rate This Code
· Login to Post a Comment
· Read more by this author
Digg This Code! Bookmark This Code Reddit: Bookmark This Code! BlinkList: Blink This Code! YahooMyWeb BlogMarks: Add This Mark! Furl: Save This Code Spurl: Mark This Code!

Code Samples


· Database Connection
· Users Online
· Pagination
· IdealMySQL Class for PHP 5.0

"" Copyright © 2002-2020; All rights lefted, all lefts righted.
Privacy Policy  |  Internet Rank