{"id":6776,"date":"2018-11-05T13:01:33","date_gmt":"2018-11-05T18:01:33","guid":{"rendered":"http:\/\/local.brightwhiz\/?p=6776"},"modified":"2021-12-08T11:18:26","modified_gmt":"2021-12-08T16:18:26","slug":"detect-http-request-method-php","status":"publish","type":"post","link":"http:\/\/local.brightwhiz\/detect-http-request-method-php\/","title":{"rendered":"You can Detect the HTTP Request Method Using PHP by Doing This"},"content":{"rendered":"\n

This is another beginner PHP<\/a> article this time focusing on how to detect the HTTP<\/a> request method. If you are a more competent developer then you might want to jump onto another article. How about this one on how to get both GET and POST methods in one request<\/a>.<\/p>\n\n\n\n

Why the Interest in Detecting the HTTP Request Method<\/h2>\n\n\n\n

Back to the point of this guide. The most compelling answer to this is, it is all about security<\/a>. We know there is more than one request method when web browsers respond to servers. You, the developer may want to restrict your processes to certain response types.<\/p>\n\n\n\n

These are also important when building code such as REST APIs<\/a>.<\/p>\n\n\n\n

A simple example would be to receive passwords from an 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\n

The simple solution is to check the request method using:<\/p>\n\n\n\n

$_SERVER['REQUEST_METHOD']<\/code><\/pre>\n\n\n\n

A 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\n

The 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\n

If 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\n

That’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":"\nHow do I Detect the HTTP Request Method Using PHP?<\/title>\n<meta name=\"description\" content=\"This guide will show you how to use PHP to access the HTTP request method such as GET, POST, PURGE, PUT, and DELETE among others\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/local.brightwhiz\/detect-http-request-method-php\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How do I Detect the HTTP Request Method Using PHP?\" \/>\n<meta property=\"og:description\" content=\"This guide will show you how to use PHP to access the HTTP request method such as GET, POST, PURGE, PUT, and DELETE among others\" \/>\n<meta property=\"og:url\" content=\"http:\/\/local.brightwhiz\/detect-http-request-method-php\/\" \/>\n<meta property=\"og:site_name\" content=\"Brightwhiz.com\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/brightwhiz\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-11-05T18:01:33+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-12-08T16:18:26+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2018\/10\/http-request-method.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"630\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Michael Bright\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@brightwhizmag\" \/>\n<meta name=\"twitter:site\" content=\"@brightwhizmag\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Michael Bright\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"http:\/\/local.brightwhiz\/detect-http-request-method-php\/#article\",\"isPartOf\":{\"@id\":\"http:\/\/local.brightwhiz\/detect-http-request-method-php\/\"},\"author\":{\"name\":\"Michael Bright\",\"@id\":\"http:\/\/local.brightwhiz\/#\/schema\/person\/81f0f3126f13834ae2e7f381b3028e32\"},\"headline\":\"You can Detect the HTTP Request Method Using PHP by Doing This\",\"datePublished\":\"2018-11-05T18:01:33+00:00\",\"dateModified\":\"2021-12-08T16:18:26+00:00\",\"mainEntityOfPage\":{\"@id\":\"http:\/\/local.brightwhiz\/detect-http-request-method-php\/\"},\"wordCount\":309,\"publisher\":{\"@id\":\"http:\/\/local.brightwhiz\/#organization\"},\"image\":{\"@id\":\"http:\/\/local.brightwhiz\/detect-http-request-method-php\/#primaryimage\"},\"thumbnailUrl\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2018\/10\/http-request-method.jpg\",\"keywords\":[\"APIs\",\"HTML\",\"HTTP\",\"InfoSec\",\"Internet\",\"Optimization\",\"PHP\",\"Security\",\"Software Design\",\"Software development\",\"Web\",\"Web Applications\",\"Web Development\"],\"articleSection\":[\"Articles\",\"Guides\",\"Programming\",\"Technology\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"http:\/\/local.brightwhiz\/detect-http-request-method-php\/\",\"url\":\"http:\/\/local.brightwhiz\/detect-http-request-method-php\/\",\"name\":\"How do I Detect the HTTP Request Method Using PHP?\",\"isPartOf\":{\"@id\":\"http:\/\/local.brightwhiz\/#website\"},\"primaryImageOfPage\":{\"@id\":\"http:\/\/local.brightwhiz\/detect-http-request-method-php\/#primaryimage\"},\"image\":{\"@id\":\"http:\/\/local.brightwhiz\/detect-http-request-method-php\/#primaryimage\"},\"thumbnailUrl\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2018\/10\/http-request-method.jpg\",\"datePublished\":\"2018-11-05T18:01:33+00:00\",\"dateModified\":\"2021-12-08T16:18:26+00:00\",\"description\":\"This guide will show you how to use PHP to access the HTTP request method such as GET, POST, PURGE, PUT, and DELETE among others\",\"breadcrumb\":{\"@id\":\"http:\/\/local.brightwhiz\/detect-http-request-method-php\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/local.brightwhiz\/detect-http-request-method-php\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/local.brightwhiz\/detect-http-request-method-php\/#primaryimage\",\"url\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2018\/10\/http-request-method.jpg\",\"contentUrl\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2018\/10\/http-request-method.jpg\",\"width\":1200,\"height\":630,\"caption\":\"HTTP Request Method\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/local.brightwhiz\/detect-http-request-method-php\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/local.brightwhiz\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"You can Detect the HTTP Request Method Using PHP by Doing This\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/local.brightwhiz\/#website\",\"url\":\"http:\/\/local.brightwhiz\/\",\"name\":\"Brightwhiz.com\",\"description\":\"Best Tech guides, Tutorials, and News\",\"publisher\":{\"@id\":\"http:\/\/local.brightwhiz\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/local.brightwhiz\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"http:\/\/local.brightwhiz\/#organization\",\"name\":\"Brightwhiz\",\"url\":\"http:\/\/local.brightwhiz\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/local.brightwhiz\/#\/schema\/logo\/image\/\",\"url\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/11\/brightwhiz-com-logo-orange.png\",\"contentUrl\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/11\/brightwhiz-com-logo-orange.png\",\"width\":706,\"height\":135,\"caption\":\"Brightwhiz\"},\"image\":{\"@id\":\"http:\/\/local.brightwhiz\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/brightwhiz\/\",\"https:\/\/x.com\/brightwhizmag\",\"https:\/\/instagram.com\/bright_whiz\/\",\"https:\/\/www.pinterest.com\/sobbayi\/\",\"https:\/\/www.youtube.com\/channel\/UC6sCdP_d_RiTIM7ErFT-PSQ\"]},{\"@type\":\"Person\",\"@id\":\"http:\/\/local.brightwhiz\/#\/schema\/person\/81f0f3126f13834ae2e7f381b3028e32\",\"name\":\"Michael Bright\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/local.brightwhiz\/#\/schema\/person\/image\/\",\"url\":\"http:\/\/1.gravatar.com\/avatar\/da90485875ff0aafa38fdd494abe87d1?s=96&d=mm&r=g\",\"contentUrl\":\"http:\/\/1.gravatar.com\/avatar\/da90485875ff0aafa38fdd494abe87d1?s=96&d=mm&r=g\",\"caption\":\"Michael Bright\"},\"sameAs\":[\"https:\/\/sobbayi.com\"],\"url\":\"http:\/\/local.brightwhiz\/author\/sobbayiadmin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How do I Detect the HTTP Request Method Using PHP?","description":"This guide will show you how to use PHP to access the HTTP request method such as GET, POST, PURGE, PUT, and DELETE among others","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/local.brightwhiz\/detect-http-request-method-php\/","og_locale":"en_US","og_type":"article","og_title":"How do I Detect the HTTP Request Method Using PHP?","og_description":"This guide will show you how to use PHP to access the HTTP request method such as GET, POST, PURGE, PUT, and DELETE among others","og_url":"http:\/\/local.brightwhiz\/detect-http-request-method-php\/","og_site_name":"Brightwhiz.com","article_publisher":"https:\/\/www.facebook.com\/brightwhiz\/","article_published_time":"2018-11-05T18:01:33+00:00","article_modified_time":"2021-12-08T16:18:26+00:00","og_image":[{"width":1200,"height":630,"url":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2018\/10\/http-request-method.jpg","type":"image\/jpeg"}],"author":"Michael Bright","twitter_card":"summary_large_image","twitter_creator":"@brightwhizmag","twitter_site":"@brightwhizmag","twitter_misc":{"Written by":"Michael Bright","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"http:\/\/local.brightwhiz\/detect-http-request-method-php\/#article","isPartOf":{"@id":"http:\/\/local.brightwhiz\/detect-http-request-method-php\/"},"author":{"name":"Michael Bright","@id":"http:\/\/local.brightwhiz\/#\/schema\/person\/81f0f3126f13834ae2e7f381b3028e32"},"headline":"You can Detect the HTTP Request Method Using PHP by Doing This","datePublished":"2018-11-05T18:01:33+00:00","dateModified":"2021-12-08T16:18:26+00:00","mainEntityOfPage":{"@id":"http:\/\/local.brightwhiz\/detect-http-request-method-php\/"},"wordCount":309,"publisher":{"@id":"http:\/\/local.brightwhiz\/#organization"},"image":{"@id":"http:\/\/local.brightwhiz\/detect-http-request-method-php\/#primaryimage"},"thumbnailUrl":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2018\/10\/http-request-method.jpg","keywords":["APIs","HTML","HTTP","InfoSec","Internet","Optimization","PHP","Security","Software Design","Software development","Web","Web Applications","Web Development"],"articleSection":["Articles","Guides","Programming","Technology"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"http:\/\/local.brightwhiz\/detect-http-request-method-php\/","url":"http:\/\/local.brightwhiz\/detect-http-request-method-php\/","name":"How do I Detect the HTTP Request Method Using PHP?","isPartOf":{"@id":"http:\/\/local.brightwhiz\/#website"},"primaryImageOfPage":{"@id":"http:\/\/local.brightwhiz\/detect-http-request-method-php\/#primaryimage"},"image":{"@id":"http:\/\/local.brightwhiz\/detect-http-request-method-php\/#primaryimage"},"thumbnailUrl":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2018\/10\/http-request-method.jpg","datePublished":"2018-11-05T18:01:33+00:00","dateModified":"2021-12-08T16:18:26+00:00","description":"This guide will show you how to use PHP to access the HTTP request method such as GET, POST, PURGE, PUT, and DELETE among others","breadcrumb":{"@id":"http:\/\/local.brightwhiz\/detect-http-request-method-php\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/local.brightwhiz\/detect-http-request-method-php\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/local.brightwhiz\/detect-http-request-method-php\/#primaryimage","url":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2018\/10\/http-request-method.jpg","contentUrl":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2018\/10\/http-request-method.jpg","width":1200,"height":630,"caption":"HTTP Request Method"},{"@type":"BreadcrumbList","@id":"http:\/\/local.brightwhiz\/detect-http-request-method-php\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/local.brightwhiz\/"},{"@type":"ListItem","position":2,"name":"You can Detect the HTTP Request Method Using PHP by Doing This"}]},{"@type":"WebSite","@id":"http:\/\/local.brightwhiz\/#website","url":"http:\/\/local.brightwhiz\/","name":"Brightwhiz.com","description":"Best Tech guides, Tutorials, and News","publisher":{"@id":"http:\/\/local.brightwhiz\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/local.brightwhiz\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"http:\/\/local.brightwhiz\/#organization","name":"Brightwhiz","url":"http:\/\/local.brightwhiz\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/local.brightwhiz\/#\/schema\/logo\/image\/","url":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/11\/brightwhiz-com-logo-orange.png","contentUrl":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/11\/brightwhiz-com-logo-orange.png","width":706,"height":135,"caption":"Brightwhiz"},"image":{"@id":"http:\/\/local.brightwhiz\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/brightwhiz\/","https:\/\/x.com\/brightwhizmag","https:\/\/instagram.com\/bright_whiz\/","https:\/\/www.pinterest.com\/sobbayi\/","https:\/\/www.youtube.com\/channel\/UC6sCdP_d_RiTIM7ErFT-PSQ"]},{"@type":"Person","@id":"http:\/\/local.brightwhiz\/#\/schema\/person\/81f0f3126f13834ae2e7f381b3028e32","name":"Michael Bright","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/local.brightwhiz\/#\/schema\/person\/image\/","url":"http:\/\/1.gravatar.com\/avatar\/da90485875ff0aafa38fdd494abe87d1?s=96&d=mm&r=g","contentUrl":"http:\/\/1.gravatar.com\/avatar\/da90485875ff0aafa38fdd494abe87d1?s=96&d=mm&r=g","caption":"Michael Bright"},"sameAs":["https:\/\/sobbayi.com"],"url":"http:\/\/local.brightwhiz\/author\/sobbayiadmin\/"}]}},"_links":{"self":[{"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/posts\/6776"}],"collection":[{"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/comments?post=6776"}],"version-history":[{"count":0,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/posts\/6776\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/media\/6824"}],"wp:attachment":[{"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/media?parent=6776"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/categories?post=6776"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/tags?post=6776"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}