[英]Restrict access to pages in php
我有一個LAMP服務器,我的主應用程序頁面每3秒需要新的ajax請求。 為了防止服務器過載,我想阻止普通查看者(那些不是付費客戶的用戶)僅打開應用程序頁面的單個實例,而付費客戶可以打開該頁面的多個實例。
有什么想法嗎?
謝謝
假設您在用戶上設置了一些cookie,則當AJAX請求到達時,它還將包含該cookie。 編寫一個函數來驗證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可能在多個選項卡上相同,但是如果數字足夠大,則可能性很小。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.