A Simple Way to Set PHP Page Only Allow HTTP Post Method Request – PHP Tutorial

By | August 9, 2019

When we open a url by our browser, the browser will use http get method to send a request. However, if you only want to allow http post method, how to do? In this tutorial, we will introduce you how to set a php page only receive http post method request.

http request method get vs post

Here is the core example code

if ( 'POST' != $_SERVER['REQUEST_METHOD'] ) {
	header('Allow: POST');
	header('HTTP/1.1 405 Method Not Allowed');
	header('Content-Type: text/plain');
	echo "only allow post";
	exit;
}

Add code above to the top of you php file.

The key is $_SERVER[‘REQUEST_METHOD’] contains http request medthods, we can check it to implement different process.

Category: PHP

Leave a Reply

Your email address will not be published. Required fields are marked *