萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> php編程 >> 阻止網頁被用戶頻繁刷新

阻止網頁被用戶頻繁刷新

 一般情況下,用戶浏覽網頁的速度都是幾秒十幾秒甚至更長時間刷新一頁,但有時候又會遇到網頁被惡意快速刷新,從而導致正常用戶浏覽速度緩慢,如何來解決這個問題呢?可以使用如下代碼來實現每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();
?>
以上代碼在真實的用戶環境下,是可以實現的
copyright © 萬盛學電腦網 all rights reserved