Link to home
Create AccountLog in
Avatar of BadRomeo
BadRomeo

asked on

PHP VOTING - Time Interval Check

Creating a php voting system.  I already use cookies and sessions to track the users ip for duplicate voting as the very first check.  - This works good.

However, I am extending the check even further to check the db to see the date that user voted.. and even a step further, to check what time the user voted and if he voted within a given time interval such as 2 hours, he is denied voting again.  

So basically, I have the users last vote time.  I need to check if he voted within the set time interval(2) and if he didn't allow voting


Any suggesstions?

UPDATED:  What do you think about this code?
$time_interval=7200;
$stamp=time();	      
 
//time check
			if($last_vote_time > $stamp - $time_interval){
			     add_vote($band_id);
			}else{
			     $error="Voting not allowed for 2 hours";
			}//end if time check

Open in new window

Avatar of Richard Quadling
Richard Quadling
Flag of United Kingdom of Great Britain and Northern Ireland image

No.

Try ...

if($last_vote_time < strtotime('-2 hours')){

If the last vote occurred before the time that is 2 hours ago, then allow voting.


I find this more readable and doesn't require a "magic number". 7200 is obviously 2 hours, as that is what we are talking about. But in a months time, when you've used a load more different magic numbers, is 7200 going to be instantly recognisable?


Avatar of BadRomeo
BadRomeo

ASKER

can you pass -2 hours as a condition to strtotime or is that just for the sake of explanation?

There are only 4 different time settings so the issue of recognizing the number isnt an issue..besides, its only used in the core.. nothing frontend.
<?php
$i_RepeatVotingInterval = 2;


if($last_vote_time < strtotime("-$i_RepeatVotingInterval hours")) {
 // Allow voting
} else {
 // No voting
}

maybe.
Ah, I see...  

one more questions

$last_vote_time is populated from a DB, I am not quite sure how to classify this field... time? varchar? int?

its the same thing with date too...  i can specify the format in php.. but mysql uses its own.. do both of them have to match?
Store the date as a datetime in the database.

When you get the data back from the DB it will come back as text. So you will need to convert it to a PHP datetime type...

strtotime($a_Row['last_vote_time'])

So...

<?php
$i_RepeatVotingInterval = 2;

if(strtotime($a_Row['last_vote_time']) < strtotime("-$i_RepeatVotingInterval hours")) {
 // Allow voting
} else {
 // No voting
}


sort of thing.

Oh. Just in case you didn't notice.

The interval is defined without the sign, as an interval has no sign, it is just a gap.

So, in the comparison I want I use it with a leading - sign.

ime(" >>>  -  <<< $i_Repe

Just in case you missed it.
If you have a lot of these intervals, hold them all centrally in a config.php file and you only need to edit that 1 file rather than scanning loads of files to find and replace.

<?php
define('PHP_RUNNING','PHP_RUNNING');
include_once './config.php';

if (str....
?>



---config.php---
<?php
if (defined('PHP_RUNNING')) {
 $i_RepeatVotingInterval = 2;
}
?>

Not sure why I'm telling you this. I'm sure you know, but it is a Sunday morning and I'm trying to stay away from the kids...
You don't need a big if-structure to check if voting is allowed.  Just let him vote and don't update the database if within 2 hours.

UPDATE Table SET Vote=Vote+1 WHERE ADDDATE(VotingTime, INTERVAL 2 HOUR)<NOW()
The amount of processing saving is going to be negligible. Either in the PHP script which has to process SOMETHING or send it to the database on the off chance it is necessary.

If you let the DB do it, you then have to still process the output to either show a "You can't vote" sort of message.

what php function would i use to insert the date and time stamp into the DATETIME field in mysql?  Would it be date() ?
ASKER CERTIFIED SOLUTION
Avatar of Richard Quadling
Richard Quadling
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account