Adding a comment system to my site

Hi everyone, I’m trying to add some kind of comment system to my site for the news that I publish on the main page. I would like someone to comment on this (they do not need to log in). The comment submission form simply requires a name and comment. What would be the easiest way to do this? It would be nice if it would display how many comments are currently in the news feed. I don't need anything fancy. Thanks!

+4
source share
6 answers

This is a simple tutorial for php guest books . I know that this is not what you asked for, but very similar. Customize it according to your needs. PS: I would really think about what eykanal wrote. You will need to add a column for the article id, page id, etc ...

Subsequent:

As already mentioned, this example / tutorial is not the safest / correctly designed part of the work and is intended only to show you the level of difficulty and give you an idea of ​​what you are getting. After reading, I would also read a little about php / mysql injection vulnerabilities and simple db design principles

+2
source

If you do not want to code your own comment system, you can use http://disqus.com/

+7
source

There are many ways to do this ... I am trying to give you a simple idea (without using templates or a complex method):

1) Create a database table to contain comments, I suggest the following fields:

id (integer) news_id (fk to id of the news) date (ie Timestamp) name (varchar(30... or less)) message (text) 

2) On your FrontEnd page, add a form consisting of 4 fields: news-id: without explanation name: enter a text field message: textarea captcha: (to avoid bot completion), I suggest you recaptcha .

 <form action="add_comment.php" method="POST"> <label for="name">Your Name:</label> <input type="text" id="name" name="name" /> <label for="name">Your Comment:</label> <textarea id="comment" name="comment"></textarea> <input type="hidden" name="news_id" value="<?php echo $news_id?>"/> <input type="submit" value="Ok" name="save"/> </form> 

This form sends data to POST in add_comment.php, which must complete the following steps:

2.A) check if $ _POST data exists

 if(isset($_POST["save"])) 

It would be better to check the origin of the data (to be sure that it is from your site).

2.B) If $ _POST data exists, check the required fields and save the error in some structure:

 if( (trim($_POST["name"]) == "") and (strlen($_POST["name"]) < 5) ){ $name_error = true; } 

2.C) If there are no errors, save the data in the database: - open the db connection - collect the request. Do not forget to wrap each variable in quotation marks and run it through mysql_real_escape_string (or use prepared statements) - run this query
- redirect to current page

2.D) If there are errors, redirect to the main page with the variable in get & error = 1. Make sure that this variable is set to determine whether to print some error messages. (it is better to stay on one page and display errors, as well as fill in the data entered ( avoid xss scripts ))

3) Manage your main page by adding a script to select a comment from the database, here are a few steps:

3.A) . For each newsletter you print, you will receive an identifier (or a unique key used to store news in db).

3.B) Complete a simple selection request to get the full comment for this news:

 $query = "SELECT name,message from comments where id_news = '{$_newsid}' order by date DESC"; 

3C) . For each comment received using this request, you can print the data as follows:

 <?php foreach($query_fetched_results as $comment):?> <p class='name'><?php echo $comment['name'];?></p> <p class='comment_body'><?php echo $comment['message'];?></p> <?php endforeach;?> 

4) Checking the number of comments is quite simple, perform a calculation of the data received from the request at point 3B.

+3
source

Besides handling spam, it's pretty simple. This is a great training exercise for the first time.

To save comments ...

In your HTML, you create a form with comment fields. Submit the form using POST.

In your PHP, read the fields from $ _POST and check for certainty - the name is x characters, but not empty, etc. Run your user data through mysql_real_escape_string () and combine them into an INSERT query string. Run the query. The key idea here is to not trust users - check them out every thing you can think of. There's a stack overflow load on this.

To display comments ...

Run a SELECT query that looks for an identifier that matches your publication. Print the names and comments into a string (using strip_tags () to remove unwanted HTML from the comments), presenting your own HTML layout as needed. You can easily count comments when outputting each comment in a string. Then display the formatted comments on the page.

There are many options to consider along the way - do you want to allow specific HTML? How do you deal with inevitable spam? Are you viewing comments, allowing avatars, gravitators, email addresses, poster URLs, etc.? Do you make messages through any system or just build pages?

EDIT: fixed my suggestion about mysql_real_escape_string (). Thanks to Colonel Shrapnel for pointing out my mistake.

+2
source

you can use IntenseDebate .

features:

Comment Threading

Improve the conversation within the commentary and respond directly to the individual commentary. rugged answers give the following conversations are manageable.

Reply-By-Email

Reply and comment on ease comments via email, even if you are on the go. Just because you are away from your computer does not mean the conversation stops.

Email Notifications

Commentators receive email alerts when a response to their comment is posted, linking them directly to the response. Add an e-mail along with the ability to subscribe to all comments and let the debate go!

NtenseDebate Functions

Email Notifications

Commentators receive email alerts when a response to their comment is posted, linking them directly to the response. Add an e-mail along with the ability to subscribe to all comments and let the debate go!

Commentator Profiles

Commentator profiles allow you and your readers to learn more about each other. See how the conversation goes to new when you and your readers are able to get to know each other. do not forget, their universal profiles can be used on any site with Intensedebate!

Moderation / Blocking

IntenseDebate offers really hardcore moderation options. Customize your settings will be reduced by keywords, number of links, email for comments, and / or IP addresses.

Comment Reputation and Vote

Your readers will begin to build their reputation as commentators when they create an IntenseDebate account. Their reputation score is based on the quantity, and more importantly, the quality, of the comments they made on all IntenseDebate sites. Bring quality comments cutting edge.

Plugin API

We opened our code to developers to submit their own creations to the debate. These enhancements include Seesmic video comments, PollDaddy polls, YouTube videos, emoticons, and more. interested in creating your own settings? Check out our plugin API.

Openid

Your readers can easily comment on comments using their OpenID. They can link their OpenIDs to an IntenseDebate profile so they don’t have to worry about remembering another set of credential logins.

Widgets

We built some fashionable widgets based on your feedback. You can display the statistics of blog comments, the most recent comments made on your blog, the most popular posts, the most recent comments you made, and even who top IntenseDebaters.

Twitter

Give your commentators the opportunity to post simultaneous tweets when they leave a comment. This is a great way to let your commentators spread the word about your site and engage in new traffic and comments!

Facebook Connect

Open the discussion and let Facebook comments on your site using our Facebook Connect integration!

RSS Readers and Tracking

RSS readers make life simple. This is why we integrated IntenseDebate comments using Google Reader and Bloglines (with many RSS readers on the way) so that you can read and post comments directly from your RSS reader.

+1
source

NOT RECOMMENDED The main form of PHP in which comments are stored in a text file:

a) Copy and paste below the code into your "php file" where you want to display the comment form.

b) Create the file "comments.txt" in the same folder where you have your "php file".

 <?php $name = $_POST['name']; $comment = $_POST['comment']; if ($_POST) { $handle = fopen("comments.txt", "a"); fwrite($handle, $name . ":<br/>" . $comment . "<br/>"); fclose($handle); } ?> <form method="post"> Name:<input type="text" name="name"><br/> Comment:<textarea name="comment"></textarea><br/> <input type="submit" name="submit" value="Post"> </form> <?php include "comments.txt"; ?> 
0
source

All Articles