Friday, September 25, 2020

GET and POST Methods in PHP



Hello, dear readers! welcome back to another section of my tutorial on PHP. In this tutorial guide, we will be discussing about the PHP GET and POST Methods.

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

  • The GET Method
  • The POST Method

Before the web browser sends the information, it encodes it by using a scheme that is called URL encoding. In this scheme, name and value pairs are joined with equal signs and different pairs are separated by the sign known as ampersand.

name1=value1&name2=value2&name3=value3

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

RECOMMENDED: A Guide to PHP Arrays


The GET Method

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

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

  • The GET method produces a long string that appears in your server logs, in the web browser's Location: box.
  • GET method is restricted to send up to 1024 characters only.
  • Never make use of 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.
  • The 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 -


RECOMMENDED: PHP Decision Making


The POST Method

The POST method sends data via the HTTP headers. The data that is being transferred is encoded as described in the case of the GET method and then is placed 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 PHP POST goes through HTTP header so security depends on the HTTP protocol. By using the secure HTTP, you can make sure that your informations are secured.
  • The PHP provides $_POST associative array to access all the sent information that are 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 -


RECOMMENDED: Strings in PHP


The $_REQUEST Variable

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

The $_REQUEST variable can be used to get the result from form data sent with 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 my next tutorial post, we will be discussing about the PHP File Inclusion.

Feel free to ask your questions where necessary and i 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.
Share:

0 comments:

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.