ShoutBox Development process
Index
- In the Beginning - An explanation of why I started this project, and how to accredit me if you use my code.
- The original ShoutBox code v1.0 - After a fair amount of messing around, this is the 1st proper version of the code, together with explanations.
- Improved Requirements - Some of which I have implemented, some I have not. Its a kind of a to-do list, of what I want from the code. If you implement any improvements to the code, I would love to hear about them, I can then accredit you for your contribution.
- Bug Fixing - Various improvements I made between versions 1.0 and 2.0 to make the code better. Some alterations are based on the requirements, some based on bugs found by me or others.
- Current Code v2.0 - The complete code, as is [last updated 17th march 2003].
- Sites using the code, or were inspired by this version - A list of sites using my code, a variant of my code, or inspired by this ShoutBox have created their own version.
In the Beginning
When deciding to learn PHP I needed a target, a program to aim for; though its true that a ShoutBox is a relatively simple program it seemed a good place to start.
After I developed my first ShoutBox, I noticed from my site statistics that I was getting a sizable number of hits through various search engines, Google for one, from people looking for a PHP implementation of a ShoutBox. This inspired me to improve the existing code and to release it under the open source GPL licence rather than the Creative Commons (which it was originally under)
Currently this is still a work in progress, but you are quite welcome to have the code for your own purposes. If you do; it would be really nice if you could link to me (or this page) from somewhere near your ShoutBox, and e-mail me telling me you have used it, that way I can know if the code is useful or not, and send you any fixes I may come up with.
If you do make any changes to my code I would be really interested to see what you have done, so drop me a line. Thanks :)
The original ShoutBox code version 1.0
The ShoutBox code uses the following html form to take the name, address and message, then to post them as strings called $name, $address, and $shout respectively, to a PHP script 'send.PHP'
The send script then takes this string variables $shout and $address then for security, strips all the html tags from it. The beginning and end spaces are then striped from each variable. After this, there is logic to determine whether the length of the string $shout is greater than 100 characters. If $shout is less than 100 characters, the string is posted as it is. If however the string contains more than 100 characters only the first 100 are taken and "..." is appended to the end of the string.
The second 'if' statement is logic top determine what should happen if one or more of the form boxes are left blank. This ensures that the string $shout is only posted if it is not null. The code within the second 'if' statement opens the text file 'shouts.txt' and sets the pointer to the end of the file. Where, using the function fwrite(), writes today's date, (defined in the function above) and the Day and the time in 24hr clock; then displays the name as a link to their url (if not null) and then the given message with a line break at the end. The file is then closed.
= 200) { $shout = substr($shout, 0, 200); $shout = $shout."..."; } // If the user has entered a shout, open the 'shouts.txt' file, // and execute the following conditionals. if ($shout != "") { $fp = fopen("shouts.txt", "a"); $today = date("D, H:i"); // If the user has not entered a name or a address, // print today's date and the shout. if ($name == "" && $address == "") { fwrite($fp, "$today: $shout\n"); } // If the user has entered a name but not an address, // print today's date, the name, a hyphen and then the shout. else if ($name != "" && $address == "") { fwrite($fp, "$today: $name - $shout\n"); } // If the user has entered an address but not a name, // print today's date, then 'Anon' as a link to the address, a hyphen and then the shout. else if ($name == "" && $address != "") { fwrite($fp, ''.$today.': Anon - '.$shout."\n"); } // If the user has entered both name and address, // Print their name as a link to their address (with the appropriate title), a hyphen and then their shout. else if ($name != "" && $address != "") { fwrite($fp, ''.$today.': '.$name.' - '.$shout."\n"); } // All situations are covered by the above code, // so the following code should never be executed. else { die("you should never see this ... if you are, something has gone horribly horribly wrong!"); } // Close the 'shouts.txt' file fclose($fp); } ?>
Finally, the display of the messages is done on the same page as the form, the array of strings $shouts is taken from the file 'shouts.txt', reversed - so the most recent is at the top, and then sliced so that the array only contains the 10 most recent messages. For every item in the remaining array, the code makes it into an unordered list item with a line break at the end.
-
".$item."\n";
}
?>
The option to view all the shouts is done in much the same way, except the list is not trimmed down to any length of shouts:
-
".$item."\n";
}
?>
The display of the shouts was done using the following CSS code:
ul#boxList { border: 1px dashed #4A7BA6; padding: 5px; color: #4A7BA6; background-color: white; }
Improved Requirements - (A sort of to-do list)
So I have code that works, but it's not perfect. I have decided to rethink the concept of the ShoutBox from the level of requirements:
- It should at the most basic level be able to take a name and a shout from a user and display it
- The full date and time should also be recorded
- All fields are optional and are not displayed if null (*)
- HTML should be striped from entry
- Trailing spaces should be trimmed from the entry
- The shout should not be allowed to be more than 300 characters, the rest of the shout is trimmed and "..." appended to the end of the shout
- The url should be in the form 'http://'.$address (if not when submitted)
- If the name field is empty, and the address field is not null, then name = "Anon"
- If all fields except the shout are empty then just the date and the shout should be displayed
- The full date and time should also be recorded
- On the main page there should be the last 10 recorded shouts
- The day and the time should be displayed on the main 10
- There should be the option to view all the other previous shouts (as well as the most recent 10)
- The previous shouts should be displayed with the full date and time
- The previous shouts and separated into months
- The IP address of the user should be recorded for the use of the administrator only
Possible customisable features:
- Length of shout?
- HTML to be striped?
- How the date and time are shown
- How many entries are shown on the main page
- The amount of entries shown on each page of the archive
Version 2.0 Bug Fixing
This section contains a list of the bug fixes and alterations between versions 1.0 and 2.0.
Someone recently [12th Mar 2003] tried (and unfortunately succeeded!) in breaking the design of my site by entering in one very long word, this did not word-wrap and looked awful. This inspired me to fix the bug using PHP's 'word-wrap' function (http://www.php.net/manual/en/function.wordwrap.php) to enter the following line of code: $item = wordwrap($item, 50, ' ', 1);
This takes the string $item, and when displaying it, inserts a space ' ' every 50 characters, making sure not to insert one in the middle of the word unless that word is more than 50 characters long. In displaying the string, it doesn't matter if there are extra spaces (i.e. between words) because HTML renders only the one. The argument '1' at the end of the line of code ensures that if the word is more than 50 characters long it will force a break.
So the code for displaying the shouts now looks like:
-
".$item."\n";
}
?>
this is on my index page, so the code for displaying all the shouts won't include the array_slice
line
The restriction on entry for the length of the 'shout' was extended from 200 to 300 characters, the 'name' field has been trimmed to 30 characters, and the 'url' to 255 characters.
The code now also adds an 'http://' to the address if it is not there already, and the 'url' field is not null. This is done using the following code:
if (substr($address, 0, 4) != 'http' && $address != "") { $address = 'http://'.$address; }
This checks if the 1st four characters contain 'http', if it is not; then it concatenates 'http://' on the front of existing 'url'.
Andy suggested I should perform some additional client side check to the above field trimming... so here it is, (the code for the form):
The current code! 'ShoutBox 2.0'
The code in the send.php
file looks as follows:
= 300) { $shout = substr($shout, 0, 300); $shout = $shout."..."; } // name also needs trimming if (strlen($name) >= 30) { $name = substr($name, 0, 30); $name = $name."..."; } // url's should not be more than 255 characters long anyway if (strlen($address) >= 255) { $address = substr($address, 0, 255); $address = $address."..."; } // Adds an 'http://' to the address if it is not there already and is not null. if (substr($address, 0, 4) != 'http' && $address != "") { $address = 'http://'.$address; } // If the user has entered a shout, open the 'shouts.txt' file, // and execute the following conditionals. if ($shout != "") { $fp = fopen("shouts.txt", "a"); $today = date("D, H:i"); // If the user has not entered a name or a address, // print today's date and the shout. if ($name == "" && $address == "") { fwrite($fp, "$today: $shout\n"); } // If the user has entered a name but not an address, // print today's date, the name, a hyphen and then the shout. else if ($name != "" && $address == "") { fwrite($fp, "$today: $name - $shout\n"); } // If the user has entered an address but not a name, // print today's date, then 'Anon' as a link to the address, a hyphen and then the shout. else if ($name == "" && $address != "") { fwrite($fp, ''.$today.': Anon - '.$shout."\n"); } // If the user has entered both name and address, // Print their name as a link to their address (with the appropriate title), a hyphen and then their shout. else if ($name != "" && $address != "") { fwrite($fp, ''.$today.': '.$name.' - '.$shout."\n"); } // All situations are covered by the above code, // so the following code should never be executed. else { die("you should never see this ... if you are, something has gone horribly horribly wrong!"); } // Close the 'shouts.txt' file fclose($fp); } ?>Redirecting... Please wait...
The code to display the shouts on the index page (or wherever you only want a selection of the most recent quotes to appear - I used 10) Looks like:
-
".$item."\n";
}
?>
To display all the quotes on one page, just remove the whole "$shouts = array_slice ($shouts, 0, 10);
" line.
Sites using my code
(Click here to view this section with screenshots)
Below is a list of sites (that I know of) which use my code, or a variant of my code:
- http://www.superdeluxo.com/personal.html - Matthew O'Hara has a nice implementation on his site, which fits in superbly with his current site design.
- http://www.tedddee.com/ - This site was using the ShoutBox as part of his site, it doesn't appear to be there at the moment.
- http://www.nitinparmar.co.uk/ - Nitin has a stylish implementation of the ShoutBox which opens in a new window, allowing you to browse his site at the same time. he has also made it so clicking on a web page from someone's shout also opens in a new window.
- http://www.balomman.tk/ - Stijn Blomme holds a Dutch site, with a very sweet variation on the ShoutBox idea, the website url is now compulsory and appears as a link on the bottom of the page with the name as the link text and the title is the actual 'shout' as it were. (the Dutch for clicking 'add' by the way is: "Toevoegen")
Sites inspired by this ShoutBox:
- http://blog.mooncalf.me.uk/ - Andy's implementation known as the 'chocolate log' is totally different to mine, using different code, by his own admission he 'borrowed only the idea of a ShoutBox'. I thought I'd mention him here though because his implementation is beautiful, the form boxes and post button are very stylish, but what I really liked was the ordered list idea on the most recent shouts. A very nice ShoutBox indeed.
- http://www.chrisbeach.co.uk/ - Chris's ShoutBox also doesn't use my code and is based only on the idea. His version runs on a database and he has plans for future development of the basic idea. Another nice feature of this is the seamless way he has integrated flash with PHP.
I would love to add more sites here, so mail me if you have used a variant of my code, and I'll add you to this list.
And finally ... Happy Developing :)