Enable each user to vote up to 5 times within 10 minutes of a site
function isfrequently ($key) { $t = 60*10; $n = 5; $mem = new Memcache (); $mem->connect (' 127.0.0.1 ', ' 11211 '); if (! $mem) { echo ' memcache connection failed! '; Exit; } $val = Unserialize ($mem->get ($key)); $val = $val? $val: Array ();/ if (count ($val) >= 5 && time ()-$val [0]< $t) {// echo ' 10 minutes maximum of 5 votes ';/ } else{// if (count ($val) > 1) {// array_shift ($val);// }// $val [] = time ();// $mem Set ($key, serialize ($val), 0, $t);// Echo ' voted successfully! ';// } if (count ($val) < 5 | | $b = array_shift ($val) <time ()-$t) { $val [] = time (); $mem->set ($key, Serialize ($val), 0, $t); Echo ' voted successfully! '; } else{ Echo ' votes up to 5 times within 10 minutes '; }
Note: $b = Array_shift ($val) <time ()-The operator priority in the $t is-above <,< above =. So the right side is less than set up, $b = 1, otherwise $b=0.
Start writing the comment section code, and then optimize it to | | Learn more in depth. When NULL is compared to a number, the default is 0.
Use Memcache or Redis to limit the maximum number of votes for a user or an IP user over a period of time