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
- $_POST
- $_GET
- $_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
GET | POST | |
BACK button/Reload | No Loss | Post Data will resubmit |
Bookmarked | We can bookmark easily | Cannot |
Cached | Yes | No |
Encoding type | application/x-www-form-urlencoded | application/x-www-form-urlencoded or multipart/form-data. Use multipart encoding for binary data |
History | Parameters stay in url | Lost |
Restrictions on data length | Yes, maximum URL length is 2048 characters | No restrictions |
Restrictions on data type | Only ASCII characters allowed | No restrictions |
Security | Less | High |
Visibility | Yes, visible in url | No |
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 ?