There are two transferring methods (GET,POST)when a web browser send information to the server using HTTP.
<form action="postProcess.php" method="post">
<input name="username" type="text" />
<input name="password" type="text" />
</form>
This HTML code specifies that the form data will be submitted to the "postProcess.php" page using the POST method.
PHP would store all the "posted" values into an array called $_POST.
The form item names represent the keys in the array $_POST. ( the name in $_POST['name'] corresponds to the name that we specified in our HTML form)
post.html
<!DOCTYPE html>
<html lang="en">
<head>
<title>Example of PHP POST method - Lautturi.com</title>
</head>
<body>
<form action="postProcess.php" method="post">
<input name="username" type="text" />
<input name="password" type="text" />
<input type="submit" value="Submit">
</form>
</body>
getProcess.php
<?php
if(isset($_POST["username"])){
echo "<p>Username: " . $_POST["username"] . "</p>";
}
if(isset($_POST["password"])){
echo "<p>Password: " . $_POST["password"] . "</p>";
}
?>
Visit http://www.localhost/post.html ,Fill in the form and submit.
The data would be send to the script http://www.localhost/postProcess.php where the data be processed.
Notes:
$_POSTis a 'superglobal', or automatic global, variable. This simply means that it is available in all scopes throughout a script.
Similarly,when method="get" PHP provides a superglobal variable $_GET to access all information in a get request.
The browser would append the data onto the end of the URL(they are separated by ampersands &):
http://www.lautturi.com/process.php?username=XXX&password=XXX
Notes:
$_GETis a 'superglobal', or automatic global, variable. This simply means that it is available in all scopes throughout a script.
get.html
<!DOCTYPE html>
<html lang="en">
<head>
<title>Example of PHP GET method - Lautturi.com</title>
</head>
<body>
<form action="getProcess.php" method="get">
<input name="username" type="text" />
<input name="password" type="text" />
<input type="submit" value="Submit">
</form>
</body>
getProcess.php
<?php
if(isset($_GET["username"])){
echo "<p>Username: " . $_GET["username"] . "</p>";
}
if(isset($_GET["password"])){
echo "<p>Password: " . $_GET["password"] . "</p>";
}
?>
Visit http://www.localhost/get.html ,Fill in the form and submit.
The browser will redirect to the URL
http://www.localhost/getProcess.php?username=lautturi&password=123
and print the result.
GET methodget method is not suitable for send sensitive informations such as username and password because it is visible on the URL.get method.POST method is more secure than GET methodPHP provides another superglobal variable $_REQUEST that by default contains the contents of $_GET, $_POST and $_COOKIE.
By default, PHP registers theses variables into the _REQUEST array in this order: GET, POST, Cookie.
newer values will override older values.
request.html
<!DOCTYPE html>
<html lang="en">
<head>
<title>Example of PHP $_REQUEST Variable - Lautturi.com</title>
</head>
<body>
<form action="http://www.localhost:82/requestProcess.php?username=lautturi" method="post">
<input name="username" type="text" />
<input name="password" type="text" />
<input type="submit" value="Submit">
</form>
</body>
requestProcess.php
<?php
if(isset($_REQUEST["username"])){
echo "<p>Username: " . $_REQUEST["username"] . "</p>";
}
if(isset($_REQUEST["password"])){
echo "<p>Password: " . $_REQUEST["password"] . "</p>";
}
var_dump($_GET); // array("username"=>"lautturi"); // the data is from URL
var_dump($_POST); // array("username"=>"admin","password"=>"123"); // the data is from the post form
var_dump($_REQUEST); // array("username"=>"admin","password"=>"123"); //username from $_POST['username'] will override the older value.
?>