Reputation: 257
I want to disable HTTP GET method for some of my php pages in my website. Can I do it in php code?
Lets suppose I have two pages a.html and action.php. Now a.html is normal page can be accessed with any method and it submits the information to action.php. But I want that information submitted to action.php can only be submitted through post method.
Upvotes: 3
Views: 4233
Reputation: 943999
Send a Method Not Allowed header along with some explanatory text (preferably something friendlier and more useful then my example below).
Exit afterwards so you don't continue processing with the regular page.
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
header('Method Not Allowed', true, 405);
echo "GET method requests are not accepted for this resource";
exit;
}
You might want to consider white listing (and testing for the absence of methods you do accept) instead of black listing GET. (Since you might not want PUT, DELETE, etc either).
Upvotes: 11
Reputation: 160883
Put the code below at the beginning.
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
header('Location: a.html');
exit;
}
Upvotes: 1