GET & POST Methods


Both the GET and POST are known as HTTP Request Methods. First of all we have to know about HTTP.

What is HTTP?

HTTP  - Hypertext Transfer Protocol is used to communications between clients and servers.

HTTP works as a mediator (request-response) protocol between a client and server.

A web browser may be the client, and an application on a computer that hosts a web site may be the server.

Example: A client sends submits HTTP request to the server; then the server returns a response to the client. The response contains status information about the request and may also contain the requested content.

GET

Requests data from a specified resource

  • GET requests can be cached
  • GET requests remain in the browser history
  • GET requests can be bookmarked
  • GET requests should never be used when dealing with sensitive data
  • GET requests have length restrictions
  • GET requests should be used only to retrieve data

 

 

 

  • The GET method produces a long string that appears in your server logs, in the browser's Location: box.
  • The GET method is restricted to send upto 1024 characters only.
  • Never use GET method if you have password or other sensitive information to be sent to the server.
  • GET can't be used to send binary data, like images or word documents, to the server.
  • The data sent by GET method can be accessed using QUERY_STRING environment variable.
  • The PHP provides $_GET associative array to access all the sent information using GET method.

Example:

<?php

  if( $_GET["name"] || $_GET["age"] )

  {

     echo "Welcome ". $_GET['name']. "<br />";

     echo "You are ". $_GET['age']. " years old.";

     exit();

  }

?>

<html>

<body>

  <form action="<?php $_PHP_SELF ?>" method="GET">

  Name: <input type="text" name="name" />

  Age: <input type="text" name="age" />

  <input type="submit" />

  </form>

</body>

</html>

 

POST - Submits data to be processed to a specified resource

The POST transfers information via HTTP headers. The information is encoded as described in case of GET method and put into a header called QUERY_STRING.

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

  • There is not data size limit to be sent.
  • POST Method can be used to send ASCII and Binary data.
  • In POST method data goes thorough HTTP header means security depends on HTTP protocol.
  • By using Secure HTTP you can make sure that your data is secure.
  • There is no browser history of POST data.
  • You cannot bookmark the POST data.

Example:

<?php

  if( $_POST["name"] || $_POST["age"] )

  {

     echo "Welcome ". $_POST['name']. "<br />";

     echo "You are ". $_POST['age']. " years old.";

     exit();

  }

?>

<html>

<body>

  <form action="<?php $_PHP_SELF ?>" method="POST">

 

  Name: <input type="text" name="name" />

  Age: <input type="text" name="age" />

 

  <input type="submit" />

  </form>

</body>

</html>