Shout Boxes are quite a popular widget that people put on their web site or blog. It gives peoples a very easy way to interact with each other.
In case you don’t know what shout box is, you can think of it as a chat script that you can put on a part of an existing web page. People can leave their messages (usually short) there and can interact with each other and with the webmaster. One special feature is that anybody can leave their message without any registrations. That’s why it’s the perfect way to interact with your visitors and let the visitors interact with each other.
Designing a simple shout box script is actually not difficult. In this post we’re going to design one.
A shout box usually needs the user to fill name (optional), URL (optional) and message to be posted. When an URL is provided ‘name’ becomes a hyperlink.
As is obvious, the messages that people post must be saved somewhere. Again Database is the best choice but for now we’ll use a file instead.
As each message consists of three information out of which two are optional, we’ll use the following format to store messages in the file.
<name>
<url>
<msg>
<name>
<url>
<msg>
…
In the case when any optional field is not provided a place holder will be stored in its place to stick with the format.
Enough talking, now have a look at the code:
<html>
<body>
<form name="form1" id="form1" method="post"
action="shout_box.php">
<table width="100%" border="0" cellspacing="0"
cellpadding="0">
<tr>
<td width="22%"><font size="2">Name:</font></td>
<td width="78%"><input name="name"
type="text" id="name" /></td>
</tr>
<tr>
<td><font size="2">URL: </font></td>
<td><input name="url" type="text" id="url"
value="http://" /></td>
</tr>
<tr>
<td><font size="2">Message</font></td>
<td><input name="msg" type="text"
id="msg" /></td>
</tr>
<tr>
<td><input name="say" type="submit"
id="say" value="Say" /></td>
<td><input type="reset" name="Submit2"
value="Reset" /></td>
</tr>
</table>
</form>
<?php
//if 'say' button was pressed
if(isset($_POST['say']))
{
//have the sent data
//striping out HTML tags
//THIS IS IMPORTANT FOR THIS
//TYPES OF APPLICATIONS
//BECAUSE SPAMMERS TRY TO
//INJECT CODES
$name=strip_tags($_POST['name']);
$url=strip_tags($_POST['url']);
$msg=strip_tags($_POST['msg']);
//if name feild was empty
if($name=='')
$name="Unknown";
//if no URL was submitted
if(trim($url)=='http://')
$url='NONE';
//if msg field was not blank
if($msg!='')
{
//make the 'data' variable
//triming any excess spaces
$data=trim($name)."\n";
$data.=trim($url)."\n";
$data.=trim($msg)."\n";
//open the file and have data
//in an array
$file_ar=file("shout_box.txt");
//overwrite the file
$fp=fopen("shout_box.txt","w");
//first put the data that
//was sent by the form
fputs($fp,$data);
//if the original file had
//contents, append it to the
//end of the file
//THIS IS TO HAVE THE LATEST
//MESSAGE AT THE TOP
if($file_ar!=NULL)
{
$loop=0;
foreach($file_ar as $line)
{
//store only 20
//messages MAX
if($loop>=19*3) break;
fputs($fp,$line);
$loop++;
}
}
fclose($fp);
}
}
//just show the messages
$fp=fopen("shout_box.txt","r");
while(!feof($fp))
{
$name=trim(fgets($fp,999));
$url=trim(fgets($fp,999));
$msg=trim(fgets($fp,999));
if($name!='')
{
//if no URL is there
if(strstr($url,'NONE'))
echo "<p><b>$name: </b>$msg</p>";
else
//make the name a link
//otherwise
//USING REL="NOFOLLOW"
//TO PROTECT SPAMMING
echo "<p><b><a href=\"$url\"
rel=\"nofollow\">$name:</a> </b>$msg</p>";
}
}
fclose($fp);
?>
Note: Put an empty file named "shout_box.txt" in the same directory this script is in.
You may integrate this script into web pages by using the following code:
<iframe src="shout_box.php" width="200" height="500"
scrolling="auto" frameborder="0" align="right"></iframe>
Below I’ve integrated our own shout box using the above code. See how it works! Post any message you want.
Previous Articles: