How to PHP Scripting- GET & POST Methods

A web browser communicates with the server typically using one of the two HTTP methods: GET & POST Methods. A form data can be submitted using these two methods. Both are used for the same purpose but stands apart under some specifications. As in GET method, key values are passed in the Url while in POST, the information transfers in a hidden manner.

 

1.GET Method.

As explained above, before sending any information, it converts values/data into a query string in URL known as Url Encoding. Which contains both page link and encoded information separated by the ? character.

URL Encoding
      http://www.example.com/index.html?name=john&email=john@gmail.com

PHP provides the superglobal variable $_GET to access all the information sent either through the URL or submitted through an HTML form using the method=”get”.

GET Method
	<!DOCTYPE html>
	 <html lang="en">
	 <head>
		<title>Example of PHP GET method</title>
	 </head>
	 <body>
	 <?php
	  if(isset($_GET["name"] || $_GET["email"])){
		echo "Welcome: ". $_GET['name']. "<br />";
		echo "Your Email is: ". $_GET["email"]. "<br />";
	 }
	 ?>
	 <form method="get" action="<?php echo $_SERVER["PHP_SELF"];?>">
		<input type="text" name="name" placeholder="Your Name"></input><br/>
		<input type="text" name="email" placeholder="Your Email"></input><br/>
		<input type="submit" value="Submit">
	 </form>
	 </body>
	</html>

 

2.POST Method.

In POST method the data is sent to the server as a package in a separate communication with the processing script. Data sent through POST method will not visible in the URL.

POST Method
	<html lang="en">
	 <head>
		<title>Example of PHP POST method</title>
	 </head>
	 <body>
	  <?php
	   if(isset($_POST["name"] || $_POST["email"])){
		 echo "Welcome: ". $_POST['name']. "<br />";
		 echo "Your Email is: ". $_POST["email"]. "<br />";
	   }
	  ?>
	  <form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">
		...
	  </form>
	 </body>
	</html>

The $_REQUEST Variable: PHP provides another super global variable $_REQUEST that contains the values of both the $_GET and $_POST variables as well as the values of the $_COOKIE superglobal variable.

 

GET vs POST Comparison

 

FeaturesGETPOST
HistoryParameters remain in browser history because they are part of the URLParameters are not saved in browser history.
BookmarkedCan be bookmarked.Can not be bookmarked.
BACK button/re-submit behaviourGET requests are re-executed but may not be re-submitted to server if the HTML is stored in the browser cache.The browser usually alerts the user that data will need to be re-submitted.
Encoding type (enctype attribute)application/x-www-form-urlencodedmultipart/form-data or application/x-www-form-urlencoded Use multipart encoding for binary data.
Parameterscan send but the parameter data is limited to what we can stuff into the request line (URL). Safest to use less than 2K of parameters, some servers handle up to 64KCan send parameters, including uploading files, to the server.
HackedEasier to hack for script kiddiesMore difficult to hack
Restrictions on form data typeYes, only ASCII characters allowed.No restrictions. Binary data is also allowed.
SecurityGET is less secure compared to POST because data sent is part of the URL. So it's saved in browser history and server logs in plaintext.POST is a little safer than GET because the parameters are not stored in browser history or in web server logs.
Restrictions on form data lengthYes, since form data is in the URL and URL length is restricted. A safe URL length limit is often 2048 characters but varies by browser and web server.No restrictions
UsabilityGET method should not be used when sending passwords or other sensitive information.POST method used when sending passwords or other sensitive information.
VisibilityGET method is visible to everyone (it will be displayed in the browser's address bar) and has limits on the amount of information to send.POST method variables are not displayed in the URL.
CachedCan be cachedNot cached

Anwar Yakkiparamban

Anwar Yakkiparamban is the founder of Lauyou Learning. Prior to Lauyou learning, Anwar worked at ARD Engineering & Development, Qatar. He holds bachelor degree in Electronics and Communication Engineering from Govt. Engineering College Idukki.

You may also like...