There are two ways a browser client can send information to a web server.

  • The GET Method
  • The POST Method

The GET Method

The GET method sends the user information included in the page request. Page and information included separated by? mark.

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

  • GET method is restricted to sending up to 1024 characters.
  • Never use the GET method if you have a password or other sensitive information that will be sent to the server.
  • GET cannot be used to send binary data, such as photos or text, to a server.
  • Data submitted via GET method can be accessed using QUERY_STRING's natural variables.

Try out following example

<?php
   if( $_GET["name"] || $_GET["age"] ) {
      echo "Welcome ". $_GET['name']. "";
      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>



When you submit the form url will show name and age like this

http://localhost/tutorial/?name=rathorji&age=25

Output:




The POST Method

The POST method transmits information about HTTP headers. The information is encrypted as defined in the GET method and then entered in a header called QUERY_STRING.

  • POST method has no restrictions on the size of the data to be sent.
  • The POST method can be used to export ASCII and binary data.
  • The information sent through the POST method passes through the HTTP header and therefore security depends on the HTTP protocol. By using Secure HTTP you can ensure that your information is secure.
  • PHP provides the sameĀ  $ _POST members to access all the information sent using the POST method.

Try out following example by putting the source code in test.php script.

#test.php

<?php
if (isset($_POST["name"]) && isset($_POST["age"])) {
    if (preg_match("/[^A-Za-z'-]/", $_POST['name'])) {
        die("invalid name and name should be alpha");
    }
    echo "Welcome " . $_POST['name'] . "";
    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>

output:

Welcome rathorji
You are 25 years old.

The $_REQUEST variable

The PHP $ _REQUEST variable can be used to get results from form data submitted via both GET and POST methods.

Try the following example by entering the source code in the example.php script.

#example.php

<?php
if (isset($_REQUEST["name"]) && isset($_REQUEST["age"])) {
    echo "Welcome " . $_REQUEST['name'] . "
"
; echo "You are " . $_REQUEST['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>