Check out the example page or download source code.
/* get comments from file */
$commentsText = file_get_contents('comments.txt');
/* create array list from comments */
$commentsList = json_decode($commentsText,true);
/* check if new comment is posted and minimum 3 characters are set */
if( !empty($_POST['comment']) && strlen($sComment) > 3 ){
/* get posted comment and remove all HTML */
$sComment = strip_tags($_POST['comment']);
/* add comment, client IP and date to array */
$commentsList['comments'][] = array(
'text' => $sComment,
'ip' => $_SERVER['REMOTE_ADDR'],
'date' => time()
);
/* convert comments to string */
$commentsText = json_encode($commentsList);
/* save comment to file */
file_put_contents($commentsFile, $commentsText);
}
/* create html list */
$commentsHTML = "<ul>";
/* loop all comments */
foreach( $commentsList['comments'] as $commentItem ){
// add comment to html list
$commentsHTML.= "<li>" . $commentItem['text'] . "</li>";
}
/* close html comments list */
$commentsHTML .= "</ul>";
I know simple but i thought to share it, you can see the full example including sorting and some more validation stuff, or check out the example page.