Home arrow PHP arrow Page 2 - Chatter

Configuration - PHP

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.

TABLE OF CONTENTS:
  1. Chatter
  2. Configuration
  3. View Chatter
  4. Manage the Chatter
  5. Secure it
By: Roger Stringer
Rating: starstarstarstarstar / 29
November 08, 2004

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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.



 
 
>>> More PHP Articles          >>> More By Roger Stringer
 

blog comments powered by Disqus
   

PHP ARTICLES

- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 10 - Follow our Sitemap

Dev Shed Tutorial Topics: