Chatter

This tutorial shows you how to make a ChatterBlock. ChatterBlocks are small windows where users can type in messages. They’re also called Shout Boxes or TagBoards and are kind of like miniature chat rooms.

Chatter Form

Let’s make the chatter form first. Open Notepad or your editor, cut and paste the following HTML code and save this file as chatter.html .

<iframe src=’view.php’ border=’0′ name=’chatterblock’ width=”120″ height=”180″ frameborder=”0″ framespacing=”0″></iframe><br>
<form action=’post.php’ method=’post’ target=’chatterblock’>
<input type=’text’ name=’name’ value=’name’><br>
<input type=’text’ name=’url’ value=’http://’><br>
<input type=’text’ name=’msg’ value=’message’ maxlength=”100″><br>
<input type=’submit’ value=’Talk’>
</form>

The code above creates an iframe code, and a small form for users to enter their name, url and messages. This will also be the code that you can cut and paste into part of your navigation.

This code can be inserted into your own Web pages as well. We use the same code here at QuickPipe for our ChatterBlock.

The Database

Before we go any further, we must create the database schema for MySQL for the ChatterBlock entries. Cut and paste the following code into MySQL either via Telnet, PhpmyAdmin or any other means of communicating with MySQL.

CREATE TABLE chatterblock(
id INT (4) not null AUTO_INCREMENT,
name VARCHAR (32),
msg VARCHAR (100),
url VARCHAR (32) ,
entered DATETIME,
ip varchar(16),
PRIMARY KEY (id));

CREATE TABLE blocked_ips(
id INT (4) not null AUTO_INCREMENT,
ip varchar(16),
PRIMARY KEY (id));

CREATE TABLE blocked_nicks(
id INT (4) not null AUTO_INCREMENT,
name varchar(32),
PRIMARY KEY (id));

As we start off, the msg variable in the ‘chatterblock’ table only allows 100 characters. If you find your chatterblock too limiting, you can increase the number of characters in the schema. Don’t forget to update the HTML code and set maxlength to your new limit.

{mospagebreak title=Configuration}

We’ll create a file called ‘chatter.php’, this file is going to be used to store the database connection info.

<?php
$con = mysql_connect(“localhost”,”user”,”pass”);
$db = mysql_select_db(“database”);
?>

Post Chatter

Cut and paste the following code into a blank file and named it ‘post.php’ (without quotes).

<?php
include(‘chatter.php’);
if ($name == ” || $msg == ” || $name == ‘name’ || $msg == ‘message’) {
die (“Error! You cannot fill in an empty shout. Please try again.”);
}
$blockedip = “select * from blocked_ips WHERE ip='”.$REMOTE_ADDR.”‘”;
$ipcheck = mysql_query($blockedip);
while ($row = mysql_fetch_array($ipcheck)){
if( $row["ip"] == $REMOTE_ADDR){
die (“IP banned, you cannot post.”);
}
}
$blockedname = “select * from blocked_nicks WHERE name='”.$name.”‘”;
$namecheck = mysql_query($blockedname);
while ($row = mysql_fetch_array($namecheck)){
if( $row["name"] == $name){
die (“Username has been banned. You may not post.”);
}
}
$last_entry = “select * from chatterblock order by id desc limit 1″;
$check = mysql_query($last_entry);
while ($row = mysql_fetch_array($check)){
$lastname = $row["name"];
$lastmsg = $row["msg"];
}
if ($lastname == $name && $lastmsg == $msg){
die (“Error! Duplicate entry detected, please submit only once.”);
}
$name = htmlspecialchars($name);
$msg = htmlspecialchars($msg);
$q = “insert into chatterblock (id,name,msg,url,entered,ip) VALUES (”,’$name’,’$msg’,’$url’,now(),’$REMOTE_ADDR’) “;
$result = mysql_query($q);
if ($result) {
header(‘Location: view.php’);
}
?>

Basically, the above code runs a check to make sure the user doesn’t submit a blank entry. Notice that in our chatter.html, we stated the default value for name to be ‘name’ and msg to be ‘message’, therefore the basic check should also include the default values. Otherwise, the user can just click ‘submit’ endless times, and you get the same meaningless entry over again.

Next, the script will strip off all the HTML tags that may be inserted into the chatterblock. This ensures that no one enters codes that may cause the chatterblock to look weird, among other things. Then it’ll insert the entry into the database to be stored and, if successful, redirect the user to view the chatterblock.

{mospagebreak title=View Chatter}

The iframe in chatter.html will load this ‘View Chatters’ page. You can format this page to look how you want the chatterblock to be displayed. Let’s take a look at the code.

<body onload=window.setTimeout(“location.href=’view.php'”,120000)>
<?php
include(‘chatter.php’);
$q = “select * from chatterblock order by id desc “;
$result = mysql_query($q);
while ($row = mysql_fetch_array($result)) {
$id = $row["id"];
$name=$row["name"];
$url = $row["url"];
$msg = $row["msg"];
echo “<b><a href=”$url” target=”new”>$name</a></b>: $msg<hr>”;
}
?>

Basically code selects all the entries in the chatterblock to be listed, in the order of oldest last, newest first (chronological order). I only put in some basic format to display the entries; you can insert CSS, tables or whatever you like inside this part. In the above code, a user’s name is displayed in bold with a hyperlink to the url entered.

Note: It’ll display the hyperlink regardless of whether a url is entered or not. if you want it to be more intelligent, run a simple if check to see if $url has a value. If $url is empty, then don’t display the hyperlink.

The message is displayed at the side of the name. Each entry is separated by a horizontal rule.

Save this file as view.php

{mospagebreak title=Manage the Chatter}

Now for a simple script to manage your chatterblock entries. This script will display all entries and give you the option of deleting any that you don’t think are appropriate.

<? include(“accesscontrol.php”); ?>
<table width=100% border=1>
<tr>
<td><b>Name</b></td>
<td><b>Message</b></td>
<td><B>Date Entered</b></td>
<td><b>IP Address</b></td>
<td>&nbsp;</td>
</tr>
<?php
include(‘chatter.php’);
if($del){
mysql_query(“DELETE FROM chatterblock WHERE id=’$del'”);
}
if($banip){
mysql_query(“INSERT INTO blocked_ips SET ip=’$banip'”);
}
if($abanip){
mysql_query(“DELETE FROM blocked_ips WHERE ip=’$abanip'”);
}
if($bann){
mysql_query(“INSERT INTO blocked_nicks SET name=’$bann'”);
}
if($abann){
mysql_query(“DELETE FROM blocked_nicks WHERE name=’$abann'”);
}
$q = “select * from chatterblock order by id desc “;
$result = mysql_query($q);
while ($row = mysql_fetch_array($result)){
$id = $row["id"];
$name=$row["name"];
$url = $row["url"];
$msg = $row["msg"];
$entered = $row["entered"];
$ip = $row["ip"];
echo “<tr>”;
echo “<td><b><a href=”$url” target=”new”>$name</a></b></td>”;
echo “<td> $msg</td>”;
echo “<td>$entered</td>”;
echo “<td>$ip</td>”;
echo “<td><a href=’cbadmin.php?del=$id’>delete</a> |”;
$blockedip = “select * from blocked_ips WHERE ip='”.$ip.”‘”;
$ipcheck = mysql_query($blockedip);
if( mysql_num_rows($ipcheck) > 0){
echo “<a href=’cbadmin.php?abanip=$ip’>unban ip</a> |”;
}else{
echo “<a href=’cbadmin.php?banip=$ip’>ban ip</a> |”;
}
$bname = “select * from blocked_nicks WHERE name='”.$name.”‘”;
$ncheck = mysql_query($bname);
if( mysql_num_rows($ncheck) > 0){
echo “<a href=’cbadmin.php?abann=$name’>unban name</a></td>”;
}else{
echo “<a href=’cbadmin.php?bann=$name’>ban name</a></td>”;
}
echo “</tr>”;
}
?>

Save this file as cbadmin.php

{mospagebreak title=Secure it}

You may have noticed that cbadmin.php calls a file called accesscontrol.php on the first line. This file is used to place a password on your chatterblock admin area.

<?php
$adlogin = “test”;
$adpassword = “test”;
if (!isset($PHP_AUTH_USER)) {
header(‘WWW-Authenticate: Basic realm=”ChatterBlock Admin”‘);
header(‘HTTP/1.0 401 Unauthorized’);
echo ‘Authorization Required.';
exit;
} else if (isset($PHP_AUTH_USER)) {
if (($PHP_AUTH_USER != $adlogin) || ($PHP_AUTH_PW != $adpassword)) {
header(‘WWW-Authenticate: Basic realm=”ChatterBlock Admin”‘);
header(‘HTTP/1.0 401 Unauthorized’);
echo ‘Authorization Required.';
exit;
}else{
session_register(“aid”);
session_register(“apass”);
}
}
?>

Save this file as accesscontrol.php, and you will now have password protection on your ChatterBlock Admin script.

Conclusion

That’s it. A quick and fast tutorial. This chatterblock is very basic, can post and can view. It doesn’t really have other nifty functions such as smilies or profanities checking. Once you get the basic chatterblock, you can always add features and functions later on.

[gp-comments width="770" linklove="off" ]
antalya escort bayan antalya escort bayan