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: 3321     Total Votes: 0     Total Points: 0 (0 reviews)    

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


Thats pretty much all, you should now have a working shoutbox:

php:
<?
if($_POST) /* check for a form submit */
{
    
$time = time(); /* get the current timestamp */
    
    /*
    * 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());
    }
    
    
/*
    * Pause for three seconds and then redirect
    * them back to the main page, this is helpful
    * so they cannot resubmit info and if they hit
    * refresh they dont get a pesky post resend message
    */
    
print("Redirecting three seconds.");
    
sleep(3);
    
header("Location: ./index.php");
}
else
{
    
$sql = "SELECT username, message, date FROM shoutbox ORDER BY date DESC LIMIT 0,7";
    
$result = mysql_fetch_assoc($result) or die(mysql_error());
?>
<!doctype html public "-//W3C//DTD HTML 4.0 //EN">
<html>
<head>
       <title></title>
</head>
<body>
<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>
<form action="<? echo $_SERVER['PHP_SELF']; ?> method="post">
    <input type="text" name="username"><br>
    <input type="text" mame="message"><br>
    <input type="submit" value="submit">
</form>
</body>
</html>
<? } ?>




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



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