Wednesday, 13 December 2017

forms - When should I use GET or POST method? What's the difference between them?

itemprop="text">

What's the difference when using
GET or POST method? Which one is more
secure? What are (dis)advantages of each of
them?




( href="https://stackoverflow.com/questions/46585/when-do-you-use-post-and-when-do-you-use-get">similar
question)


itemprop="text">
class="normal">Answer



It's not a
matter of security. The HTTP protocol defines GET-type requests as being href="https://stackoverflow.com/questions/1077412/what-is-an-idempotent-operation">idempotent,
while POSTs may have side effects. In plain English, that means that GET is used for
viewing something, without changing it, while POST is used for changing something. For
example, a search page should use GET, while a form that changes your password should
use POST.



Also, note that PHP confuses the
concepts a bit. A POST request gets input from the query string and through the request
body. A GET request just gets input from the query string. So a POST request is a
superset of a GET request; you can use $_GET in a POST request,
and it may even make sense to have parameters with the same name in
$_POST and $_GET that mean different
things.



For example, let's say you have a form
for editing an article. The article-id may be in the query string (and, so, available
through $_GET['id']), but let's say that you want to change the
article-id. The new id may then be present in the request body
($_POST['id']). OK, perhaps that's not the best example, but I
hope it illustrates the difference between the two.



No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...