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

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

问题描述

使用 GETPOST 方法有什么区别?哪个更安全?它们各自的(缺点)优势是什么?

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

(类似问题)

推荐答案

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

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.

另外,请注意 PHP 有点混淆了这些概念.POST 请求从查询字符串和请求正文中获取输入.GET 请求只是从查询字符串中获取输入.所以 POST 请求是 GET 请求的超集;您可以在 POST 请求中使用 $_GET,甚至在 $_POST$_GET 中使用同名参数可能更有意义这意味着不同的事情.

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.

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

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天全站免登陆