Jump to content

php help, limit script


johny30238

Recommended Posts

hello,

 

I have a form i am using for a raffle that i have weekly. the problem is i want to limit how many times they can register.  I am using a basic form that collects name, email, and IP ( which is hidden ). When it gets submitted there is time stamp as well using this

date("m/d/y : H:i:s", time()

 

I would like to restict registering by one hour at a time. can you please give me some help with this. I have thought of maybe using cookies, but some of my smarter members can bypass that. So i think i will either use email or IP .

 

any help would be very appreciated

Link to comment
Share on other sites

I hope it isn't too late to change the data type for the time field to DATETIME. Then you could insert a NOW() timestamp with each record, and use MySQL's date and time functions for the comparison. It can still be done if that isn't a possibility, but it won't be as easy.

Link to comment
Share on other sites

Well, I will create another column, and see if i can get that method working. One question though, can i made a hidden form value for NOW() and name it date then use like this when inserting:

mysql_query("INSERT INTO table VALUES(

                        '',

                        '".addslashes($_POST['name'])."',

                        '".addslashes($_POST['email'])."',

                        '".date("m/d/y : H:i:s", time())."',

                        '".addslashes($_POST['ip'])."',

                        '".addslashes($_POST['date'])."'

                        )") or die(mysql_error());

 

or will that method not work?

Link to comment
Share on other sites

The NOW() function is native to MySQL, so it would just go directly in the query string. Also, you should use the mysql-specific function mysql_real_escape_string() to escape string data rather than addslashes().

 

$name = mysql_real_escape_string($_POST['name']);
$email = mysql_real_ascape_string($_POST['email']); 
"INSERT INTO `table` (`name`, `datetime`, `email`) VALUES ( '$name', NOW(), '$email' )"

Link to comment
Share on other sites

I've given this a quick test on a local database and it works for me. You'll obviously need to insert the values that are assigned to the variables, and possibly make some other tweaks to it, but it should certainly get you started.

 

$current_user = ''; // enter identifying factor, such as IP address, user_id, etc. that is stored in database for comparison
$id_field = ''; // the name of the field in the DB table that holds the identifying information
$table = ''; // name of DB table storing data for this purpose
$last_entered = ''; // name of field that holds the timestamps in DB table

$query = "SELECT ( UNIX_TIMESTAMP( NOW() ) - UNIX_TIMESTAMP(`$last_entered`) ) AS `diff` FROM `$table` WHERE `$id_field` = $current_user ORDER BY `$last_entered` DESC LIMIT 1";
if( $result = mysql_query($query) ) {
$array = mysql_fetch_assoc($result);
if( mysql_num_rows($result) === 0 || $array['diff'] > 60 * 60 ) {
	// OK TO REGISTER
} else {
	// ALREADY REGISTERED WITHIN LAST HOUR
	echo "You already registered within the last hour. You must wait another " . ceil((3600 - $array['diff']) / 60) . " minutes before you can register again.";
}
} else {
// There was a problem with the query
echo "Database query failed";
}

Link to comment
Share on other sites

Thank you for writing this, but how now i get Database query failed. I did insert the correct data and i noticed after i left $current_user empty it would compare with he last entry. but when u do put a value to compare it will it gives me a error.

 

EDIT: i found the problem in the query $current_user was missing " ' ' " After that it worked perfectly.

Thanks alot for the help!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.