In PHP, $_REQUEST is a superglobal variable. It is an associative array which is a collection of contents of $_GET, $_POST and $_COOKIE variables.
- The settings in your “php.ini” file decides the composition of this variable.
- One of the directives in “php.ini” is request_order, which decides the order in which PHP registers GET, POST and COOKIE variables.
- The presence and order of variables listed in this array is defined according to the PHP variables_order.
- 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
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>
Start the XAMPP server and enter http://localhost/hello.php?first_name=Amar&last_name=Sharma as the URL in a browser window.
You should get the output as −
$_REQUEST with POST Method
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>
In your browser, enter the URL “http://localhost/hello.html”. You should get the similar output in the browser window.
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>
It will produce the following output −
Leave a Reply