We now have a youtube channel. Subscribe!

GET and POST Methods in PHP

PHP GET and POST Methods


Hello folks! welcome back to a new section of our tutorial on PHP. In this tutorial guide, we will be studying about the PHP GET and POST Methods.

There are two ways the browser can send information to the web server. They are -

  • The GET Method
  • The POST Method

Before the browser sends the information, it encodes it using a scheme that's called URL encoding. In this scheme, name/value pairs are conjoined with equal signs and different pairs are separated by the ampersand sign.

name1=value1&name2=value2&name3=value3

Spaces are removed and replaced with the + character and any other nonalphanumeric characters are replaced with a hexadecimal value.


PHP GET Method

The GET Method

The GET method sends the encoded user information appended to the page request. The page and the encoded information are separated by ? character.

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

  • GET method produces a lengthy string that appears in your server logs, in the browser's Location: box.
  • GET method is restricted to send up to 1024 characters only.
  • Never use the GET method if you have any vital information like password to be sent to the server.
  • The GET method can not be used to send binary data like images or word documents, to the server.
  • The information sent by GET method can be accessed via QUERY_STRING environment variables.
  • PHP supplies the $_GET associative array to access all sent informations using the GET method.

Example

Try the following example below by putting the source code into a test.php script.

<?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>

Output

When the above code is executed, it will produce the following result -



PHP POST Method

The POST Method

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

  • The POST method does not have any restriction on data size to be sent.
  • The PHP POST method can be used to send ASCII and also binary data.
  • The data sent by POST goes through HTTP header so security depends on the HTTP protocol. By making use of secure HTTP, you can make sure that your informations are secured.
  • PHP gives $_POST associative array to access all sent information using POST method.

Example

Try the following example below by putting the source code in a test.php script -

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

Output

When the above code is executed, it will produce the following result -



PHP Superglobal Variables

The $_REQUEST Variable

The $_REQUEST variable contains both the contents of $_GET, $_POST, and $_COOKIE. We will discuss about $_COOKIE variable in subsequent tutorials.

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

Example

Try the following example below by putting the source code into a test.php script -

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

Here, $_PHP_SELF variable holds the name of self script in which it is being called.

Output

When the above code is executed, it will produce the following result -



Alright guys! This is where we are rounding up for this tutorial post. In our next tutorial post, we will be discussing about the PHP File Inclusion.

Feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.
© 2022 ‧ WebDesignTutorialz. All rights reserved. Developed by Jago Desain