Home / Programming / PHP Programming / PHP GET and POST methods
PHP GET and POST methods

PHP GET and POST methods

There are two ways the client (web browser) can send HTTP request to the web server.

  • The GET method
  • The POST method

The Hypertext Transfer Protocol (HTTP) is the medium to send GET and POST request to the web server. The Web Server processes the request.

The GET Method

The GET method sends request in encoded string. Requested information is separated by question mark (?) sign and all information is separated by Ampersand (&) sign.

http://website.com/index.php?value=1&name=1&value=2&name=2

Notes on GET method:

  • GET method requests are cached by browser
  • GET method requests always stored in browser history
  • User can bookmark GET request
  • Never use GET method when dealing with sensitive and confidential data
  • GET method is restricted to send limited data

Example:

<?php
    if(!empty($_GET["name"]))
    {
        echo "Welcome ". $_GET['name'];
        die();
    }
?>

<html>
<body>
    <form action="<?php $_PHP_SELF ?>" method="GET">
    Name: <input type="text" name="name" />
    <input type="submit" />
    </form>
</body>
</html>

The POST Method

The POST method is secure method. The POST method send request via HTTP headers

POST index.php HTTP/1.1
Host: website.com
value=1&name=1&value=2&name=2

Notes on POST method:

  • POST method requests are not cached by browser
  • POST method requests are not stored in browser’s history
  • User can bookmark POST method requests
  • Never use GET method when dealing with sensitive and confidential data
  • POST method haven’t restrictions on length of data
  • POST method is secured to send sensitive data

Example:

<?php
    if(!empty($_POST["name"]))
    {
        echo "Welcome ". $_POST['name'];
        die();
    }
?>

<html>
<body>
    <form action="<?php $_PHP_SELF ?>" method="POST">
    Name: <input type="text" name="name" />
   <input type="submit" />
   </form>
</body>
</html>

Comments

comments

About Sumit Bijvani

Check Also

Submit a form in PHP without refreshing the page using AJAX

Submit a form in PHP without refreshing the page using AJAX

The following is a very simple trick to create form in PHP which can be ...

Leave a Reply

Your email address will not be published.