PHP - Comments box

From Global Programming Syntax

Jump to: navigation, search

On some of your basic webpages you may like to allow visitors to leave comments behind for others to view. This can be done with a comments box. A comments box is simple to embed and will this script does not even require MySQL. Instead this script just records the messages into a text file then php reads the text file to view the messages. Also to prevent people from recording private information from your site, this comments box does not input html code but instead bb code. So first to install this script, you will need to copy the below script into a page of your desire.

<?
//display comment box
include("config.php");
if (file_exists($commentbox['file']))
{
$data=file_get_contents($commentbox['file']);
} else { $data=''; }
if (isset($_POST['commentbox']) && isset($_POST['submit']))
{
$comment=$_POST['commentbox'];
$comment=str_replace('<','&#60;',$comment);
$comment=str_replace('>','&#62;',$comment);
$comment=preg_replace('/\[b\]/is','<b>',$comment);
$comment=preg_replace('/\[\/b\]/is','</b>',$comment);
$comment=preg_replace('/\[i\]/is','<i>',$comment);
$comment=preg_replace('/\[\/i\]/is','</i>',$comment);
$comment=preg_replace('/\[u\]/is','<u>',$comment);
$comment=preg_replace('/\[\/u\]/is','</u>',$comment);
$comment=preg_replace('/\[centre\]/is','<center>',$comment);
$comment=preg_replace('/\[\/centre\]/is','</center>',$comment);
$comment=preg_replace('/
/is'
,'<br>',$comment); //requires new line for code to work.
$comment=preg_replace('/\[url\=(http\:\/\/|https\:\/\/)(.*?)\]/is',
'<a href=$1$2><font color=<commentbox["linkcolor"]>>',$comment);
$comment=preg_replace('/\[url\=(.*?)\]/is','<a href=http://$1><font color=<commentbox["linkcolor"]>>',$comment);
$comment=preg_replace('/\[\/url\]/is','</font></a>',$comment);
$comment=preg_replace('/\[size\=(.*?)\]/is','<font size=$1>',$comment);
$comment=preg_replace('/\[\/size\]/is','</font>',$comment);
$comment=preg_replace('/\[style\=(.*?)\]/is','<font face="$1">',$comment);
$comment=preg_replace('/\[\/style\]/is','</font>',$comment);
$data.='<>'.$comment;
file_put_contents($commentbox['file'],$data);
unset($comment);
unset($_POST['commentbox']);
}
echo "
<script>
<!-- Begin
function insertAtCursorb(myField, myValue) {
//IE support
if (document.selection) {
myField.focus();
sel = document.selection.createRange();
sel.text = myValue;
}
//MOZILLA/NETSCAPE support
else if (myField.selectionStart || myField.selectionStart == '0') {
var startPos = myField.selectionStart;
var endPos = myField.selectionEnd;
myField.value = myField.value.substring(0, startPos)
+ myValue
+ myField.value.substring(endPos, myField.value.length);
} else {
myField.value += myValue;
}
}
 
function formHandler(commentboxform){
var URL = document.commentboxform.style.options[document.commentboxform.style.selectedIndex].value;
//window.location.href = URL;
insertAtCursorb(document.commentboxform.commentbox,URL);
}
 
function formHandlerb(commentboxform){
var URL = document.commentboxform.format.options[document.commentboxform.format.selectedIndex].value;
insertAtCursorb(document.commentboxform.commentbox,URL);
}
// End -->
 
function insertAtCursor(myField, myValue) {
//IE support
if (document.selection) {
myField.focus();
sel = document.selection.createRange();
sel.text = myValue;
}
//MOZILLA/NETSCAPE support
else if (myField.selectionStart || myField.selectionStart == '0') {
var startPos = myField.selectionStart;
var endPos = myField.selectionEnd;
myField.value = myField.value.substring(0, startPos)
+ myValue
+ myField.value.substring(endPos, myField.value.length);
} else {
myField.value += myValue;
}
}
</script>
"
;
 
if (file_exists($commentbox['file']))
{
$data=str_replace('<commentbox["linkcolor"]>',$commentbox['linkcolor'],$data);
if ($data=='')
{
$data='No comments have been posted.';
}
$data=explode('<>',$data);
$id=0;
$colorid=0;
echo "<table border=0 cellpadding=5 cellspacing=0>";
while (isset($data[$id]))
{
if ($colorid==2) { $colorid=0; }
if ($colorid==0)
{
echo "<tr><td bgcolor='".$commentbox['color1']."'>".$data[$id]."</td></tr>";
} else {
echo "<tr><td bgcolor='".$commentbox['color2']."'>".$data[$id]."</td></tr>";
}
 
$colorid+=1;
$id+=1;
}
} else {
echo "<table border=0 cellpadding=5 cellspacing=0><tr><td bgcolor='".$commentbox['color2']."'>
No comments have been posted.</td></tr>"
;
}
 
echo "
 
<tr><td><form name=commentboxform id=commentboxform method=post style='margin:0; padding:0;'>"
;
if (isset($_POST['edit']))
{
echo "<input type=hidden value='".$_POST['edit']."' name='editnew'>";
} else if ($_GET['mode']!=='create' && $_GET['mode']!=='delete') {
echo "<input type=hidden value='".$_POST['editnew']."' name='editnew'>";
}
echo "<font size=5><b>".$sqlerror."</b></font>";
if ($_SESSION['htmlcomment']==' ')
{
$_SESSION['htmlcomment']=='';
}
 
echo "<table border=0 cellpadding=0 cellspacing=0>
<tr><td align=center width=275><select name='style' size=1 onChange='javascript:formHandler()' style='width:150;' align=left>
<option value=''>Text Style</option>
<option value='[style=arial][/style]'>Arial</option>
<option value='[style=arial black][/style]'>Arial Black</option>
<option value='[style=courier new][/style]'>Courier New</option>
<option value='[style=comic sans ms][/style]'>Comic Sans MS</option>
<option value='[style=tahoma][/style]'>Tahoma</option>
<option value='[style=times new roman][/style]'>Times New Roman</option>
<option value='[style=verdana][/style]'>Verdana</option>
</select>
<select name='format' size=1 onChange='javascript:formHandlerb()' style='width:125;'>
<option value=''>Format Text</option>
<option value=''></option>
<option value='[i][/i]'>Italics</option>
<option value='[b][/b]'>Bold</option>
<option value='[u][/u]'>Underline</option>
<option value='[size=4][/size]'>Text Size</option>
</select>
 
 
</td><td align=right>
<input TYPE='button' VALUE='Insert Link' ONCLICK="
.'"'."javascript:insertAtCursor
(document.commentboxform.commentbox, '[url=http://www.example.com]link text[/url]');"
.'"'." style='width:90;'>
</td><td align=right>
<input TYPE='button' name='addpicwindow' VALUE='Centre text' ONCLICK="
.'"'."javascript:insertAtCursor
(document.commentboxform.commentbox, '[centre][/centre]');"
.'"'." style='width:90;'>
</td></tr><tr><td colspan=6>
<textarea name=commentbox id=commentbox ROWS='16' COLS='90'>"
.$_POST['commentbox']."</textarea><br>
<table border=0 cellpadding=0 cellspacing=0 width=100%><tr><td colspan=2 align='right'>
<input type=submit value='Submit' name='submit'><input type=submit value='Preview' name='preview'>
</td></tr><tr><td colspan=2 height=20></td></tr></table>
</td></tr></table>
</form>"
;
 
echo "</td></tr>";
if (isset($_POST['commentbox']) && isset($_POST['preview']))
{
$comment=$_POST['commentbox'];
$comment=str_replace('<','&#60;',$comment);
$comment=str_replace('>','&#62;',$comment);
$comment=preg_replace('/\[b\]/is','<b>',$comment);
$comment=preg_replace('/\[\/b\]/is','</b>',$comment);
$comment=preg_replace('/\[i\]/is','<i>',$comment);
$comment=preg_replace('/\[\/i\]/is','</i>',$comment);
$comment=preg_replace('/\[u\]/is','<u>',$comment);
$comment=preg_replace('/\[\/u\]/is','</u>',$comment);
$comment=preg_replace('/\[centre\]/is','<center>',$comment);
$comment=preg_replace('/\[\/centre\]/is','</center>',$comment);
$comment=preg_replace('/
/is'
,'<br>',$comment); //requires new line for code to work.
$comment=preg_replace('/\[url\=(http\:\/\/|https\:\/\/)(.*?)\]/is',
'<a href=$1$2><font color='.$commentbox['linkcolor'].'>',$comment);
$comment=preg_replace('/\[url\=(.*?)\]/is','<a href=http://$1><font color='.$linkcolor.'>',$comment);
$comment=preg_replace('/\[\/url\]/is','</font></a>',$comment);
$comment=preg_replace('/\[size\=(.*?)\]/is','<font size=$1>',$comment);
$comment=preg_replace('/\[\/size\]/is','</font>',$comment);
$comment=preg_replace('/\[style\=(.*?)\]/is','<font face="$1">',$comment);
$comment=preg_replace('/\[\/style\]/is','</font>',$comment);
 
echo "<tr><td bgcolor='".$commentbox['color1']."'>".$comment."</td></tr>";
}
 
echo "</table>";
?>

Now that you have the core of the script, you need to make another file for the settings. This new file by default is called 'config.php' (without the quotes) and is placed in the same directory/folder as the above script. So copy and past the below script into config.php:

<?
$commentbox['file']='comments.txt'; //name of text file
$commentbox['color1']='#DDDDDD'; //message background color 1
$commentbox['color2']='#EEEEEE'; //message background color 2
$commentbox['linkcolor']='#0000FF'; //link color
?>

After copying those 2 files into the appropriate locations and configuring the config.php file, you will then have an active comments box. To delete or alter messages, because this script is so basic, you will need to just alter the text file associated with this script.

Personal tools
languages
page stats
Toolbox