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.


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:

<form name="form1" id="form1" method="post"
  <table width="100%" border="0" cellspacing="0" 
      <td width="22%"><font size="2">Name:</font></td>
      <td width="78%"><input name="name" 
    type="text" id="name"  /></td>
      <td><font size="2">URL: </font></td>
      <td><input name="url" type="text" id="url" 
    value="http://" /></td>
      <td><font size="2">Message</font></td>
      <td><input name="msg" type="text"
     id="msg" /></td>
      <td><input name="say" type="submit" 
    id="say" value="Say" /></td>
      <td><input type="reset" name="Submit2" 
    value="Reset" /></td>
//if 'say' button was pressed
//have the sent data
    //striping out HTML tags
//if name feild was empty
//if no URL was submitted
//if msg field was not blank
//make the 'data' variable
        //triming any excess spaces
//open the file and have data
        //in an array
//overwrite the file
//first put the data that
        //was sent by the form
//if the original file had
        //contents, append it to the
        //end of the file
$file_ar as $line)
//store only 20 
                    //messages MAX
if($loop>=19*3) break;

//just show the messages
//if no URL is there
"<p><b>$name: </b>$msg</p>";
//make the name a link
            //USING REL="NOFOLLOW"
echo "<p><b><a href=\"$url\" 
        rel=\"nofollow\">$name:</a> </b>$msg</p>"

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

Generating XML Feeds (RSS, Atom) Using PHP

RSS/ATOM feeds are very common these days and almost all Content Management Systems (CMS) can generate it. But in the case when you want to generate it yourself or just want to learn how you can, read on! Both RSS and ATOM feeds are written in eXtensible Markup Language (XML) standard markup. Not just standard markups, you also need to be sure of what and how you put data in those markup elements (tags). For all this refer to the feed specifications of RSS and ATOM . XML itself is very strict and the standard specifications makes it even harder to generate valid feeds. And moreover, why re-invent the wheel when we can have it – ready-made. The solution I'm referring to here is, to use a third-party Library – Universal FeedWriter. FeedWriter is a PHP class written by Anis uddin Ahmad that can dramatically  ease-off feeds (both RSS and Atom) generation. You can download this library from  here . Every feed should have at least the following data: Feed title URL(of the webs