Use Memcache or Redis to limit the maximum number of votes for a user or an IP user over a period of time

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.