views:

39

answers:

3

I have a timer on the site which the user will only be able to click on every * seconds/minutes/hours. Every user has a timer value in the users table where the values are written and read. The value in the database is the amount of seconds that the user will have to wait.

I've figured out how to make the timer countdown from the correct value in the sql database but I have yet to figure out how to make it servside secure.

What would be the most effective way to detect when clicking to early?

A: 

Hm, when user clicks, you record the time (server time), subtract the value from the db and see if it is less than the value that's been set.

Maybe you should provide more detail...

playcat
I'll try to give as much detail as possible. What would you like to know?
Kraffs
How do you 'recognize' users? By cookie? Session? Or is it a logged user? That said, I think it's a simple matter of checking if user already clicked the timer, in which case you simply subtract values and determine if you can accept the click or not. If it's a first click, just store the value and that's it.
playcat
Regarding security, just make sure that script that takes care of time management can't be called by a person - define a constant in another script and check if that constant is defined when 'time management' script is called.
playcat
How would I use the date to check if * seconds have passed?
Kraffs
use time() - it gives the number of secondes since unix epoch
playcat
A: 

I am with Playcat on this.

On first click store the time in the database. On second click select first click; Check if now – firstclick < userTimervalue

IF so then the the user didn’t wait long enough return false ELSE return true and update firstclick to currenttime.

The only thing I would add is you should add clientside javascript to mimic the timer it can reduce the checks against the database.

Sam Plus Plus
A: 

You can either fetch the whole record and compare using PHP, or issue a SQL command checking it. Assuming MySQL:

SELECT IF(COUNT(id), 1, 0) AS allowed FROM some_table
WHERE NOW() > DATE_ADD(timestampCol, INTERVAL 5 MINUTE) AND user_id = :userid;

http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-add

Not sure what that has to do with "security" as it's commonly referred to though. If your question is "how do I secure an application", the question is too broad for quick answer. If your question is "how do make sure this works", then this is the way.

Joe