2011-02-16 114 views
0

我有一個LAMP服務器,我的主應用程序頁面每3秒要求一次新的Ajax請求。爲了防止服務器過載,我想阻止普通觀衆(不支付客戶的那些人)僅打開應用程序頁面的單個實例,而付費客戶端可以打開頁面的多個實例限制訪問php中的頁面

任何我的想法?

感謝

+0

什麼樣的信息。您認爲可以阻止來自「未知」用戶的付費用戶 – RobertPitt 2011-02-16 08:20:40

回答

1

假設你有用戶,當AJAX請求到達時,它還會包含一些餅乾餅乾集。寫一個函數來驗證的cookie(如:isUserLoggedIn()),並監視用戶如何經常請求一個頁面:

$minLoggedOutRequestDelay = 3; 

// Set up the variable for the first time 
if (! isset($_SESSION["lastAjaxRequest"])) 
{ 
    $_SESSION["lastAjaxRequest"] = 0; 
} 

if ($_SESSION["lastAjaxRequest"] - microtime() > $minLoggedOutRequestDelay 
    AND (! isUserLoggedIn())) 
{ 
    // Do something to stop the request from going through 
    // or maybe just log it 
} 
$_SESSION["lastAjaxRequest"] = microtime(); 

// Continue as normal 

這將導致只有一個選項卡,馬上開始工作。如果他們有多個打開,由於網絡延遲,「活動」選項卡可能會在選項卡之間切換。要根據打開多少個選項卡並使其中一個完全工作並且其他人完全不工作,您需要在頁面加載時生成一個隨機數。把它作爲(如AJAX請求分辨不同頁面的一部分:...&pageRandomNumber=828918&...

$minLoggedOutRequestDelay = 3; 
$maxLoggedOutPages = 1; 

// Set up the array in case its the first time 
if (! isset($_SESSION["lastAjaxRequest"])) 
{ 
    $_SESSION["lastAjaxRequest"] = array(); 
} 

// Trim inactive pages from the array 
foreach ($_SESSION["lastAjaxRequest"] as $pageRandomNumber => $lastTime) 
{ 
    if ($lastTime - microtime() > $minLoggedOutRequestDelay * 2) 
    { 
     unset($_SESSION["lastAjaxRequest"][$pageRandomNumber]); 
    } 
} 

// Make sure the current page is initialised 
if (! isset($_SESSION["lastAjaxRequest"][$_REQUEST["pageRandomNumber"]])) 
{ 
    $_SESSION["lastAjaxRequest"][$_REQUEST["pageRandomNumber"]] = 0; 
} 

if ((! isUserLoggedIn()) 
    AND count($_SESSION["lastAjaxRequest"]) > $maxLoggedOutPages) 
{ 
    // Do something to stop the request from going through 
    // or maybe just log it 
} 
$_SESSION["lastAjaxRequest"][$_REQUEST["pageRandomNumber"]] = microtime(); 

// Continue as normal 

它可能pageRandomNumber是在多個選項卡相同,但極不可能給予足夠的數字