PHP Tutorial PHP Advanced PHP References

PHP - GET & POST Methods



The Hypertext Transfer Protocol (HTTP) is designed to enable communications between clients and servers. It works as a request-response protocol between a client and server. The two most common HTTP methods are:

  • The GET method
  • The POST method

The GET Method

The HTTP GET method is used to send information from client to server. Information sent via the GET method is visible to everyone because all variable names and values are displayed in the URL with page and the encoded information are separated by the ? character (known as QUERY_STRING). Each variable name and value pair is separated by ampersand (&) sign.

The HTTP GET method is restricted to send about 2000 characters. However, because the variables are displayed in the URL, it is possible to bookmark the page.

An example URL with two variables ("variable1" and "variable2") sent using GET method will be similar to:

http://www.test.com/index.htm?variable1=value1&variable2=value2

The data sent by HTTP GET method can be accessed using QUERY_STRING environment variable. The PHP provides $_GET associative array to access all the sent information using the GET method.

The HTML form

Consider the example below where test.php contains following code. This page will send the GET variables ("name" and "age") to the action script, welcome.php.

<html>
<body>
   
  <form action="welcome.php" method="GET">
    Name: <input type = "text" name = "name" />
    Age: <input type = "text" name = "age" />
    <input type = "submit" />
  </form>
      
</body>
</html>

The test.php will look similar to:

The HTML form

The action script

Now, consider the welcome.php as the action script page which contains the following code:

<html>
<body>
   
  <?php
    echo "Hello ". $_GET['name']. "<br>";
    echo "You are ". $_GET['age']. " years old.";
  ?>
      
</body>
</html>

Result

If the following input is provided to the test.php:

The HTML form

When "submit" button is clicked, it will open the welcome.php script with the URL similar to (note the query string):

http://www.test.com/welcome.php?name=John&age=25

The URL contains the following result:

The HTML form

Example: GET method example

The example below demonstrates the above discussed concept.

<html>
<body>
   
  <form action="welcome.php" method="GET">
    Name: <input type = "text" name = "name" />
    Age: <input type = "text" name = "age" />
    <input type = "submit" />
  </form>
      
</body>
</html>

Note: The HTTP GET method is used for sending non-sensitive data and it should NEVER be used for sending sensitive information.

Note: The HTTP GET method can not be used to send binary data, like images or word documents, to the server.

The POST Method

The HTTP POST method is used to send information from clients and servers via HTTP headers. Information sent with the POST method is invisible to others. The data sent by POST method goes through HTTP header therefore the security depends on HTTP protocol. The secure HTTP can be used to make sure that the information is secure.

The HTTP POST method has no limits on the amount of information to send. However, because the variables are not displayed in the URL, it is not possible to bookmark the page.

The PHP provides $_POST associative array to access all the sent information using HTTP POST method.

The HTML form

Consider the example below where test.php contains following code. This page will send the POST variables ("name" and "age") to the action script, greeting.php.

<html>
<body>
   
  <form action="greeting.php" method="POST">
    Name: <input type = "text" name = "name" />
    Age: <input type = "text" name = "age" />
    <input type = "submit" />
  </form>
      
</body>
</html>

The test.php will look similar to:

The HTML form

The action script

Now, consider the greeting.php as the action script page which contains the following code:

<html>
<body>
   
  <?php
    echo "Hello ". $_POST['name']. "<br>";
    echo "You are ". $_POST['age']. " years old.";
  ?>
      
</body>
</html>

Result

If the following input is provided to the test.php:

The HTML form

When "submit" button is clicked, it will open the greeting.php with the following result:

The HTML form

Example: POST method example

The example below demonstrates the above discussed concept.

<html>
<body>
   
  <form action="greeting.php" method="POST">
    Name: <input type = "text" name = "name" />
    Age: <input type = "text" name = "age" />
    <input type = "submit" />
  </form>
      
</body>
</html>

Note: The HTTP POST method can be used to send ASCII as well as binary data.

The $_REQUEST Variable

The PHP $_REQUEST is a built-in super global variable which is always available in all scopes. The $_REQUEST in an associative array and by default it contains the contents of $_GET, $_POST and $_COOKIE. We will discuss about cookie variable in later section.

The HTML form

Consider the example below where test.php contains following code. This page will send the POST variables ("name" and "age") to the action script, greetings.php.

<html>
<body>
   
  <form action="greetings.php" method="POST">
    Name: <input type = "text" name = "name" />
    Age: <input type = "text" name = "age" />
    <input type = "submit" />
  </form>
      
</body>
</html>

The test.php will look similar to:

The HTML form

The action script

Now, consider the greetings.php as the action script page which contains the following code:

<html>
<body>
   
  <?php
    echo "Hello ". $_REQUEST['name']. "<br>";
    echo "You are ". $_REQUEST['age']. " years old.";
  ?>
      
</body>
</html>

Result

If the following input is provided to the test.php:

The HTML form

When "submit" button is clicked, it will open the greetings.php with the following result:

The HTML form

Example: $_REQUEST example

The example below demonstrates the above discussed concept.

<html>
<body>
   
  <form action="greetings.php" method="POST">
    Name: <input type = "text" name = "name" />
    Age: <input type = "text" name = "age" />
    <input type = "submit" />
  </form>
      
</body>
</html>

5