PHP Get Post

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

  • 1. GET method
  • 2. POST method

GET Method

This method instructs the browser to send the encoded information (the name/value pairs) through the URL parameter by appending it to the page request. The browser implement this method by concatenating the question mark character (?) to the end of the page request since it specifies where the query string (name/values pairs) starts from, and all the form data is visible to everyone as it is displayed in the browser address bar.

  • GET requests can be cached
  • GET transfers the information through http head location & displays the data on URL address.
  • GET requests can be bookmarked
  • GET requests remain in the browser history
  • GET requests should never be used when dealing with sensitive data
  • GET requests have length restrictions
  • GET requests should be used only to retrieve data
  • GET can't upload the file.
  • GET is unsecured.

File: example_get_form.html

<form action="get_action_page.php" method="post">
 <div class="form-group">
   <label for="username">Username:</label>
   <input type="text" name="username" class="form-control" id="username" />
</div>
<div class="form-group">/span>
   <label for="pwd">Password:</label>
   <input type="password" name="password" class="form-control" id="pwd" />
</div>
   <button type="submit" class="btn btn-default">Submit</button>
</form>

File: get_action_page.php

<?php  
 echo "Username : " . $_GET['username']; 
 echo "Password : " . $_GET['password'];
?>

 

 

POST Method

This method sends information via HTTP header. All name/value pairs sent through this method is invisible to anyone else since all the information are embedded within the body of the HTTP request.

When you submit a form to a server through the POST method, PHP provides a superglobal variable called $_POST. The $_POST variable is used by PHP to create an associative array with an access key ($_POST['name as key']). The key is created automatically by PHP when the form is submitted. PHP uses the form field element name attribute (name="unique-name-here") to create the key.

  • POST requests are never cached
  • POST transfers the information through document body.
  • POST requests do not remain in the browser history
  • POST requests cannot be bookmarked
  • POST requests should be used when dealing with sensitive data
  • POST requests have no restrictions on data length
  • POST can upload the file.
  • POST is highly secured.

File: example_post_form.html

<form action="post_action_page.php" method="post">
 <div class="form-group">
   <label for="username">Username:</label>
   <input type="text" name="username" class="form-control" id="username" />
</div>
<div class="form-group">/span>
   <label for="pwd">Password:</label>
   <input type="password" name="password" class="form-control" id="pwd" />
</div>
   <button type="submit" class="btn btn-default">Submit</button>
</form>

File: post_action_page.php

<?php  
 echo "Username : " . $_POST['username']; 
 echo "Password : " . $_POST['password'];
?>