您的位置:首页 > 编程语言 > PHP开发

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

2015-10-18 11:43 627 查看
一般情况下,用户浏览网页的速度都是几秒十几秒甚至更长时间刷新一页,但有时候又会遇到网页被恶意快速刷新,从而导致正常用户浏览速度缓慢,如何来解决这个问题呢?可以使用如下代码来实现每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();
?>
以上代码在真实的用户环境下,是可以实现的
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: