Skip to main content

Designing a Simple ‘Shout Box’ Script in PHP

Designing a Simple ‘Shout Box’ Script in PHP

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:

Popular posts from this blog

Fix For Toshiba Satellite "RTC Battery is Low" Error (with Pictures)

RTC Battery is Low Error on a Toshiba Satellite laptop "RTC Battery is Low..." An error message flashing while you try to boot your laptop is enough to panic many people. But worry not! "RTC Battery" stands for Real-Time Clock battery which almost all laptops and PCs have on their motherboard to power the clock and sometimes to also keep the CMOS settings from getting erased while the system is switched off.  It is not uncommon for these batteries to last for years before requiring a replacement as the clock consumes very less power. And contrary to what some people tell you - they are not rechargeable or getting charged while your computer or laptop is running. In this article, we'll learn everything about RTC batteries and how to fix the error on your Toshiba Satellite laptop. What is an RTC Battery? RTC or CMOS batteries are small coin-shaped lithium batteries with a 3-volts output. Most laptops use

The Best Way(s) to Comment out PHP/HTML Code

PHP supports various styles of comments. Please check the following example: <?php // Single line comment code (); # Single line Comment code2 (); /* Multi Line comment code(); The code inside doesn't run */ // /* This doesn NOT start a multi-line comment block /* Multi line comment block The following line still ends the multi-line comment block //*/ The " # " comment style, though, is rarely used. Do note, in the example, that anything (even a multi-block comment /* ) after a " // " or " # " is a comment, and /* */ around any single-line comment overrides it. This information will come in handy when we learn about some neat tricks next. Comment out PHP Code Blocks Check the following code <?php //* Toggle line if ( 1 ) {      // } else {      // } //*/ //* Toggle line if ( 2 ) {      // } else {      // } //*/ Now see how easy it is to toggle a part of PHP code by just removing or adding a single " / " from th

Designing a Simple Order Form Application in PHP

Ok guys, for this post we’re going to create a simple yet complete order form page. Order forms are used on many sites to take customers order online. Order forms should have the capability to take orders from visitors regarding what items they want to purchase and store the information for further processing. For this post’s example, we are going to create an order form for a Book Seller. The form will be designed to take order of five different items (books). Our order form application should be able to take order of five different items in any separate quantities tht user wants, it should also ask for shipping address and name of the customer. It should then store the information provided in a file along with the date and time order was placed. The application should also be able to take any number of orders and store them all linearly for further human processing. For this, we need a front end of a HTML form to which the user would interact a