jem
jem

Reputation: 257

Disable HTTP GET for some pages php

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

Answers (3)

Quentin
Quentin

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

Johni
Johni

Reputation: 2959

You can read the current method and redirect the user if it is GET.

Upvotes: 0

xdazz
xdazz

Reputation: 160883

Put the code below at the beginning.

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
  header('Location: a.html');
  exit;
}

Upvotes: 1

Related Questions