PHP Request Methods

by Vincy. Last modified on July 8th, 2022.

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

  • GET
  • POST
  • PUT
  • HEAD

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

PHP GET Method

This is the default method used while sending data by submitting an HTML form. If an 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 a PHP script. So, on submitting with any value of the 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 query string of the URL to access the PHP page from the server. This query string will be separated with? mark. And, it contains all form field’s name, value pair separated by an ampersand(&).

Let us have a simple example PHP program saved as php_get_request.php. It deals 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 the form tag doesn’t have any specification for the request method, the default method GET will be taken. The following screenshot 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 passwords, payment information and etc. In such a situation, the 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 the PHP page from the server will be done through an HTTP header in a secure manner without transparency.

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

For sending data in such a manner via an HTML form submit, we need to specify the form method as POST. After changing the form method, it is required to use $_POST global inside the PHP portion of the 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 query string will be appended with the page URL; Rather the arguments are sent through HTTP headers.

PHP PUT Request

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

This is the easiest way of putting the required file to the target location of the server, compared with the PHP POST method. But, for handling such requests in a secure manner to avoid anonymous overwrite, we need to 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 the same as that of the GET request. But, the difference is, that the response of HEAD request won’t contain any element body, but rather contains information about the element, instead. So, this type of request will be used for some verification purposes.

Vincy
Written by Vincy, a web developer with 15+ years of experience and a Masters degree in Computer Science. She specializes in building modern, lightweight websites using PHP, JavaScript, React, and related technologies. Phppot helps you in mastering web development through over a decade of publishing quality tutorials.

Comments to “PHP Request Methods”

Leave a Reply

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

↑ Back to Top

Share this page