PHP Shoutbox


Somewhat moderate if you do not know what you're doing ^^;

Okay first off we want to make a page where we will include the shoutbox coding.

well... actually you may want to run this query in phpMyAdmin

PHP Code
  1. CREATE TABLE `shoutbox` (
  2. `id` int(11) NOT NULL auto_increment,
  3. `by` varchar(255) NOT NULL default 'System',
  4. `message` text NOT NULL,
  5. `link` varchar(255) NOT NULL default 'https://google.com',
  6. PRIMARY KEY (`id`)
  7. ) ENGINE=MyISAM;


Whooo thats done now we can really begin....

For the main coding i made 2 files to hold the code. The file i wanted it in then a box.php located in my includes folder.

First, make something a bit like this to show the shoutbox form.
PHP Code
  1. <center><iframe src="includes/box.php" height="200" width="150" name="tagit">Browser Does Not Support I-Frames</iframe>
  2. <form method="POST" target="tagit" action="includes/box.php?x=post">
  3. <b>Link / Email</b>:
  4. <input type="text" name="link" value="https://" size="15"><BR />
  5. <textarea name="message" cols="5" rows="35"></textarea><br>
  6. <input type="submit" name="tag" value="Post">&nbsp;<input type="reset" value="Clear" name="clear">
  7. </center>


That will have an Iframe for the shoutbox then the form underneath it.

Next up, you will want to make a file named box.php or just make one and edit the iframe src="" part.

In it type up something similar to:

PHP Code
  1. <?php
  2. session_start(); //Allow Sessions
  3. include("config.php"); //get config
  4. include("bbcode.php"); //get bbcodes or other functions
  5. switch($_GET[x]){ //change links to ?x=*case*
  6. default: //default page
  7. echo "
  8. <SCRIPT LANGUAGE=\"JavaScript\">
  9.  
  10. function refresh() {
  11. setTimeout('dorefresh()', 30000);
  12. }
  13. function dorefresh() {
  14. window.location.reload();
  15. }
  16. </script><body onLoad=\"refresh()\">
  17. "; //this is the javascript and body for the shoutbox...basically for auto refresh...
  18. $color1 = ""; //Alternating color 1
  19. $color2 = ""; //alternating color 2
  20. $row_count = 0; //how many rows do you start with
  21. $gettags = mysql_query("select * from shoutbox order by id desc limit 15"); //this is for the main frame where shouts are viewed. only 15 but you can change
  22. $nutags = mysql_num_rows($gettags); //count them all up
  23. if($nutags == 0) { //if there are none
  24. echo "<table width='100%'>
  25. <tr><td bgcolor='$row_color' width='100%'>
  26. <center>There are currenctly no tags!</center>
  27. </td></tr>
  28. </table>";//tell them there are none
  29. }else{ //or do something else....
  30. while($t=mysql_fetch_array($gettags)){ //repeat the tables
  31. $row_color = ($row_count % 2) ? $color1 : $color2;
  32. echo "<table width='100%'>
  33. <tr><td bgcolor='$row_color' width='100%'>
  34. <b> <a href=\"$t[link]\">@</a>$t[by]</b>
  35. ";//theres the header information for the shout
  36. if($t[by] == $logged[username]) { //if name is logged in
  37. echo "<a href='box.php?x=delete&id=$t[id]'>(Delete)</a>"; //allow them to delete
  38. }elseif($logged['username'] && $logged['level'] == 6) { //or if the user is logged in and an admin
  39. echo "<a href='box.php?x=delete&id=$t[id]'>(Delete)</a>"; //allow to delete
  40. } //end that ^^;
  41. $message = bbcode(censor($t[message])); //censor and add bbcode to it
  42. echo "<br>$message</td></tr></table>"; //echo the mesage and end the table
  43. $row_count++; //adds another row to the counter above :)
  44. } //end the while...
  45. echo "<a href=\"box.php?x=archive\">Archive</a>"; //Archive link
  46. }
  47. break; //end page
  48. case 'post': //posting page
  49. if($logged[username] && $_POST[tag]){ //check if user is logged in and the form was submitted
  50. if(empty($_POST[message])){ //if the message is empty
  51. echo "<b>Error</b>: You Must Enter A Message"; //echo this
  52. }else{ //or do something else
  53. $user = $logged[username]; //user variable
  54. $link = strip_tags(stripslashes($_POST[link])); //link
  55. $msg = htmlspecialchars(stripslashes($_POST[message])); //the message variable
  56. mysql_query("INSERT INTO shoutbox (`by`,`message`,`link`) VALUES ('$user','$msg','$link')") or die(mysql_error());//do the query or die
  57. echo "<meta http-equiv=\"refresh\" content=\"1;url=box.php\">Shout Posted"; //redirect to main :)
  58. }//end msg empty check
  59. }elseif($logged[username] && !$_POST[tag]){ //check if they just went there VIA Direct URL
  60. echo "<b>Error</b>: You Must Submit The Form"; //echo this
  61. } //end check
  62. break; //end page
  63. case 'delete': //delete page
  64. $id = stripslashes(htmlspecialchars($_GET[id])); //clean up the ID
  65. $get = mysql_query("SELECT * FROM shoutbox WHERE id = '$id'") or die(mysql_error()); //select it from DB
  66. $t = mysql_fetch_array($get); //allow info from it to be selected
  67. if($t[by] == $logged[username]){ //check if logged user is the same as the user who posted it.
  68. mysql_query("DELETE FROM shoutbox WHERE id = '$id'") or die(mysql_error()); //delete if it is true
  69. echo "<meta http-equiv=\"refresh\" content=\"1;url=box.php\"><center>Shout Deleted</center>"; //delete message
  70. }elseif($logged[username] && $logged[level] == 6){ //or continue
  71. mysql_query("DELETE FROM shoutbox WHERE id = '$id'") or die(mysql_error()); //query or die!
  72. echo "<meta http-equiv=\"refresh\" content=\"1;url=box.php\"><center>Shout Deleted</center>"; //delete message
  73. } //end
  74. break; //end the page
  75. case 'archive': //Post Archive
  76. $color1 = ""; //alt color 1
  77. $color2 = ""; //alt color 2
  78. $row_count = 0; //row count start
  79. $gettags = mysql_query("select * from shoutbox order by id desc limit 200"); //get the last 200 shouts
  80. $nutags = mysql_num_rows($gettags); //get how many there are
  81. if($nutags == 0) { //if none
  82. echo "<table width='100%'>
  83. <tr><td bgcolor='$row_color' width='100%'>
  84. <center>There are currenctly no tags!</center>
  85. </td></tr>
  86. </table>"; //make it say so
  87. }else{ //or continue
  88. while($t=mysql_fetch_array($gettags)){ //continue
  89. $row_color = ($row_count % 2) ? $color1 : $color2; //do sopme math to check if there is 1 or more shouts
  90. echo "<table width='100%'>
  91. <tr><td bgcolor='$row_color' width='100%'>
  92. <b>
  93. $t[by]</b> "; //shout headers, simpler then they really are above in the real thing ;P
  94. if($t[by] == $logged[username]) { //if logged name is this users post name
  95. echo "<a href='box.php?x=delete&id=$t[id]'>(Delete)</a>"; //allow to delete
  96. }elseif($logged[username] && $logged[level] == 6) { //or if an admin
  97. echo "<a href='box.php?x=delete&id=$t[id]'>(Delete)</a>"; //allow to delete
  98. }//end check
  99. $message = bbcode(censor($t[message])); //clean up the post :)
  100. echo "
  101. <br>
  102. $message
  103. </td></tr>
  104. </table>"; //echo the rest of the shout
  105. $row_count++; //row move up a number
  106. } //end while
  107. echo "<a href=\"box.php\">Return</a>"; //echo return link
  108. } //end..shout check
  109. break; //end page
  110. } //end if/else
  111. ?>


I know its a bit sloppy but hey :P at least it'll work...

Now, one last file...functions.php

In it create something like the following:

PHP Code
  1. <?
  2. function bbcode($content){// Our bbcode function
  3. $content = nl2br($content); // Add a everytime you hit 'return' or 'enter'
  4.  
  5. $match = array(
  6. '#\[b\](.*?)\[\/b\]#se', // Bold Tag
  7. '#\[i\](.*?)\[\/i\]#se', // Italic Tag
  8. '#\[u\](.*?)\[\/u\]#se', // Underlined Tag
  9. '#\[url=(.*?)\](.*?)\[\/url\]#se', // Url Tag
  10. '#\[url\](.*?)\[\/url\]#se', // Url Tag
  11. '#\[img\](.*?)\[\/img\]#se', // Image Tag
  12. );
  13. $replace = array(
  14. "'<b>\\1</b>'", // Bold
  15. "'<i>\\1</i>'", // Itlaic
  16. "'<u>\\1</u>'", //Underlined
  17. "'<a href=\"\\1\" target=\"_BLANK\">\\2</a>'", // Url
  18. "'<a href=\"\\1\" target=\"_BLANK\">\\1</a>'", // Url
  19. "'<img border=\"0\" src=\"\\1\">'", // Image
  20. );
  21.  
  22. return preg_replace($match, $replace, $content); //return parsed text ass BBCode
  23.  
  24. } //end the function
  25. function censor($text, $replacement = "<b>Censored</b>"){ // they said bad thing lets import it...
  26.  
  27. if( strlen($text) > 0 ){ //if there was actually text..
  28. $name = 'badwords.txt'; //get our bad words
  29. $words = file($name); //make sure its a file
  30.  
  31. $text = str_replace($words, $replacement, $text); //return censored text.
  32. } //end function
  33. ?>


This code was created by me so i could allow my users to chat among each other and such things though i never really used the alternating colors O.o.

This does work and i can get proof once i fix the mySQL DB.

Note: All these were bug checked twice before commenting and after commenting and they passed so if you find errors(other than spelling and punctuation and all that) post them here i shall help to the best of my ability.
ShadowMage's Avatar
Author:
Views:
4,848
Rating:
Posted on Monday 24th March 2008 at 03:03 PM
DanielXP
DanielXP's Avatar
Have you made all the changes what are in the comments of this tutorial?
Posted on Monday 24th March 2008 at 02:50 PM
Dalez
Dalez's Avatar
Don't work :(
Shows my name, and says delete underneath it, but not my msg.
Posted on Thursday 18th October 2007 at 12:05 AM
Arudis
Arudis's Avatar
doesnt work it doesnt post
Posted on Tuesday 22nd May 2007 at 07:54 PM
Diablosblizz
Diablosblizz's Avatar
There its works - but theres a problem. If you delete one post, and there are two posts by you then the other delete message disappears.
Posted on Tuesday 22nd May 2007 at 12:30 AM
ShadowMage
ShadowMage's Avatar
in where your functions are get rid of censor and replace with:
Code

function censor($text, $replacement = "<b>Censored</b>"){ // they said bad thing lets import it...

if( strlen($text) > 0 ){ //if there was actually text..
$name = 'badwords.txt'; //get our bad words
$words = file($name); //make sure its a file
} //end if
$text = str_replace($words, $replacement, $text); //return censored text.
return $text;
}
Posted on Monday 21st May 2007 at 11:44 PM
Diablosblizz
Diablosblizz's Avatar
It still does not work. It doesn't show the post.

This is what I see:

[code] Diablosblizz (Delete)
Archive[code]
Posted on Saturday 19th May 2007 at 01:23 PM
ShadowMage
ShadowMage's Avatar
this is for the RMB user system so you would use that config.php
Posted on Saturday 19th May 2007 at 04:59 AM
Josh
Josh's Avatar
Nice tutorial, but what about the config.php?
Posted on Tuesday 15th May 2007 at 11:48 PM
ShadowMage
ShadowMage's Avatar
One More Thing,

Change:
Code
<textarea name="message" cols="5" rows="35"></textarea><br>


To:
Code
<textarea name="message" cols="35" rows="5"></textarea><br>
Posted on Tuesday 15th May 2007 at 11:46 PM
ShadowMage
ShadowMage's Avatar
Where i have:
Code
<input type="submit" name="tag" value="Post">&nbsp;<input type="reset" value="Clear" name="clear">
</center>


Please change to:
Code
<input type="submit" name="tag" value="Post">&nbsp;<input type="reset" value="Clear" name="clear">
</form>
</center>