簡體   English   中英

防止通過AJAX調用直接訪問文件

[英]Prevent direct access to file through AJAX call

在這個問題上,我想盡辦法了,我想防止人們直接訪問使用AJAX調用的文件。

簡而言之:

  • main-script.phpajax-request.php進行AJAX調用
  • 只能從main_script.php調用ajax-request.php
  • 禁止直接訪問ajax-request.php

我已經讀到這樣similiar問題這一個 ,但“接受的答案”在這里(像其他許多問題的答案),好像它不應該被接受。 但是,引起我注意的是這個答案 ,他在這里談論使用$_SESSION和哈希。 現在的問題是(1)如果您真的可以阻止人們在這種情況下直接訪問,我表示懷疑; (2)我無法確定如何使用會話和哈希來實現這一目標。

因此,如果有人可以幫助我進行思考或在正確的方向上給予我幫助(或在根本不可能的情況下提供建議),我將不勝感激。

只要我知道該位置上有一些資源,就不能阻止我請求yourdomain/ajax-request.php ,但是您可以防止對該文件的未經授權的訪問,類似於防止來賓訪問您的會員區域的方法網站或任何需要授權的資源。 區別在於我沒有通過提供密碼來獲得授權訪問,不,僅當我請求main_script.php ,我才可以訪問ajax-request.php

例如,我要做的最簡單的方法是使用( $_COOKIE和數據庫)或( $_SESSION

main_script.php

$_SESSION['canRequestAjax'] = 1;

other_scripts.php

unset($_SESSION['canRequestAjax']);
/* 
 remove the variable if he visited something else, 
 because he can only request the ajax-request.php RIGHT AFTER the main_script.php
*/

ajax-request.php

if (empty($_SESSION['canRequestAjax'])){
    die('error'); // TODO: proper 403 response
}
unset($_SESSION['canRequestAjax']);
/* 
 remove the variable if he requested the ajax ajax-request.php file, only 1 ajax is allowed. 
NOTE: remove this line if the user may do more than 1 ajax requests when he is on
 main-script.php
*/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM