Forgot Password / Register
Site Statistics
Total Members: 520
Total Tutorials: 242
Newsest User: 8884244477
Todays Unique Hits: 341
0 Users 16 Guests Online

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

Code

CREATE TABLE `shoutbox` (
`id` int(11) NOT NULL auto_increment,
`by` varchar(255) NOT NULL default 'System',
`message` text NOT NULL,
`link` varchar(255) NOT NULL default 'http://google.com',
PRIMARY KEY (`id`)
) 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.
Code

<center><iframe src="includes/box.php" height="200" width="150" name="tagit">Browser Does Not Support I-Frames</iframe><br />
<form method="POST" target="tagit" action="includes/box.php?x=post">
<b>Link / Email</b>:<br />
<input type="text" name="link" value="http://" size="15"><BR />
<textarea name="message" cols="5" rows="35"></textarea><br>
<input type="submit" name="tag" value="Post">&nbsp;<input type="reset" value="Clear" name="clear">
</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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
session_start
(); //Allow Sessions
include("config.php"); //get config
include("bbcode.php"); //get bbcodes or other functions
switch($_GET[x]){ //change links to ?x=*case*
default: //default page
echo "
<SCRIPT LANGUAGE=\"JavaScript\">

function refresh() {
setTimeout('dorefresh()', 30000);
}
function dorefresh() {
window.location.reload();
}
</script><body onLoad=\"refresh()\">
"
//this is the javascript and body for the shoutbox...basically for auto refresh...
$color1 "";  //Alternating color 1
$color2 "";  //alternating color 2
$row_count 0//how many rows do you start with
$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
$nutags mysql_num_rows($gettags); //count them all up
if($nutags == 0) { //if there are none
echo "<table width='100%'>
<tr><td bgcolor='
$row_color' width='100%'>
<center>There are currenctly no tags!</center>
</td></tr>
</table>"
;//tell them there are none
}else{ //or do something else....
while($t=mysql_fetch_array($gettags)){ //repeat the tables
$row_color = ($row_count 2) ? $color1 $color2;        
echo 
"<table width='100%'>
<tr><td bgcolor='
$row_color' width='100%'>
<b> <a href=\"
$t[link]\">@</a>$t[by]</b>
"
;//theres the header information for the shout
if($t[by] == $logged[username]) { //if name is logged in
echo "<a href='box.php?x=delete&id=$t[id]'>(Delete)</a>"//allow them to delete
}elseif($logged['username'] && $logged['level'] == 6) { //or if the user is logged in and an admin
echo "<a href='box.php?x=delete&id=$t[id]'>(Delete)</a>"//allow to delete
//end that ^^;
$message bbcode(censor($t[message]));  //censor and add bbcode to it
echo "<br>$message</td></tr></table>"//echo the mesage and end the table
$row_count++; //adds another row to the counter above :)
//end the while...
echo "<a href=\"box.php?x=archive\">Archive</a>"//Archive link
}
break; 
//end page
case 'post'//posting page
if($logged[username] && $_POST[tag]){ //check if user is logged in and the form was submitted
    
if(empty($_POST[message])){ //if the message is empty
    
echo "<b>Error</b>: You Must Enter A Message"//echo this
    
}else{ //or do something else
    
$user $logged[username]; //user variable
    
$link strip_tags(stripslashes($_POST[link])); //link
    
$msg htmlspecialchars(stripslashes($_POST[message])); //the message variable
    
mysql_query("INSERT INTO shoutbox (`by`,`message`,`link`) VALUES ('$user','$msg','$link')") or die(mysql_error());//do the query or die 
    
echo "<meta http-equiv=\"refresh\" content=\"1;url=box.php\">Shout Posted"//redirect to main :)
    
}//end msg empty check
}elseif($logged[username] && !$_POST[tag]){ //check if they just went there VIA Direct URL
echo "<b>Error</b>: You Must Submit The Form"//echo this
//end check
break; //end page
case 'delete'//delete page
$id stripslashes(htmlspecialchars($_GET[id])); //clean up the ID
$get mysql_query("SELECT * FROM shoutbox WHERE id = '$id'") or die(mysql_error()); //select it from DB
$t mysql_fetch_array($get); //allow info from it to be selected
if($t[by] == $logged[username]){ //check if logged user is the same as the user who posted it.
mysql_query("DELETE FROM shoutbox WHERE id = '$id'") or die(mysql_error()); //delete if it is true
echo "<meta http-equiv=\"refresh\" content=\"1;url=box.php\"><center>Shout Deleted</center>"//delete message
}elseif($logged[username] && $logged[level] == 6){ //or continue
mysql_query("DELETE FROM shoutbox WHERE id = '$id'") or die(mysql_error()); //query or die!
echo "<meta http-equiv=\"refresh\" content=\"1;url=box.php\"><center>Shout Deleted</center>"//delete message
//end 
break; //end the page
case 'archive'//Post Archive
$color1 "";   //alt color 1
$color2 "";   //alt color 2
$row_count 0//row count start
$gettags mysql_query("select * from shoutbox order by id desc limit 200"); //get the last 200 shouts
$nutags mysql_num_rows($gettags); //get how many there are
if($nutags == 0) { //if none
echo "<table width='100%'>
<tr><td bgcolor='
$row_color' width='100%'>
<center>There are currenctly no tags!</center>
</td></tr>
</table>"
//make it say so
}else{ //or continue
while($t=mysql_fetch_array($gettags)){ //continue
$row_color = ($row_count 2) ? $color1 $color2//do sopme math to check if there is 1 or more shouts        
echo "<table width='100%'>
<tr><td bgcolor='
$row_color' width='100%'>
<b> 
$t[by]</b> "//shout headers, simpler then they really are above in the real thing ;P
if($t[by] == $logged[username]) { //if logged name is this users post name
echo "<a href='box.php?x=delete&id=$t[id]'>(Delete)</a>"//allow to delete
}elseif($logged[username] && $logged[level] == 6) { //or if an admin
echo "<a href='box.php?x=delete&id=$t[id]'>(Delete)</a>"//allow to delete
}//end check
$message bbcode(censor($t[message]));  //clean up the post :)
echo "
<br>
$message
</td></tr>
</table>"
//echo the rest of the shout
$row_count++; //row move up a number
//end while
echo "<a href=\"box.php\">Return</a>"//echo return link
//end..shout check
break; //end page
//end if/else
?>


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
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?
function bbcode($content){// Our bbcode function
$content nl2br($content); // Add a  everytime you hit 'return' or 'enter'

        
$match = array(
            
'#\[b\](.*?)\[\/b\]#se'// Bold Tag
            
'#\[i\](.*?)\[\/i\]#se'// Italic Tag
            
'#\[u\](.*?)\[\/u\]#se'// Underlined Tag
            
'#\[url=(.*?)\](.*?)\[\/url\]#se'// Url Tag
            
'#\[url\](.*?)\[\/url\]#se'// Url Tag
            
'#\[img\](.*?)\[\/img\]#se'// Image Tag
               
);
        
$replace = array(
            
"'<b>\\1</b>'"// Bold
            
"'<i>\\1</i>'"// Itlaic
            
"'<u>\\1</u>'"//Underlined
            
"'<a href=\"\\1\" target=\"_BLANK\">\\2</a>'"// Url
            
"'<a href=\"\\1\" target=\"_BLANK\">\\1</a>'"// Url
            
"'<img border=\"0\" src=\"\\1\">'"// Image
                   
);
                
        return 
preg_replace($match$replace$content); //return parsed text ass BBCode

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

    
if( strlen($text) > ){ //if there was actually text..
        
$name 'badwords.txt'//get our bad words
        
$words file($name); //make sure its a file

        
$text str_replace($words$replacement$text); //return censored text.
    
//end function
?>


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
Author:
Views:
4607
Rating:
Posted on Monday 24th March 2008 at 03:03 PM
DanielXP
DanielXP
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
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
doesnt work it doesnt post
Posted on Tuesday 22nd May 2007 at 07:54 PM
Diablosblizz
Diablosblizz
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
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
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
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
Nice tutorial, but what about the config.php?
Posted on Tuesday 15th May 2007 at 11:48 PM
ShadowMage
ShadowMage
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
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>