Purpose
In the HTTP protocol, there are two ways to send data: GET and POST.
Let's explore their differences and advantages and disadvantages.
Components of HTTP Request
[Scheme]://[Host]:[Port][Path]?[Query]#[Fragment]
[Header]
Example of HTTP URI Components
http://www.example.com:1030/software?id=test#section-4
Scheme | Host | Port | Path | Query | Fragment |
---|---|---|---|---|---|
http | ://http://www.example.com/ | :1030 | /software | ?id=test | #section-4 |
Sending Data with GET
This method sends data by declaring parameters in the query section of the HTTP URI.
Example of GET
http://www.example.com?id=test
Advantages
- Can be cached in the URI, allowing for bookmarks, backtracking, etc.
- Faster than POST
- Parameter values are visible, so contents can be guessed.
Disadvantages
- Limited length for the URI
- Parameter values are visible, so contents can be guessed, making it less secure.
PHP Code to Receive Data Sent with GET
<?php
$data = $_GET['a'];
echo "$data";
?>
Sending Data with POST
This method sends data by declaring parameters in the header section of the HTTP request.
Example of POST
$name=Han is the parameter and data being sent.
POST /login HTTP/1.1
Host: www.example.com
Content-Length: 255
Cache-Control: max-age=242342
Upgrade-Insecure-Requests: 134423423
Origin: <http://www.example.com>
Content-Type: application/x-www-form-urlencoded
$name=Han
Advantages
- No length limit
- Parameter values are not visible, so it is more secure.
Disadvantages
- Cannot be cached, making it impossible to bookmark or backtrack.
PHP Code to Receive Data Sent with POST
<?php
$data = $_POST['a'];
echo "$data";
?>
댓글 없음:
댓글 쓰기