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

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;
}
- 2 回答
- 0 關注
- 198 瀏覽
添加回答
舉報