HTML<\/a> form via the POST method only. To be able to handle this feature you need to know how to detect the correct HTTP request method.<\/p>\n\n\n\nThe simple solution is to check the request method using:<\/p>\n\n\n\n
$_SERVER['REQUEST_METHOD']<\/code><\/pre>\n\n\n\nA simple use case would look something like this:<\/p>\n\n\n\n
if ( $_SERVER['REQUEST_METHOD'] === 'POST' ) {\n \/\/ Do something because the request method is POST\n }\n else {\n \/\/ Abort whatever\n }<\/code><\/pre>\n\n\n\nThe above method does not bode well from a security standpoint. The input here has not been validated nor sanitized. One way to fix this is to use the built-in filter_input<\/code> PHP function. The above code would look something like this now:<\/p>\n\n\n\n$request_method = filter_input(INPUT_SERVER, 'REQUEST_METHOD', FILTER_SANITIZE_ENCODED);\n\nif ( $request_method === 'POST' ) {\n\/\/ Do something because the request method is POST\n}\nelse {\n\/\/ Abort whatever\n}<\/code><\/pre>\n\n\n\nIf you want to test and handle different request methods differently, a more elegant way would be to use the PHP built-in switch statement. Here is a good example to start with. It also shows the supported HTTP request methods:<\/p>\n\n\n\n
$request_method = $_SERVER['REQUEST_METHOD'];\n\nswitch ($request_method) {\n case 'GET':\n \/\/Handle GET Request here\n break;\n case 'POST':\n \/\/Here Handle POST Request here \n break;\n case 'PUT':\n \/\/ Handle PUT Request here \n break;\n case 'PATCH':\n \/\/ Handle PATCH Request here \n break;\n case 'DELETE':\n \/\/Here Handle DELETE Request\n echo 'You are using '.$method.' Method';\n break;\n case 'COPY':\n \/\/ Handle COPY Request here\n echo 'You are using '.$method.' Method';\n break;\n\n case 'OPTIONS':\n \/\/ Handle OPTIONS Request \n break;\n case 'LINK':\n \/\/ Handle LINK Request here \n break;\n case 'UNLINK':\n \/\/ Handle UNLINK Request here \n break;\n case 'PURGE':\n \/\/Here Handle PURGE Request here \n break;\n case 'LOCK':\n \/\/ Handle LOCK Request here \n break;\n case 'UNLOCK':\n \/\/Here Handle UNLOCK Request here \n break;\n case 'PROPFIND':\n \/\/ Handle PROPFIND Request here \n break;\n case 'VIEW':\n \/\/Here Handle VIEW Request \n break;\n Default:\n \/\/ Handle default behavior\n break;\n}<\/code><\/pre>\n\n\n\nThat’s it. There is not much to this but with these simple tips, you are now on your way to handling better code in terms of readability and security. Furthermore, you are now past the first hurdle to building your very own REST API.<\/p>\n","protected":false},"excerpt":{"rendered":"
This is another beginner PHP article this time focusing on how to detect the HTTP request method. If you are a more competent developer then you might want to jump…<\/p>\n","protected":false},"author":1,"featured_media":6824,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,23,27,16],"tags":[60,303,304,313,320,433,452,526,543,544,635,636,638],"yoast_head":"\n
How do I Detect the HTTP Request Method Using PHP?<\/title>\n \n \n \n \n \n \n \n \n \n \n \n \n \n\t \n\t \n\t \n \n \n \n \n \n\t \n\t \n\t \n