php阻止网页被用户频繁刷新

时间:2022-02-20 16:59:34

一般情况下,用户浏览网页的速度都是几秒十几秒甚至更长时间刷新一页,但有时候又会遇到网页被恶意快速刷新,从而导致正常用户浏览速度缓慢,如何来解决这个问题呢?可以使用如下代码来实现每ip页面访问数量限制:

<?php
$min_seconds_between_refreshes = 3;#设置刷新的时间

session_start();

if(array_key_exists('last_access', $_SESSION) && time()-$min_seconds_between_refreshes <= $_SESSION['last_access'])
{
// The user has been here at least $min_seconds_between_refreshes seconds ago - block them
exit('You are refreshing too quickly, please wait a few seconds and try again.');
}
// Record now as their last access time
$_SESSION['last_access'] = time();
?>
以上代码在真实的用户环境下,是可以实现的