PHP Request Methods

August 1, 2013

In PHP, it supports various request methods depends on which the capabilities and functionalities to be applied on request data before sending it to server, will be varied. The list of PHP supported request methods are,

  • GET
  • POST
  • PUT
  • HEAD

We can know about which method is used on server page request, by using $_SERVER variable‘s REQUEST_METHOD index, we have seen recently. In this article, we are going to discuss about these types of PHP request methods to know their usage, limitations and etc.

php_request_method

PHP GET Method

This is the default method used, while sending data by submitting HTML form. If a HTML form contains either GET or nothing as the value of its method attribute, then, the form fields values will be loaded into $_GET super global, using which we can access form fields from PHP script. So, on submitting with any value of HTML form’s method attribute except POST, this PHP request method will be taken by default.

On using this method, all the parameters will be sent as querystring of the URL to access PHP page from server. This querystring will be separated with ? mark. And, it contains all form field’s name, value pair separated with ampersand(&).

Let us have a simple example PHP program saved as php_get_request.php. It deal with PHP GET request method, for getting more clear knowledge about it.

<?php
if(count($_GET)>0) {
print "<PRE>";
print_r($_GET);
print "</PRE>";
}
?>
<form>
First Name: <input type="text" name="txt_first_name"></br>
Last Name: <input type="text" name="txt_last_name"></br>
<input type="submit" name="submit" value="Submit">
</form>

Since, form tag doesn’t have any specification for request method, the default method GET will be taken. The following screen shot shows the entries of form fields.

php_form_submit

On submitting these inputs, the URL requesting server page will be as follows.


http://localhost/php_samples/php_request_methods/get_request.php?txt_first_name=Vincy&txt_last_name=PHPPOT&submit=Submit.

If we send form data containing some special characters, then the URL will be encoded with the rules we have seen with PHP URL encoding.

Since, all parameters are shown transparently with the requesting URL, this is not a secure method to pass sensitive data like password, payment information and etc. In such situation, PHP POST method is used.

Sending POST request for PHP file

This method is used to send large sized data without any size limitation in GET method. In this method the data to be sent for accessing PHP page from server will be done through HTTP header in secure manner without transparency.

PHP POST methods are also used to send selected files to be uploaded to the target location of server.

For sending data in such manner via a HTML form submit, we need to specify form method as POST. After changing form method, it is required to use $_POST global inside PHP portion of above example, to display the posted data to the browser.

Now, the above code can be changed as,

<?php
if($_SERVER["REQUEST_METHOD"] == "POST") {
if(count($_POST)>0) {
print "<PRE>";
print_r($_POST);
print "</PRE>";
}
}
?>
<form method="POST">
First Name: <input type="text" name="txt_first_name"></br>
Last Name: <input type="text" name="txt_last_name"></br>
<input type="submit" name="submit" value="Submit">
</form>

While executing this program, no querystring will be appended with page URL; Rather the arguments are sent through HTTP headers.

PHP PUT Request

We can not use this method while requesting server page via HTML form submit. Because, the above two kind of PHP request methods are valid to use with form input. If we want to send PUT request, then we can obtain it by using curl script or command line execution.

This is the easiest way of putting required file to the target location of server, compared with PHP POST method. But, for handling such requests in secure manner to avoid anonymous overwrite, we need configure with apache conf file by using script directive.

HEAD Method in PHP

The server generated response data with the header returned for this type of request will be same as that of GET request. But, the difference is, that the response of HEAD request wont contain any element body, rather contains information about the element, instead. So, this type of request will be used for some verification purpose.

Comments on
PHP Request Methods

Add a Comment