Dreevoo.com | Online Learning and Knowledge Sharing
 
Home | Programs | Programming languages | PHP & MySQL | Adding Comments in PHP
Guest
Click to view your profile
Topics
Programs
Languages
Recipes
Home
Shortcuts
 
 

Adding Comments in PHP

I will show you how to add comment functionality to websites using PHP programing language.

 
  Author: mat | Version: Dreamweaver CS5 | 15th November 2011 |  
 
 
1.
 

Open a new PHP document and save it as index.php.

 
 
2.
 

Click to choose the Design view.

 
 
3.
 

In the menu click Insert, Form and choose Textarea.

Enter comment_field into ID: field and click OK.

Click Yes if  "Add form tag?" window appears.

 
 
4.
 

Got to the end of the Textarea entry field and press Enter to go into the next line (see picture).

 
 
5.
 

In the menu click Insert, Form and choose Button.

Enter comment into ID: field and click OK.

 
 
6.
 

Now insert (copy/paste) the following code bellow the </form> tag:

<table border="1">
<tr>
<td width="161" bgcolor="#CCCCCC">Comments</td>
</tr>
<?php
$file_name = "comment_base.txt";
//write data
if (isset($_POST['comment_field'])) {
  //save data
  $file = fopen($file_name, 'a') or die("File failed to open");
  $comment = $_POST['comment_field']."\n";
  fwrite($file, $comment);
  fclose($file);
}

//read data
if (is_file($file_name)) {
  $file = fopen($file_name, 'r') or die("File failed to open");
  while (!feof($file)) {
       $line = fgets($file, 4096);
       if (strlen($line)>0) {
            echo "<tr><td>";
            echo htmlspecialchars($line,  ENT_QUOTES, 'UTF-8');
          echo "</td></tr>";
       }
  }
  fclose($file);
}
?>
</table>


 
 
7.
 

Let's see what the code actually does.

In the first part we enter the data

$file_name = "comment_base.txt";
We set the file which the comments will be stored in (comment_base.txt in my case)

if (isset($_POST['comment_field']))
Here we check if the comment was sent to a server. If not the if case does not allow to run the code.

$file = fopen($file_name, 'a') or die("File failed to open");
The fopen function open the file. The 'a' parameter keeps existing data saved.

$comment = $_POST['comment_field']."\n";
In $comment variable the content that was sent to a server gets saved.

fwrite($file, $comment);
The fwrite function writes the $comment value into the file.

fclose($file);
File gets closed.

 
 
8.
 

if (is_file($file_name)) {
The function checks if the file exists.

$file = fopen($file_name, 'r') or die("File failed to open");
Again we open the file, the parameter 'r' means, we will open it in a read only mode. In case of an error we get error message.

while (!feof($file)) {
       $line = fgets($file, 4096);
       if (strlen($line)>0) {
            echo "<tr><td>";
            echo htmlspecialchars($line,  ENT_QUOTES, 'UTF-8');
          echo "</td></tr>";
       }
  }

With the function while we read the data in the chunks of 4096 size usig the fgets function. We keep reading the file data until the eof condition is not fulfilled, meaning until the End Of File.

Function strlen verifies the length of a read line so we don't get empty lines displayed.

To display the comments, we used the echo function. From security reasons we added the
htmlspecialchars function which converts the characters alike > and < into text form and that way comments cannot be published as a HTML code.

fclose($file);
File gets closed.

 
 
9.
 

You can now upload the script to a PHP supporting web server and see how it works.

Click here to download the index.php file created in this lesson.

 
 
 
   
  Please login to post a comment
   
 
 
online learning made for people
Dreevoo.com | CONTRIBUTE | FORUM | INFO