php shoutbox

have you seen my shoutbox? no? there, it’s the thingy with text on the right of my website (duhh…). anyway, it looks nice isn’it? you want that php script? i wrote it with some help from my friends during my first encounter with php two years ago. the script has been upgraded to a better one since. this php shoutbox use flat file as a database. so, it’s easy to use, fully customizable and good to go anywhere you like provided your server supports php. it has been tested on windows 2003, windows xp and of course, linux and all it’s variants. this version have a ip logging and empty form detector…

<?php

$shout_file = "shouts.txt";

$num_to_display = 9;
$textsize = 9;
$hourdiff = "13";
$timeadjust = ($hourdiff * 3600);
$datetime_formatstr = "G:i/d.m.y";

$strip_tags = TRUE;
$allowed_tags = "<b>";
$limit = TRUE;
$max_length = 350;
$return_page = "index.php";
$div_width = "100%";
$win_w = $div_width;
$theip = $_SERVER[REMOTE_ADDR];

if(!file_exists($shout_file))
{
$file=fopen($shout_file,"w");
fclose($file);
}

if(isset($_POST[‘name’]) && isset($_POST[‘message’]))
{
$name=str_replace(array(""","’"),array(""","’"),$_POST[‘name’]);
$message=str_replace(array(""","’"),array(""","’"),$_POST[‘message’]);

$name=strip_tags($_POST[‘name’]);
$name=substr($_POST[‘name’],0,32);
if($strip_tags)
{
$message=strip_tags($_POST[‘message’],$allowed_tags);
}
if($limit)
{
$message=substr($_POST[‘message’],0,$max_length-1);
}

$time=time();

$out="newpostn".$theip."n".$time."n".$name."n".$message."nendpostn";

$file=fopen($shout_file,"r");
$in=fread($file,filesize($shout_file));
fclose($file);

$output=$out.$in;

$file=fopen($shout_file,"w");
fwrite($file,$output);
fclose($file);
header("Location: ".$return_page);
exit;
}

if(!isset($_GET[‘archive’]))
{

?>
<script language="javascript" src="shoutjava.js"></script>
<?php } ?>
<table border="0" width="80%" cellpadding="1" cellspacing="2"><tr><td>
<?php
$file=fopen($shout_file,"r");
$count=0;
while($file && !feof($file) && ($count < $num_to_display)) {
do {
$line=rtrim(fgets($file,4096));
}
while($line!="newpost" && !feof($file));
if(feof($file))
break;
$theip=rtrim(fgets($file,4096));
$time=rtrim(fgets($file,4096));
$name=rtrim(fgets($file,4096));
$message="";
$line=fgets($file,4096);
while(rtrim($line)!="endpost" && !feof($file))
{
$message=$message.$line;
$line=fgets($file,4096);
}
$datetime=date($datetime_formatstr,$time + $timeadjust);
$out="&raquo; ".$datetime."<br>[<b>".$name."</b>]n".$message."<br>n<br>n";

print($out);
if(!isset($_GET[‘archive’]))
$count++;
}
fclose($file);
if(!isset($_GET[‘archive’]))
{
print("<a
href="javascript:shouter(‘shoutbox.php?archive=show’,350,300)">archives</a>");

}
?>
</td></tr></table><br>
<?php if(!isset($_GET[‘archive’])) { ?>
<form action="shoutbox.php" method="post" name="shoutform">
<table border="0" width="80%">
<tr>
<td>name:</td>
<td><input type="text" size="11" name="name" maxlength="32"></td>
</tr>
<tr>
<td colspan="2">message:</td>
</tr>
<tr>
<td colspan="2"><textarea name="message" rows="2" cols="14"
maxlength="350"></textarea></td>
</tr>
<tr>
<td colspan="2"><input type="button" value="shout!" onclick="verify();"></td>

</tr>
</table></form>
<?php }

this php script requires a flat text file. for example, a file name “shouts.txt” and it must be chmod 777 or 666 (server writable). the script above is a modified version, so it also requires a javascript file. you can download all file here and see the source code here. to include it to your page, your page must have a .php extension and included into your page by putting this:

<?php include("shoutbox.php"); ?>

you can modify it to your style and needs. if you need some help regarding this, don’t hesitate to leave a comment or contact me.

enjoy and good luck.

One thought on “php shoutbox

Leave a Reply

Your email address will not be published. Required fields are marked *