Sponsored by NuSphere - PHP Software for PHP Application Developers - On Sale This Week for $100 off


PHP Tutorials and Scripts   




Title: Creating a shoutbox    Marked Cool    (Review this resource)
Author: econner
Posted On: 2004-12-07
Category: Home > PHP Tutorials

Popularity: 2 points out of 10    

Description: Creating a shoutbox and securing your database from being filled with spam.

Total Hits: 3325     Total Votes: 0     Total Points: 0 (0 reviews)    

Page Navigation:  [1]  [2]  [3]


Alright now that that's done we need to create some code to display the messages and then a few security functions. To display the messages all we need to do is select the last few from the database (i usually go with 7), but whatever you want and then use some html to display them.

php:
$sql = "SELECT username, message, date FROM shoutbox ORDER BY date DESC LIMIT 0,7";
$result = mysql_fetch_assoc($result) or die(mysql_error());


Ok so what exactly is this select statement doing? It's selecting the user info from the table shoutbox, ordering the select by the column date in descending order. More recent timestamps will have a greater value than older ones. Finally, it is limiting the results of the select to seven. Now to display this info:

php:
<table>
<tr>
<td width="100">Shoutbox</td>
</tr>
<?
while($row = mysql_fetch_assoc($result))
{
    
/*
    * format the date
    */
    
$date = date("m-d-y h:i", $row['date']);
    
    
/*
    * print out the info
    */
    
print("<tr><td>".$row['message']."<br>
            Posted by "
.$row['username']." on $date.</td></tr>");
}
?>
</table>


Alright, now the main parts of the shoutbox are complete. We have code to display the messages and some code to insert a new message into the database. Next all we need to do is add in some security functionality to prevent someone from posting thousands of times in a row. This was the whole reason for adding their ip to the database.

php:
    /*
    * Select the post(s) of the user and check to make sure it has been 15
    * minutes since their last post.
    */
    
$sql = "SELECT user_ip FROM shoutbox WHERE user_ip=".$_SERVER['REMOTE_ADDR']." AND date-$time >= 900";
    
$result = mysql_query($sql) or die(mysql_error());
    
$num = mysql_num_rows($result);
    
    
/*
    * insert their info into database using
    * $_SERVER['REMOTE_ADDR'] for their ip
    */
    
if(!$num)
    {
        
$sql = "INSERT INTO shoutbox(username, message, user_ip, date) VALUES(
            '"
.addslashes($username)."', '".addslashes($message)."',
            '"
.$_SERVER['REMOTE_ADDR']."', '$time'";
        
mysql_query($sql) or die(mysql_error());
    }
?>




Page Navigation:  [1]  [2]  [3]



© Copyright 2003-2008 www.php-editors.com. The ultimate PHP Editor and PHP IDE site.