Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple jSon PHP comments
<?php
/*
* comments.php
*
* Copyright 2012 kib <kib@sofasurfer.org>
*
*
*
*/
/* path to comments data file */
$commentsFile = 'data/comments.txt';
/* variable for error message */
$errorMessage = false;
/**
* Function to sort comments list by date */
function cmp($a, $b){
return strcmp($b['date'], $a['date']);
}
/* check if comments file exist */
if( file_exists($commentsFile) ){
/* get comments from file */
$commentsText = file_get_contents($commentsFile);
/* create array list from comments */
$commentsList = json_decode($commentsText,true);
}
/* check if new comment is posted */
if( !empty($_POST['comment']) ){
/* get IP address */
$happyPersonIp = $_SERVER['REMOTE_ADDR'];
/* get posted comment */
$sComment = strip_tags($_POST['comment']);
/* check if posted message is valid */
if( !empty($sComment) ){
/* show error */
$errorMessage = "You should add some comment!";
}else{
/* add comment to array */
$commentsList['comments'][] = array(
'text' => $sComment,
'ip' => $happyPersonIp,
'date' => time()
);
/* convert comments to string */
$commentsText = json_encode($commentsList);
/* save comment to file */
file_put_contents($commentsFile, $commentsText);
}
}
/* check if comments exist */
if( !empty($commentsList) ){
/* reverse array so latest comment is first */
$commentsList = array_reverse($commentsList,true);
/* sort list by date */
usort($commentsList['comments'], "cmp");
/* 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>";
}
?>
<html>
<head>
<title>Simple comments | Happy Code | sofasurfer.ch</title>
<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico">
<STYLE type="text/css">
/* some happy style */
body{font-family:cursive;}
ul {list-style:none;}
li {padding:20px;}
.error{color:red;}
</STYLE>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-3456716-9']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</head>
<body>
<center>
<img src="img/head.jpg" alt="Be Happy Happy" />
<form id="comments" method="POST">
<h1>What Do You Want?</h2>
<div class="error"><?=$errorMessage?></div>
<textarea id="comment" name="comment" cols="70" style="font-family:cursive;font-size: 18px;font-weight:bold;"> </textarea><br/>
<input type="submit" value="this" onclick="_gaq.push(['_trackPageview', '/tracking/comment-submit']);" />
<input type="submit" value="nothing" onclick="document.getElementById('comment').value='no';" />
<?=$commentsHTML?>
</form>
<p>written by <a href="http://blog.sofasurfer.ch/2011/03/04/php-simple-comments-readwrite-json-data-to-text-file/">blog.sofasurfer.ch</a> download source code <a href="http://storage.sofasurfer.org/download/comments.tar.gz" target="_blank">comments.tar.gz</a></p>
</center>
</body>
</html>
@androidovshchik

This comment has been minimized.

Copy link

androidovshchik commented Feb 2, 2017

Hi
Please fix an error on 44 string

if (empty($sComment)) {

It must be
Awesome! The most simple comments i have ever seen

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.