Jump to content
MakeWebGames

Recommended Posts

Posted

I'm afraid your question is too vague; please try and re-word it.

Anyone can enter a form into a site, and will be using their IP to do so (unless they have some kind of proxy set up).

If you're asking how to store the contents a user submit from a form using their IP as a sort of user-system, that's different.

Posted

I would like to make it so that I can store their IP into a database or something along those lines and then make it so they can only submit the form once per hour. Help me out a bit?

Posted

Assuming you already have a database connection (and database)..

Untested

[mysql]CREATE TABLE `your_table` (

`id` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY,

`name` VARCHAR( 50 ) NOT NULL DEFAULT 'n/a',

`message` TEXT NOT NULL,

`ip` VARCHAR( 20) NOT NULL DEFAULT '127.0.0.1',

`time` INT( 11 ) NOT NULL DEFAULT 0

) ENGINE = MyISAM;[/mysql]

 

$IP = isset($_SERVER['REMOTE_ADDR']) ? mysql_real_escape_string($_SERVER['REMOTE_ADDR'], $connection_identifier) : '127.0.0.1';
if(!isset($_POST['msg'])) {
$select = sprintf("SELECT `time` FROM `your_table` WHERE (`ip` = '%s')", $IP);
$query = mysql_query($select, $connection_identifier) or die("Could not execute query:
".mysql_error());
if(mysql_num_rows($query)) {
$row = mysql_fetch_assoc($query);
if($row['time'] < time() - 3600) {
echo "You have already posted a comment within the last hour.
Please wait for ".date('i:s', time() - $row['time'])." before attempting to post again";
//Page footer.
exit;
}
}
echo "<form action='comments.php' method='post'>";
echo "<table width='50%' style='text-align:center;'>";

echo "<tr>";
echo "<th>Your name</th>";
echo "<td><input type='text' name='name' /></td>";
echo "</tr>";

echo "<tr>";
echo "<th>Message</th>";
echo "<td><textarea name='msg' rows='10' cols='50'></textarea></td>";
echo "</tr>";

echo "<tr>";
echo "<td colspan='2'><input type='submit' value='Submit' /></td>";
echo "</tr>";

echo "</table>";
echo "</form>";
} else {
if(empty($_POST['name'])) {
echo "You didn't enter your name";
//Insert page footer here
exit;
}
if(empty($_POST['msg'])) {
echo "You didn't enter a message";
//Page footer..
exit;
}

$_POST['name'] = mysql_real_escape_string($_POST['name'], $connection_identifier);
$_POST['msg'] = mysql_real_escape_string($_POST['msg'], $connection_identifier);
$query = sprintf("INSERT INTO `your_table` VALUES ('', '%s', '%s', '%s', %u)", $_POST['name'], $_POST['msg'], $IP, time());
mysql_query($query, $connection_identifier) or die("Could not execute query:
".mysql_error());
echo "Your comment has been submitted";
}

 

Be sure to use htmlspecialchars() when outputting from the database.

Use stripslashes() if you get backslashes (\) before speech marks and apostrophies

  • 2 weeks later...
Posted

Itd be alot better if you just did it by userid's. I mean ppl can create multiple accounts, but then again, if u did it by ip, it would make it easier for them considering using a proxy can still post before an hour

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...