 |
Title: Creating a shoutbox Marked Cool (Review this resource) Author: econner Posted On: 2004-12-07 Category: Home > PHP Tutorials
Popularity:
Description: Creating a shoutbox and securing your database from being filled with spam.
Total Hits: 3121 Total Votes: 0
Total Points: 0 (0 reviews)
Creating a shoutbox
In this tutorial I will show you how to create a shoutbox and put in
some functionality to attempt to keep a user from filling up your database
and overloading your server.
The first thing we'll need of course is a table to store the messages,
it will also store the ip of the user posting so they cannot post more
than once over a specific time period we will also put in a redirect to
secure it a little bit more, this is also nice because by refreshing the
page they won't be able to resend the information. We'll need a column
to store the username they enter and one to store their message also.
CREATE TABLE shoutbox {
id INT NOT
NULL AUTO_INCREMENT,
username
VARCHAR(20),
message TEXT,
user_ip VARCHAR(16),
date TIMESTAMP;
PRIMARY KEY(id)
};
That is all you'll need to start; a column to store their username, one
for the message, one for their ip and one to store the timestamp of the
date posted. Now we'll need to create a form so they can send their messages.
It will only need two text fields and a submit button.
html:
<form action="<?
echo $_SERVER['PHP_SELF'];
?> method="post">
Username: <input type="text" name="username"><br>
Message: <input type="text" mame="message"><br>
<input type="submit" value="submit">
</form>
Next we will need to create a simple php script to process their submit
and insert the info into the database. We will use $_SERVER['REMOTE_ADDR']
to get their ip address and use the php time() function to get the timestamp
of their post. Ok now some code to process the submit:
php:
<?php
if($_POST)
/* check for a form submit */
{
$time =
time();
/* get the current timestamp */
/*
* insert their info into database using
* $_SERVER['REMOTE_ADDR'] for their ip,
* also use addslashes to escape characters
* like quotes etc.
*/
$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");
}
?>
|
|
 |