PHP GET & POST methods

Admin   PHP   199  2020-10-19 19:02:01

There are two ways for the client to send information to Web Server:

GET method
POST method

Before the browser sends the information, it encodes it using a schema called URL encoding. In this schema, the name / value pairs associated with the equal sign and the different pairs are separated by slashes & .

name1=value1&name2=value2&name3=value3


Spaces are removed, replaced with a + character, and any non-numeric and literal characters are replaced with hexa values. After the information is encrypted, it is sent to the server.

php get post methods

PHP GET method

The GET method sends the encrypted user information appended to the page request. Pages and encrypted information are separated by a dot ( ? ) Character .

http://www.test.com/index.htm?name1=value1&name2=value2


The GET method creates a long string of characters to appear in your Server log, in the Location: box of the browser.

The GET method is limited to sending up to only 1024 characters.

Never use the GET method if sending passwords or sensitive information to the server.

GET cannot send binary data, such as images or word documents, to the server.

The data sent by the GET method can be accessed using the QUERY_STRING environment variable.

PHP provides the $_GET associative array to access all the information sent by the GET method.

Try the following example by placing the following source code in the test.php script:

<?php
   if ($_GET ["name"] || $_GET ["age"])
   {
      echo "Welcome". $_GET ['name']. "<br />";
      echo "You". $_GET ['age']. " year old.";
      
      exit();
   }
?>
<html>
   <body>
   
      <form action="<?php $_PHP_SELF?>" method ="GET">
         Full name: <input type="text" name="name" />
         Age: <input type="text" name="age" />
         <input type="submit" />
      </form>
      
   </body>
</html>


The above PHP code will produce:

POST method in PHP

The POST method passes information through the HTTP header. This information is encoded as described in the case of the GET method and placed in a header called QUERY_STRING.

The POST method does not have any restriction on the size of the data to be sent.

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

The data sent by the POST method is via an HTTP header, so security is dependent on the HTTP protocol. By using Secure HTTP you can be sure your information is secure.

PHP provides $_POST associative array to access all information sent by POST method.

Try the following example by placing the source code in the test.php script:

<?php
   if ($_POST["name"] || $_POST["age"])
   {
      if (preg_match("/[^ A-Za-z '-] /", $_ POST['name']))
      {
         die ("Invalid name variable - should be letters");
      }
      echo "Welcome". $_POST ['name']. "<br />";
      echo "You". $_POST ['age']. " year old.";
      
      exit();
   }
?>

 

<html>
   <body>
   
      <form action="<?php $_PHP_SELF?>" method ="POST">
         Full name: <input type="text" name="name" />
         Age: <input type="text" name="age" />
         <input type="submit" />
      </form>
   
   </body>
</html>


The above PHP code will produce:


The variable $_REQUEST in PHP

The variable $_REQUEST in PHP contains the contents of both $_GET, $_POST, and $_COOKIE. We will talk about the variable $_COOKIE when explaining Cookie in PHP.

The $_REQUEST variable in PHP can be used to get the result from the form data sent by both GET and POST methods.

Try the following example by placing the source code in the test.php script:

<?php
   if ($_REQUEST["name"] || $_REQUEST["age"])
   {
      echo "Welcome". $_REQUEST ['name']. "<br />";
      echo "You". $_REQUEST ['age']. " year old.";
      exit();
   }
?>

 

<html>
   <body>
      
      <form action="<?php $_PHP_SELF?>" method="POST">
         Full name: <input type="text" name="name" />
         Age: <input type="text" name="age" />
         <input type="submit" />
      </form>
      
   </body>
</html>


Here, the variable $_PHP_SELF contains the name of the script itself, the script it is being called on.