亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何通過 REQUEST_METHOD 拒絕頁面請求?

如何通過 REQUEST_METHOD 拒絕頁面請求?

PHP
慕容708150 2021-09-05 16:07:46
我有一個頁面應該只允許“POST”標題,但它目前接受所有。這是我已經擁有的代碼。echo 顯示使用 Postman 測試時使用的方法,無論我使用什么方法我都會得到 200 OK 結果。我是否需要在 .htaccess 或 Apache 配置中進一步添加任何內容?// required headers    header("Access-Control-Allow-Origin: *");    header("Content-Type: application/json; charset=UTF-8");    header("Access-Control-Allow-Methods: POST");    header("Access-Control-Max-Age: 3600");    header("Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With");    echo ($_SERVER["REQUEST_METHOD"]);
查看完整描述

2 回答

?
萬千封印

TA貢獻1891條經驗 獲得超3個贊

要通過 PHP 檢查允許的方法并發出單個錯誤:


if ($_SERVER["REQUEST_METHOD"] !== 'POST') {

    header('HTTP/1.0 403 Forbidden');

    echo 'This method is not allowed!';

    exit;

}

// Here comes your regular code


查看完整回答
反對 回復 2021-09-05
?
尚方寶劍之說

TA貢獻1788條經驗 獲得超4個贊

要只允許POST請求,您可以將其添加到 htaccess 文件中:


<LimitExcept POST HEAD>

    Order Allow,Deny

    Deny from all

</LimitExcept>

編輯


或者您可以在 PHP 腳本上執行此操作:


$currentRequestMethod = $_SERVER['REQUEST_METHOD'];


//A PHP array containing the methods that are allowed.

$allowedRequestMethods = array('POST', 'HEAD');


//Check to see if the current request method isn't allowed.

if(!in_array($currentRequestMethod, $allowedRequestMethods)){

    //Send a "405 Method Not Allowed" header to the client and kill the script

    header($_SERVER["SERVER_PROTOCOL"]." 405 Method Not Allowed", true, 405);

    exit;

}


查看完整回答
反對 回復 2021-09-05
  • 2 回答
  • 0 關注
  • 198 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號