超文本传输协议 (HTTP) 旨在实现客户端和服务器之间的通信。它作为客户端和服务器之间的请求响应协议。两种最常见的 HTTP 方法是:
- GET 方法
- POST 方法
GET 方法
HTTP GET 方法用于从客户端向服务器发送信息。通过 GET 方法发送的信息对每个人都可见,因为所有变量名称和值都显示在带有页面的 URL 中,并且编码信息由 ? 分隔。字符(称为 QUERY_STRING)。每个变量名称和值对均以与号 (&) 分隔。
HTTP GET 方法仅限发送大约 2000 个字符。但是,由于变量显示在 URL 中,因此可以为页面添加书签。
使用 GET 方法发送的包含两个变量("variable1"和"variable2")的示例 URL 类似于:
http://www.test.com/index.htm?variable1=value1&variable2=value2
可以使用QUERY_STRING环境变量访问HTTP GET方法发送的数据。 PHP 提供了 $_GET 关联数组来访问使用 GET 方法发送的所有信息。
HTML 表单
考虑下面的示例,其中 test.php 包含以下代码。此页面会将 GET 变量("name"和"age")发送到操作脚本 welcome.php。
<html>
<body>
<form action="welcome.php" method="GET">
Name: <input type = "text" name = "name" />
Age: <input type = "text" name = "age" />
<input type = "submit" />
</form>
</body>
</html>
test.php 看起来类似于:

执行脚本
现在,将welcome.php视为执行脚本页面,其中包含以下代码:
<html>
<body>
<?php
echo "Hello ". $_GET['name']. "<br>";
echo "You are ". $_GET['age']. " years old.";
?>
</body>
</html>
结果
如果向test.php提供以下输入:

当点击"提交"按钮时,将会打开welcome.php脚本的 URL 类似于(注意查询字符串):
http://www.test.com/welcome.php?name=John&age=25
URL 包含以下结果:

示例:GET 方法示例
下面的示例演示了上述概念.
<html>
<body>
<form action="welcome.php" method="GET">
Name: <input type = "text" name = "name" />
Age: <input type = "text" name = "age" />
<input type = "submit" />
</form>
</body>
</html>
POST 方法
HTTP POST 方法用于通过 HTTP 标头从客户端和服务器发送信息。使用 POST 方法发送的信息对其他人不可见。 POST 方法发送的数据经过 HTTP header,因此安全性依赖于 HTTP 协议。可以使用安全HTTP来确保信息的安全。
HTTP POST方法对发送的信息量没有限制。但是,由于变量未显示在 URL 中,因此无法为页面添加书签。
PHP 提供 $_POST 关联数组来访问所有变量使用 HTTP POST 方法发送的信息。
HTML 表单
考虑下面的示例,其中 test.php 包含以下代码。此页面会将 POST 变量("name"和"age")发送到操作脚本 greeting.php。
<html>
<body>
<form action="greeting.php" method="POST">
Name: <input type = "text" name = "name" />
Age: <input type = "text" name = "age" />
<input type = "submit" />
</form>
</body>
</html>
test.php 看起来类似于:

动作脚本
现在,将greeting.php视为包含以下代码的动作脚本页面:
<html>
<body>
<?php
echo "Hello ". $_POST['name']. "<br>";
echo "You are ". $_POST['age']. " years old.";
?>
</body>
</html>
结果
如果向test.php提供以下输入:

当点击"提交"按钮时,将会打开greeting.php结果如下:

示例:POST 方法示例
下面的示例演示了上述概念。
<html>
<body>
<form action="greeting.php" method="POST">
Name: <input type = "text" name = "name" />
Age: <input type = "text" name = "age" />
<input type = "submit" />
</form>
</body>
</html>
$_REQUEST 变量
PHP $_REQUEST 是一个内置的超级全局变量,在所有作用域中始终可用。 $_REQUEST 位于关联数组中,默认情况下它包含 $_GET、$_POST 和 $_COOKIE 的内容。我们将在后面的部分中讨论 cookie 变量。
HTML 表单
考虑下面的示例,其中 test.php 包含以下代码。此页面会将 POST 变量("name"和"age")发送到操作脚本 greetings.php。
<html>
<body>
<form action="greetings.php" method="POST">
Name: <input type = "text" name = "name" />
Age: <input type = "text" name = "age" />
<input type = "submit" />
</form>
</body>
</html>
test.php 看起来类似于:

动作脚本
现在,将greetings.php视为包含以下代码的动作脚本页面:
<html>
<body>
<?php
echo "Hello ". $_REQUEST['name']. "<br>";
echo "You are ". $_REQUEST['age']. " years old.";
?>
</body>
</html>
结果
如果向test.php提供以下输入:

当点击"提交"按钮时,将会打开greetings.php结果如下:

示例:$_REQUEST 示例
下面的示例演示了上述概念。
<html>
<body>
<form action="greetings.php" method="POST">
Name: <input type = "text" name = "name" />
Age: <input type = "text" name = "age" />
<input type = "submit" />
</form>
</body>
</html>