Php 简明教程

PHP - $_REQUEST

在 PHP 中,$_REQUEST 是一个超级全局变量。它是一个关联数组,由 $_GET、$_POST 和 $_COOKIE 变量的内容集合组成。

In PHP, $_REQUEST is a superglobal variable. It is an associative array which is a collection of contents of $_GET, $_POST and $_COOKIE variables.

  1. The settings in your "php.ini" file decides the composition of this variable.

  2. One of the directives in "php.ini" is request_order, which decides the order in which PHP registers GET, POST and COOKIE variables.

  3. The presence and order of variables listed in this array is defined according to the PHP variables_order.

  4. If a PHP script is run from the command line, the argc and argv variables are not included in the $_REQUST array because their values are taken from the $_SERVER array, which in turn is populated by the web server.

$_REQUEST with GET Method

将以下脚本保存在 Apache 服务器的文档文件夹中。如果你在 Windows 上使用 XAMPP 服务器,请将脚本作为 “hello.php” 放在 “c:/xampp/htdocs” 文件夹中。

Save the following script in the document folder of the Apache server. If you are using XAMPP server on Windows, place the script as "hello.php" in the "c:/xampp/htdocs" folder.

<html>
<body>
   <?php
      echo "<h3>First Name: " . $_REQUEST['first_name'] . "<br />"
      . "Last Name: " . $_REQUEST['last_name'] . "</h3>";
   ?>
</body>
</html>

启动 XAMPP 服务器并在浏览器窗口中输入 http://localhost/hello.php?first_name=Amar&last_name=Sharma 作为 URL。

Start the XAMPP server and enter http://localhost/hello.php?first_name=Amar&last_name=Sharma as the URL in a browser window.

你应该可以获得 output 如下 −

You should get the output as −

php $ request 1

$_REQUEST with POST Method

在文档根目录下,将以下脚本保存为 “hello.html”。

Under the document root, save the following script as "hello.html".

<html>
<body>
   <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>
</body>
</html>

在您的浏览器中,输入 URL “http://localhost/hello.html”。您应该在浏览器窗口中获得类似的 output

In your browser, enter the URL "http://localhost/hello.html". You should get the similar output in the browser window.

php $ request 2

您还可以在 HTML 脚本中嵌入 PHP 代码,并使用 PHP_SELF 变量将表单 POST 到自身 −

You may also embed the PHP code inside the HTML script and POST the form to itself with the PHP_SELF variable −

<html>
<body>
   <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
      <p>First Name: <input type="text" name="first_name" /></p>
      <p>Last Name: <input type="text" name="last_name" /></p>
      <input type="submit" value="Submit" />
   </form>
   <?php
      if ($_SERVER["REQUEST_METHOD"] == "POST")
      echo "<h3>First Name: " . $_REQUEST['first_name'] . "<br />"
      . "Last Name: " . $_REQUEST['last_name'] . "</h3>";
   ?>
</body>
</html>

它将生成以下 output

It will produce the following output

php $ request 3