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 );}}}