Html GET 和 POST 方法之间的区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15170295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 06:02:44  来源:igfitidea点击:

Difference between GET and POST methods?

htmlformshttppostget

提问by Johny G

I'm new in this forum and I'm learning PHP from this night.

我是这个论坛的新手,我从今晚开始学习 PHP。

I want to send a form but I do not know the difference between:

我想发送表格,但我不知道两者之间的区别:

<form action="page2.php" method="GET">

and

<form action="page2.php" method="POST">

Anyone could help me please ?

任何人都可以帮助我吗?

Thanks.

谢谢。

回答by F__M

GET:

得到:

  • Parameters remain in browser history because they are part of the URL
  • Can be bookmarked.
  • GET method should not be used when sending passwords or other sensitive information.
  • 7607 character maximum size.
  • Url example: page2.php?category=sport
  • 参数保留在浏览器历史记录中,因为它们是 URL 的一部分
  • 可以加书签。
  • 发送密码或其他敏感信息时不应使用 GET 方法。
  • 7607 个字符的最大大小。
  • 网址示例:page2.php?category=sport

POST:

邮政:

  • Parameters are not saved in browser history.
  • Can not be bookmarked.
  • POST method used when sending passwords or other sensitive information.
  • 8 Mb max size for the POST method.
  • Url example: page2.php
  • 参数不会保存在浏览器历史记录中。
  • 不能加书签。
  • 发送密码或其他敏感信息时使用的 POST 方法。
  • POST 方法的最大大小为 8 Mb。
  • 网址示例:page2.php

回答by LAT

By convention HTTP GET is used for search forms while HTTP POST is used to change forms. A GET displays its information in the URL, publicly viewable, from which you can query the variables. A POST will not display its information. There is really no difference security wise.

按照惯例,HTTP GET 用于搜索表单,而 HTTP POST 用于更改表单。GET 在 URL 中显示其信息,可公开查看,您可以从中查询变量。POST 不会显示其信息。在安全方面真的没有区别。

回答by kaysush

If a GETrequest is used, the form parameters are encoded in the URL in what is called a query string.For example

如果使用GET请求,则表单参数以所谓的查询字符串编码在 URL 中。例如

www.someemailprovider.com/[email protected]&password=xxyz

www.someemailprovider.com/[email protected]&password=xxyz

A POSTrequest, unlike a GET request, passes the form parameters in the body of the HTTP request, not in the URL.

POST请求,不像GET请求,通过在HTTP请求的主体中的形式参数,而不是在URL中。

Moreover GETis idempotent and POSTis not that means If you call GET method on server nothing will be changed on server, but if you call POST then server will be changed may be a some additional data will be added in to the server, so GET is idempotent while POST is not.

此外GET是幂等的,POST并不意味着如果您在服务器上调用 GET 方法,则服务器上不会发生任何变化,但是如果您调用 POST,则服务器将被更改,可能是一些额外的数据将添加到服务器中,因此 GET 是幂等的而 POST 不是。

Note

笔记

The main thing to keep in mind as a programmer is that defining your form to use the GET method does not protect against causing changes. You could use a GET request to do pretty much the same thing as a POST query. It's just that browsers are generally coded to expect that POST requests will be used for things that will cause changes – like placing an order, or writing to a database, etc . GET requests should be used for pure queries that don't affect anything on the server. So, one should always remember not to use GET requests for any action that would cause a change on the server – like ordering a big screen tv.

作为程序员要记住的主要事情是定义表单以使用 GET 方法并不能防止引起更改。您可以使用 GET 请求执行与 POST 查询几乎相同的操作。只是浏览器通常被编码为期望 POST 请求将用于会导致更改的事情 - 例如下订单或写入数据库等。GET 请求应该用于不影响服务器上的任何内容的纯查询。因此,应始终记住不要将 GET 请求用于任何会导致服务器发生更改的操作——例如订购大屏幕电视。

回答by Josh Dean

These are both HTTP request methods, not PHP exclusive.

这些都是 HTTP 请求方法,不是 PHP 独有的。

$_GETis appended to end or URL. i.e. http://example.org/?foo=barAccess it in PHP with:

$_GET附加到结尾或 URL。即http://example.org/?foo=bar在 PHP 中访问它:

$foo = $_GET['foo'];

or $foo = $_REQUEST['foo'];

或 $foo = $_REQUEST['foo'];

GET is used for information you don't mind people seeing, and can be manually typed into links and urls to get results.

GET 用于您不介意人们看到的信息,并且可以手动输入链接和 url 以获得结果。

$_POSTis not visible in your URL, and generally is used after submitting a form. Access it in PHP with:

$_POST在您的 URL 中不可见,通常在提交表单后使用。在 PHP 中访问它:

$foo = $_POST['foo'];

or $foo = $_REQUEST['foo'];

或 $foo = $_REQUEST['foo'];

Read more about HTTP requests at http://www.w3schools.com/tags/ref_httpmethods.asp

http://www.w3schools.com/tags/ref_httpmethods.asp阅读有关 HTTP 请求的更多信息