Use memcached in php to implement the page anti-refresh function

Source: Internet
Author: User
This article mainly introduces how to use memcached in php to implement the page anti-refresh function. this is a safe and stable anti-refresh solution. For more information, see add a new requirement, prevent users from refreshing pages frequently. The specific requirement is that when the user requests more than 100 specified pages within one minute, the request is rejected directly. After careful analysis, we found that memcache is the most convenient function:

1. use the user IP address and the requested file name as the KEY value.

2. the memcached method increment () is used to record the number of user visits. increment () is an increase in atomicity and accuracy.

3. set () to 60 s, so as to count the number of visits per minute.

Therefore, a function is written and called in the project's initialization file. the Function content is as follows:

Function requestCount () {$ patharr = pathinfo ($ _ SERVER ['php _ SELF ']); $ filename = explode ('? ', $ Patharr ['basename']); // Obtain the current file name if (in_array ($ filename ['0'], array ('index. php', 'login. php', 'password. php ') {global $ memcache; $ clientip = get_client_ip (); // Generate KEY $ ipkey = 'C' using the long integer IP address and file name '. sprintf ("% u", ip2long ($ clientip )). $ filename ['0']; if ($ visitCount = $ memcached-> get ($ ipkey) {if ($ visitCount = 100) {die ('Please don \'t refresh your page! ') ;}Else {$ memcache-> increment ($ ipkey) ;}} else {$ memcache-> set ($ ipkey, 60 );}}}

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.