PHP 中 $_REQUEST 是一个超全局变量。它是一个关联数组,它是 $_GET、$_POST 和 $_COOKIE 变量内容的集合。
- “php.ini” 文件中的设置决定了此变量的组成。
- “php.ini” 中的指令之一是 request_order,它决定了 PHP 注册 GET、POST 和 COOKIE 变量的顺序。
- 此数组列出的变量存在和顺序是根据 PHP variables_order 定义的。
- 如果从命令行运行 PHP 脚本,则 argc 和 argv 变量不包含在 $_REQUST 数组中,因为它们的值取自 $_SERVER 数组,而 $ 数组又由 Web 服务器填充。
使用 GET 方法的 $_REQUEST
将以下脚本保存在 Apache 服务器的 document 文件夹中。如果您在 Windows 上使用 XAMPP 服务器,请将脚本作为 “index.php” 放在 “c:/xampp/htdocs” 文件夹中。
<html>
<body>
<?php
echo "<h3>姓: " . $_REQUEST['first_name'] . "<br />"
. "名: " . $_REQUEST['last_name'] . "</h3>";
?>
</body>
</html>
启动 XAMPP 服务器并在浏览器窗口中输入 http://localhost/index.php?first_name=罗&last_name=明光 作为 URL。
您应该将输出为 −
使用 POST 方法的 $_REQUEST
在文档根目录下,将以下脚本另存为 “index.html”。
<html>
<body>
<form action="hello.php" method="post">
姓: <input type="text" name="first_name" /> <br />
名: <input type="text" name="last_name" />
<input type="submit" value="提交" />
</form>
</body>
</html>
在浏览器中,输入 URL “http://localhost/index.html”。您应该在浏览器窗口中获得类似的输出。
您还可以将 PHP 代码嵌入到 HTML 脚本中,并使用 PHP_SELF 变量将表单 POST 到自身 -
<html>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<p>姓: <input type="text" name="first_name" /></p>
<p>名: <input type="text" name="last_name" /></p>
<input type="submit" value="提交" />
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST")
echo "<h3>姓: " . $_REQUEST['first_name'] . "<br />"
. "名: " . $_REQUEST['last_name'] . "</h3>";
?>
</body>
</html>
将产生以下输出 -