什么时候应该使用GET或POST方法?他们之间有什么区别? [英] When should I use GET or POST method? What's the difference between them?

查看:196
本文介绍了什么时候应该使用GET或POST方法?他们之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 GET POST 方法有什么区别?哪一个更安全?他们每个人的优点是什么?



类似的问题

解决方案

不是安全问题。 HTTP协议将GET类型的请求定义为 idempotent ,而POST可能有副作用。用简单的英语,这意味着GET用于查看某些内容,而不用更改它,而POST用于更改某些内容。例如,搜索页面应该使用GET,而更改密码的表单应该使用POST。

另外,请注意,PHP将这些概念混淆了一下。 POST请求从查询字符串和请求主体获取输入。 GET请求只是从查询字符串获取输入。所以POST请求是GET请求的超集;你可以在POST请求中使用 $ _ GET ,甚至可以在 $ _ POST $ _ GET 表示不同的东西。

例如,假设您有一个用于编辑文章。 article-id可能位于查询字符串中(因此,可通过 $ _ GET ['id'] )获得,但假设您要更改文章 - ID。新的id可能会出现在请求正文中( $ _ POST ['id'] )。好的,也许这不是最好的例子,但我希望它能说明两者之间的区别。


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

(similar question)

解决方案

It's not a matter of security. The HTTP protocol defines GET-type requests as being 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.

这篇关于什么时候应该使用GET或POST方法?他们之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆