PHP Get And Post Methods And How To Use Them

Posted by TotalDC

Moving on in our series of PHP tutorials for beginners, in this tutorial you will learn how to send information to the server using PHP GET and POST methods and how to retrieve info.

What Are POST And Get Methods In PHP?

Web browser communicates with the server using one of two HTTP methods – GET and POST. Both methods pass the information differently and have some advantages and disadvantages.

GET Method In PHP

When using the GET method data is sent as URL parameters that are usually strings of parameter name and value pairs separated by ampersands (&). URL with GET data will look like this:

https://www.example.com/action.php?name=paul&age=31

More than one pair of parameters and values can be embedded in the URL. Only simple text can be sent using the GET method.

Advantages And Disadvantages Of Using GET Method In PHP

  • Since the data sent by the GET method are displayed in the URL, it is possible to bookmark the page with specific query string values.
  • GET method is not suitable for passing sensitive information like username and password, because these are fully visible in the URL.
  • There is a limitation to the total data to be sent.

PHP provides the superglobal variable $_GET to access all the information sent through the URL or submitted through HTML form using the method=”GET”.

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Simple GET Method Example</title>
</head>
<body>
<?php
if(isset($_GET["name"])){
    print "<p>Hi, " . $_GET["name"] . "</p>";
}
?>
<form method="GET">
    <input type="text" name="name" id="inputName">
    <input type="submit" value="Submit">
</form>
</body>

POST Method In PHP

When using the POST method data will not be visible in the URL.

Advantages And Disadvantages Of Using The POST Method In PHP

  • It is more secure than GET because user-entered information is not visible in the URL or in the server logs.
  • There is a much larger limit on the amount of data that can be passed and you can send text data and binary data (uploading a file) using POST.

PHP gives $_POST to access all the information sent via POST method or submitted through HTML form using the method=”POST”.

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Example of PHP POST method</title>
</head>
<body>
<?php
if(isset($_POST["name"])){
    print "<p>Hi, " . $_POST["name"] . "</p>";
}
?>
<form method="post">

    <input type="text" name="name" id="inputName">
    <input type="submit" value="Submit">
</form>
</body>

$_REQUEST Variable In PHP

Another superglobal variable $_REQUEST contains the values of $_GET and $_POST variables and $_COOKIE superglobal variable.

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Example of PHP $_REQUEST variable</title>
</head>
<body>
<?php
if(isset($_REQUEST["name"])){
    print "<p>Hi, " . $_REQUEST["name"] . "</p>";
}
?>
<form method="post">
    <input type="text" name="name" id="inputName">
    <input type="submit" value="Submit">
</form>
</body>

If you are new to PHP cookies and form handling – don’t worry. I will write about them some other time.