PHP Tutorial Tutorials - PHP POST and GET

PHP POST and GET

There are two transferring methods (GET,POST)when a web browser send information to the server using HTTP.

The POST Method

<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:

$_POST is a 'superglobal', or automatic global, variable. This simply means that it is available in all scopes throughout a script.

The GET Method

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:

$_GET is 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.

Advantages and Disadvantages between GET AND POST

  • It is possible to bookmark the page with specific query using GET method
  • The get method is not suitable for send sensitive informations such as username and password because it is visible on the URL.
  • You can send limited amount of data through get method.
  • POST method is more secure than GET method

$_REQUEST Variable

PHP 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.
?>
Date:2019-10-01 02:48:17 From:www.Lautturi.com author:Lautturi