Spread the love

PHP gives there is 3 types of request methods to by which client can send information to server. In this article we will learn difference in get vs post and also in request. So, Methods are as follow

  1. $_POST
  2. $_GET
  3. $_REQUEST

1. $_POST Request Method

$_POST is used to fetch the post request data from the client end.

Example of client and server request in php

<form action="/action_page.php" method="post">
  <label for="fname">First name:</label>
  <input type="text" id="fname" name="fname"><br><br>
   
  <input type="submit" value="Submit">
</form>

File: index.php

<?php 
$first_name = $_POST['fname'];

File: action_page.php

2. $_GET Request Method

$_GET is used to fetch the query params from URL and form.

<form action="/action_page.php" method="get"> // here method is get
  <label for="fname">First name:</label>
  <input type="text" id="fname" name="fname"><br><br>
   
  <input type="submit" value="Submit">
</form>

we can also use it in URL

https://your_url.com/index.php?post=115&action=edit

In php

<?php 

$first_name = $_GET['fname'];  //form index.php
//url
$post= $_GET["post"];

$action=$GTE["action"];

File: action_page.php

3. $_REQUEST Method

$_REQUEST is combination of both $_GET and $_POST. so we can fetch both type of request using $_REQUEST.

<?php 

$first_name = $_REQUEST['fname'];  //form index.php

//url
$post= $_REQUEST["post"];

$action=$_REQUEST["action"];

Difference between GET vs POST

Here is table for get vs post

GETPOST
BACK button/ReloadNo LossPost Data will resubmit
BookmarkedWe can bookmark easilyCannot
CachedYesNo
Encoding typeapplication/x-www-form-urlencodedapplication/x-www-form-urlencoded or multipart/form-data. Use multipart encoding for binary data
HistoryParameters stay in urlLost
Restrictions on data lengthYes, maximum URL length is 2048 charactersNo restrictions
Restrictions on data typeOnly ASCII characters allowedNo restrictions
SecurityLessHigh
VisibilityYes, visible in urlNo
Difference between get and post

Difference between $_GET and $_REQUEST

As i mentioned above $_REQUEST is combination of both post and get. Hence, its only exist in PHP and can be used as both get and posy methods.

Also Read : How to connect PHP to MySQL ?

Leave a Reply