About the difference between GET and POST
nice to meet you! My name is Hase, and I joined the development team as a new graduate this year.
This time, I would like to explain the difference between "GET" and "POST", which are used when passing information in PHP, for beginners to programming.
What are GET and POST?
This refers to the method used when passing data such as input forms to a web server.
Features of GET
- Add data after the URL and send.
- Since the data is written in the URL, the data entered by others will be completely visible.
- URLs have a limited number of characters (the maximum number of characters that can be used in Internet Explorer URLs is 2,048), which limits the amount of data that can be sent.
- Only text data can be sent (binary data such as image data cannot be sent as it cannot be written in the URL).
Example: http://localhost/sample/confirm.php?name=%E5%B1%B1%E7%94%B0%E5%A4%AA%E9%83%8E&age=22
"?" is the beginning of a parameter.
"&" is a parameter separator.
The left side of "=" is the GET variable name, and the right side is the value to be passed.
The value passed is %E5%B1%B1%E7... because Japanese is converted. (Japanese cannot be used in URLs)
Features of POST
- Data is not appended to the URL.
- Since no data is written in the URL, the data cannot be viewed by others.
- Can be sent without any limit on the amount of data.
- Both text and binary can be sent.
Example: http://localhost/sample/confirm.php
How to use GET and POST
It is best to use POST if the following apply to you
If you have a large amount of data
As mentioned above, the maximum number of characters that can be used in a URL in Internet Explorer is 2,048 characters, and
in the case of GET, the data is added after the URL and sent, which limits the amount of data that can be sent.
Therefore, if the amount of data is large, please use POST.
When sending confidential information
If there is information in the data that you do not want outsiders to know, such as an email address or password,
if you use GET, the data information will be added after the URL, making it visible to others.
Therefore, please use POST when sending confidential information.
When sending binary data
Please use POST when sending binary data such as images.
At the end
If you want to share the data via URL, and you don't mind others seeing the data, you can use GET.
However, if your form contains personal information, such as a personal information input form or product order form, be sure to use POST.
That's it.