Wednesday 8 August 2018

PHP: Detect If Request Type Is POST Or GET.

This is a simple guide on how to detect the request type with PHP. i.e. Is the user making a POST request or a GET request – or are they using one of the other HTTP Methods, such as PUT and DELETE.
Here is a simple example:
As you can see, the request method is accessible via the $_SERVER array.
Obviously, known the request type is handy, as it allows you to:
  1. Restrict HTTP requests to certain request types. i.e. If you have a page that allows a user to submit a comment, you might want to restrict that script to POST requests or PUT requests.
  2. It allows you to implement REST, which is a popular software architectural style that allows you to organize interactions between independent systems.
An example of handling different request methods in PHP:

0 comments:

Post a Comment