Php 简明教程
PHP - GET & POST
由于 PHP 主要用于 Web 应用程序开发,因此浏览器客户端发送的数据主要与 GET 和 POST 类型的 HTTP 请求方法有关。HTTP 协议还定义了向服务器发送请求的其他方法。除了 GET 和 POST 方法之外,还有 PUT、DELETE、HEAD 和 OPTIONS 方法。在本章中,我们主要研究 PHP 如何处理 GET 和 POST 方法。
Since PHP is mostly used for web application development, the data sent by the browser client is mainly with the GET and POST types of HTTP request methods. The HTTP protocol also defines other methods for sending the request to the server. They are PUT, DELETE, HEAD and OPTIONS (in addition to GET and POST methods). In this chapter, we shall concentrate on how PHP handles the GET and POST methods.
The GET Method
GET 方法发送编码的用户信息,并将其附加到页面请求。页面和编码信息由 ? 字符分隔。
The GET method sends the encoded user information appended to the page request. The page and the encoded information are separated by the ? character.
http://www.test.com/index.htm?name1=value1&name2=value2
-
The GET method produces a long string that appears in your server logs, in the browser’s Location: box.
-
The GET method is restricted to send upto 1024 characters only.
-
Never use GET method if you have password or other sensitive information to be sent to the server.
-
GET can’t be used to send binary data, like images or word documents, to the server.
-
The data sent by GET method can be accessed using QUERY_STRING environment variable.
-
The PHP provides $_GET associative array to access all the sent information using GET method.
通过将源代码放入 test.php 脚本中,尝试以下示例。
Try out following example by putting the source code in test.php script.
<?php
if( $_GET["name"] || $_GET["age"] ) {
echo "Welcome ". $_GET['name']. "<br />";
echo "You are ". $_GET['age']. " years old.";
exit();
}
?>
<form action = "<?php <b>$_PHP_SELF</b> ?>" method = "GET">
Name: <input type = "text" name = "name" />
Age: <input type = "text" name = "age" />
<input type = "submit" />
</form>
它将产生以下结果 −
It will produce the following result −
The POST Method
POST 方法通过 HTTP 标头传输信息。该信息已被编码,就像 GET 方法中所描述的那样,并放入名为 QUERY_STRING 的标头中。
The POST method transfers information via HTTP headers. The information is encoded as described in case of GET method and put into a header called QUERY_STRING.
-
The POST method does not have any restriction on data size to be sent.
-
The POST method can be used to send ASCII as well as binary data.
-
The data sent by POST method goes through HTTP header so security depends on HTTP protocol. By using Secure HTTP you can make sure that your information is secure.
-
The PHP provides $_POST associative array to access all the sent information using POST method.
通过将源代码放入 test.php 脚本中,尝试以下示例。
Try out following example by putting the source code in test.php script.
<?php
if( $_POST["name"] || $_POST["age"] ) {
if (preg_match("/[^A-Za-z'-]/",$_POST['name'] )) {
die ("invalid name and name should be alpha");
}
echo "Welcome ". $_POST['name']. "<br />";
echo "You are ". $_POST['age']. " years old.";
exit();
}
?>
<form action = "<?php <b>$_PHP_SELF</b> ?>" method = "POST">
Name: <input type = "text" name = "name" />
Age: <input type = "text" name = "age" />
<input type = "submit" />
</form>
它将产生以下结果 −
It will produce the following result −
Difference between GET and POST
GET 方法和 POST 方法之间的主要区别在于,虽然附加到 URL 的请求参数在浏览器的 URL 中公开出来,但 POST 数据包含在消息正文中,并且不会在 URL 中显示出来。因此,GET 方法不应用于将敏感数据发送到服务器。
The main difference between the GET and POST methods is that while the request parameters appended to the URL are exposed in the browser’s URL, the POST data is included in the message body, and not revealed in the URL. Hence, the GET method shouldn’t be used to send sensitive data to the server.
其次,GET 方法中的请求数据不能超过 2048 个字符,只能由 ASCII 字符组成,而使用 POST 方法时,对请求数据没有限制,也可以是二进制的(POST 数据的默认最大大小由 php.ini 文件中的 post_max_size 设置决定)
Secondly, the request data in GET method cannot exceed 2048 characters and can consist of ASCII characters only, while with POST method, there is no limit to the request data which can be in binary also (the default maximum size of POST data is determined by post_max_size setting in php.ini file)
PHP 提供了以下三个 superglobals 来检索和处理请求参数 −
PHP provides the following three superglobals to retrieve and process the request parameters −
-
$_GET − an associative array to access all the sent information using GET method.
-
$_POST − an associative array to access all the sent information using POST method.
-
$_REQUEST − an associative array that can be used to get the result from form data sent with both the GET and POST methods.
$_GET Array
你可以在查询字符串中以直接附加到 URL 的形式传递请求参数。
You can pass the request parameters in the form of query string directly appended to the URL.
将以下 PHP 脚本保存在文档根文件夹 ( htdocs ) 中,并将其命名为“hello.php” −
Save the following PHP script in the document root folder (htdocs) as "hello.php" −
<?php
echo "First name: " . $_REQUEST['first_name'] . " " .
"Last Name: " . $_REQUEST['last_name'] . "";
?>
在浏览器窗口中输入 http://localhost/hello.php?first_name=Amar&last_name=Sharma 作为 URL(确保 PHP 服务器正在运行)。
Enter http://localhost/hello.php?first_name=Amar&last_name=Sharma as the URL in a browser window (ensure that the PHP server is running).
$_GET 数组通过请求填充,输出显示如下 −
The $_GET array is populated from the request and the output is displayed as below −
First name: Amar Last Name: Sharma
如果 HTML 表单数据的方法属性为 GET,您也可以使用该数据填充 $_GET 数组。
You can also populate the $_GET array with the HTML form data if its method attribute is GET.
使用以下 HTML 表单收集数据并将其发送到“hello.php”。在文档根目录下,将以下脚本另存为“hello.html” −
Use the following HTML form to collect the data and send it to "hello.php". Under the document root, save the following script as "hello.html" −
<form action="hello.php" method="get">
First Name: <input type="text" name="first_name"/> <br/>
Last Name: <input type="text" name="last_name" />
<input type="submit" value="Submit" />
</form>
在浏览器中,输入 URL“http://localhost/hello.html” −
In your browser, enter the URL "http://localhost/hello.html" −
您应该会在浏览器窗口中获取类似 output 的内容。
You should get the similar output in the browser window.
$_POST Array
使用 POST 请求向服务器发送数据的最简单方法是将 HTML 表单的方法属性指定为 POST。假设浏览器中的 URL 为“http://localhost/hello.php”,那么在 HTML 表单“hello.html”中将 method=POST 设置为前面的示例 −
The easiest way to send data to a server with the POST request is specifying the method attribute of HTML form as POST. Assuming that the URL in the browser is "http://localhost/hello.php", method=POST is set in a HTML form "hello.html" as in the earlier example −
<form action="hello.php" method="post">
First Name: <input type="text" name="first_name"/> <br/>
Last Name: <input type="text" name="last_name" />
<input type="submit" value="Submit" />
</form>
“hello.php”脚本(位于文档根目录文件夹中)在 $_POST 数组中检索表单数据并将其作为 HTTP 响应呈现给浏览器 −
The "hello.php" script (in document root folder) retrieves the form data in the $_POST array and renders it as the HTTP response back to the browser −
<?php
echo "First name: " . $_POST['first_name'] . " " .
"Last Name: " . $_POST['last_name'] . "";
?>
在浏览器中打开 "http://localhost/hello.html" 。输入的数据将由服务器检索,然后像前面的示例一样呈现在客户端。
Open "http://localhost/hello.html" in your browser. The data entered is retrieved by the server, and rendered back to the client, as in the earlier example.