Contact Information

KodeBlog

We're Available 24/ 7. Drop us an email.

PHP - GET & POST

A web browser client uses two methods to send information to the web server. By using the GET method or POST method. In the process of information being sent from Client to Web Server, the information is first encoded as URL schema which includes the information such as name, types, etc. with equal sign and different pairs are separated by the ampersand sign.

For example

name=fred&faculty=science&age=35

Topics to be covered

We will cover the following topics in this lesson

  • URL Encoding
  • GET Method
  • POST Method
  • Summary

URL Encoding

In the URL encoding scheme there is no room for a space character so it is replaced with the Plus + character and any other non-alphanumeric characters are also replaced with a hexadecimal representative values. Once this is all completed, then the information finally sent to web server.

All of the URL passed through GET Method are easily stored in the Logs of web server at the other end and can be read by web admins (this can be for a debugging purpose or monitoring)

GET Method

With the GET method, web browser client sends the information in encoding appended to the page request. The page and the encoded information are separated by the question mark ? character.

For Example

https://www.example.com/index.php?label1=value1&lable2=value2

 

Characteristics of GET Method

  • It generates a long string of URL that will be displayed in the web server's Log files and in the client web browser's Address bar
  • This method is restricted and limited to send up to 1024 characters only
  • This method will send username and password strings nakedly so it is strongly advised to not to use get string for such and the likes of such sensitive information
  • GET cannot send any binary data format, such as images or Microsoft Word documents to the server
  • The data that is sent via the GET method can be easily access via QUERY_STRING environment variable
  • With PHP we can use an associative array called $_GET in order to access all the information sent by GET method utilization

Example of GET Method

<?php 

if( $_GET["city"] || $_GET["country"] ) {
    echo "This city of ". $_GET['city']. "<br />";
    echo "Is in the country of ". $_GET['country'];

    exit();
}

?>

<html>
    <head>
        <title>PHP Forms GET</title>
    </head>
    <body>
        <form action = "<?php $_PHP_SELF ?>" method = "GET">
            City: <input type = "text" name = "city" />
            Country: <input type = "text" name = "country" />
            <input type = "submit" />
        </form>
    </body>
</html>

HERE,

  • if( $_GET["city"] || $_GET["country"] ) {...} checks to see if the URL contains query strings city or country. If yes, then the values are displayed.
  • exit(); exits the function if the user entered something into the text boxes.
  • <form action = "<?php $_PHP_SELF ?>" method = "GET"> defines a form that uses the GET method and the action uses $_PHP_SELF which points to the same file.

The result of above code is divided in to two segments, first segment asks for user input and the second segment returns the gathered information alongside PHP snippet rendered.

Segment 1

PHP Forms GET

(Segment 1 is asking for user's input so let's assume that user entered New York in city and America in country)

Segment 2

PHP GET Global Variable

And the encoded URL in result of using the GET method will look like this at

Segment 2

http://localhost:8000/get.php?city=Arusha&country=Tanzania

POST Method

POST method is considered secure and most used method nowadays because In this method the information is not passed through the URL but it uses HTTP headers to send information. The information is encoded similarly as it gets in GET but sent via using the header called QUERY_STRING.

Characteristics of POST Method

  • Unlike GET method, with POST method there is no restriction on data length to be sent
  • POST method can send data in both ASCII and in binary format. Which means both the text information and images, documents and executable files can be sent through POST method
  • The data sent through the POST method goes packed inside an HTTP header so the security basically is independent of how HTTP protocol is dealing it. If you're using SSL over HTTP (HTTPS) then the data is well encrypted and near-unbreakable to anyone in the world
  • We use the $_POST associative array whenever we need to access all the information sent using the POST method

Example of POST Method

The following example uses POST method

<?php

if( $_POST["city"] || $_POST["country"] ) {
    echo "The City of ". $_POST['city']. "<br />";
    echo "Is in the country of ". $_POST['country'];
    
    exit();
}

?>

<html>
    <body>
        <form action = "<?php $_PHP_SELF ?>" method = "POST">
        City: <input type = "text" name = "city" />
        Country: <input type = "text" name = "country" />
        <input type = "submit" />
    </form>
    </body>
</html>

HERE,

  • if( $_POST["city"] || $_POST["country"] ) {...} checks for the definition city or country key(s) in the array variable $_POST.

As with the GET Method, POST Method also break down output in to two segments. First segment asks for user input and the second segment returns the gathered information alongside PHP snippet rendered.

Segment 1

PHP Forms GET

(Segment 1 is asking for user's input so let's assume that user entered Lusaka in city and Zambia in country)

Segment 2

PHP Post Global Variable

Unlike with the GET Method, POST Method will return a clean URL even after segment 2 of the result

http://localhost:8000/post.php (at segment 1)

http://localhost:8000/post.php (after segment 2, no change no data sent via URL)

Summary

Web browsers primarily support two HTTP verbs out of the box namely GET and POST. Get is useful for such things as searching or filtering records. Values submitted via GET verb are visible in the web browser URL. POST method is usually used to create new resources on the server. It is also used to authenticate users. Values submitted via POST method are not visible in the URL. On the backend, GET method data is accessible via $_GET array variable while POST method data is accessible via $_POST array variable.

SUPPORT US

If you found this tutorial useful, support us by using the social media buttons to like and share the tutorial. If you didn’t find it useful, please use the comments section below to let us know how we can do better next time.

Subscribe to our newsletter, like our Facebook fan page or follow us on Twitter to get free updates when we publish new tutorials

Author: Rodrick Kazembe

Rodrick is a developer who works on Desktop, Web and Mobile Applications. He is familiar with Python, Java, JavaScript, C++, C#, Kotlin, PHP, Python and the list goes on. Rodrick enjoys sharing knowledge especially when it comes to technology.

PHP - Loop Types

Leave A Reply