PHP shoutbox
Creating a shoutbox using PHP
I have seen a shoutbox described as a chat application to a guestbook. I do not consider a shoutbox a chat application, I think it's more of a simple comment system. Visitors to your website can quickly leave feedback without having to register for chat or a forum and they do not have to answer all the questions that would normally be in a guestbook. Creating a shoutbox with PHP and MySQL is not very difficult.
First create a table to hold the shoutbox data:
CREATE TABLE myshout (
shoutid int(4) NOT NULL auto_increment,
shoutname varchar(20) NOT NULL default '',
shoutmsg text NOT NULL,
shoutdate datetime NOT NULL default '0000-00-00 00:00:00',
PRIMARY KEY (shoutid)
) TYPE=MyISAM;
The class below contains the functions for connecting to a database, saving information into your table and also displaying information from your shoutbox table.
PHP code: shoutbox.php
<?php class shoutbox { var $listlimit; var $bgcolor1; var $bgcolor2; function connect() { mysql_connect("server", "user", "password") OR die ("Connection Error to Server"); mysql_select_db("shout Database") OR die("Connection Error to Database"); } function displayform() { //display the shoutbox form echo" <TABLE WIDTH=\"170\" BORDER=\"0\" CELLPADDING=\"4\" CELLSPACING=\"0\"> <FORM NAME=\"shout\" ACTION=\"index.php\" METHOD=\"post\"> <TBODY> <TR> <TD ALIGN=\"center\"> <INPUT TYPE=\"text\" VALUE=\"name\" NAME=\"shoutname\" SIZE=\"20\"><BR> <INPUT TYPE=\"text\" VALUE=\"shout\" NAME=\"shout\" SIZE=\"20\"><BR> <INPUT TYPE=\"submit\" NAME=\"submit\" VALUE=\"Shout!\"> <INPUT TYPE=\"reset\" VALUE=\"Reset\"> </TD> </TR> </TBODY> </FORM> </TABLE>\n"; } function displayshoutbox() { //displays shouts in the table up to the defined limit echo "<p> <TABLE WIDTH=\"170\" BORDER=\"0\" CELLPADDING=\"4\" CELLSPACING=\"0\"> "; $shoutboxResult = mysql_query("SELECT * FROM myshout ORDER BY shoutid DESC LIMIT $this->listlimit;"); if ($shoutboxRow = mysql_fetch_array($shoutboxResult)) { do { $shoutid = $shoutboxRow[shoutid]; $shoutname = $shoutboxRow[shoutname]; $shoutdate = $shoutboxRow[shoutdate]; $shoutmsg = $shoutboxRow[shoutmsg]; $shoutmsg = wordwrap($shoutmsg, 20, "\n", 1); $bgcolor = ($count++ % 2) ? "$this->bgcolor1" : "$this->bgcolor2"; echo "<TR><TD BGCOLOR=\"$bgcolor\">$shoutname: $shoutmsg<DIV ALIGN=\"right\"> $shoutdate</DIV></TD></TR>\n"; } while($shoutboxRow = mysql_fetch_array($shoutboxResult)); } else { echo "No shouts yet"; } echo "</TABLE>"; } function insertshout($shoutname, $shout) { //insert data into table $shoutname = strip_tags($shoutname); $shout = strip_tags($shout, '<a><b><i><u>'); //strip html tags allowing only <a><b><i><u> if (mysql_query ("INSERT INTO myshout (shoutname, shoutmsg, shoutdate) VALUES ('$shoutname', '$shout', NOW())")) { echo ""; } else { echo mysql_error(); } } } ?>
function connect() Connects to the database.
function displayform() Displays the shoutbox form via which shouts are entered into the table.
function displayshoutbox() Displays any shouts in the shoutbox table up to a predefined limit.
If there are no shouts a message stating that there are no shouts is displayed.
function insertshout($shoutname, $shout) Receives the shoutbox user's name and shout message.
Stips any HTML tags in the name or message, except for those allowed and inserts the data into the
table along with the date.
See it in action. ¦ Get complete code.
PHP Code: index.php uses the functions in the shoutbox.php file to operate the shoutbox.
<?php require_once("shoutbox.php"); if($submit){ $shoutbox = new shoutbox; $shoutbox->connect(); $shoutbox->insertshout($shoutname, $shout); header("Location: $PHP_SELF"); } . . . $shoutbox = new shoutbox; $shoutbox->listlimit = "20"; $shoutbox->bgcolor1 = "#ffcc00"; $shoutbox->bgcolor2 = "#c0c0c0"; $shoutbox->connect(); $shoutbox->displayform(); $shoutbox->displayshoutbox(); ?>
See it in action. ¦ Get complete code.