Article From:https://segmentfault.com/q/1010000009558772
Question:

For example, if 5 tags are generated within one minute, prompt for frequent operation, please enter the verification code.

What should we do?

Answer 0:

Record the generation time of each tag.
If the rule is, only 4 tags can be generated in one minute, and fifth will be prompted to report errors.
Just before adding the fifth, compare the generation time of the first of the last four with the current time, is it more than 60 seconds, if not, it will report an error.

Answer 1:

This is very convenient to implement using redis, using a key to store the number of submissions, if key is count.
Count is extracted from redis at every time of submission.

  1. If count is empty, set the count value to 1 and set the time out for one minute to submit normally.

  2. If the count value is not empty and greater than or equal to 5, the error prompt is “frequently operated, please enter the verification code”.

  3. If the count value is not empty and less than 5, it is submitted normally and the count value plus one.

Answer 2:

Record two values in session, one is the number of submission $count, the other is the commit time $time. Detect + + $count = = 5 [condition 1] at each commit, and if so, time () – $time & gt; = 60 seconds [condition 2], twoIf all parts are satisfied, prompt operation is frequent. If only 1 conditions are satisfied, $count=0; $time=time ();

Answer 3:

Redis cache operation is recommended.

<?php
try {
  $redis = new Redis(); // Create an instance$redis-> connect (REDIS_HOST, REDIS_PORT, REDIS_TIMEOUT); / / connection$redis-> Ping (); / / confirm connectionHave been successful} catch (Exception $e) {Die ('Can not connect Redis.');}$incrkey ='TEST: user: Min '; / / cached K per minuteEy$incrValue = $redis-> incr ($incrkey);If ($incrValue = = 1) {/ / set cache time (key name, cache time [unit: seconds])$rediS-> expire ($incrkey, 60);} else if ($incrValue > = 5) {Die ("frequent operation, please enter the verification code");}

Similar Posts:

Leave a Reply

Your email address will not be published. Required fields are marked *