Johnson
Johnson

Reputation: 818

PHP: Making time limit

Im using ajax call to insertmsg.php to insert a message.

Now javascript isn't a realtime language so i can not count on having an disabling button at the top of the ajax function.

So I wonder how I can make a timelimit to the insertmsg.php with: mysql_query("INSERT INTO.. , with a delay on 2 seconds, without storing last time the user wrote in the database. Or is that the only way? How can I do it with session maybe?

Upvotes: 0

Views: 288

Answers (4)

Alin P.
Alin P.

Reputation: 44346

I think I understand what you want. But you English is very bad...

You want to prevent the user from sending messages at intervals smaller than 2 seconds.

You can do this both on the client side and on the server side, but the client side check can be easily disabled by a willing user.

To do it on the server side, in PHP, you need to store the time when the user last sent a message.

$_SESSION['lastMessageTime'] = time();

When a call for a new insert is made you just check

if(time() - $_SESSION['lastMessageTime'] <= 2)
    exit('Not allowed!')

Don't forget to enable you session before using the $_SESSION variable.


Or maybe you want your script to do the insert with a delay of 2 seconds?

You can wait in PHP with the sleep function. Or you can delay an action in JavaScript with setTimeout.

Upvotes: 1

SW4
SW4

Reputation: 71150

Not sure I understand- Javascrip is realtime, in the sense you can set a timer to run a checking function (for a response) at a set interval. If you only wish the PHP script to run for a set time, set_time_limit (use http://php.net/manual/en/function.set-time-limit.php)

Upvotes: 0

Johnson
Johnson

Reputation: 818

Once you inserted a comment, give a sessionvariable, with time() and then when next time it will try to insert, check for if sessionvariable > 120. If yes, run query.....

Upvotes: 0

Tokk
Tokk

Reputation: 4502

Are you trying to determinate whether the user answers a querstion in time?

If so you can store the time you send the question to the site in a session, and compare to the time of sending with the time when you retrieve a new ajax call. Then you know how long the user spent to type the answer.

Upvotes: 0

Related Questions