PHP Post And Get

by Hiroshi on June 23, 2008

in PHP Basics, Post & Get

Post and Get are both used in forms to pass data from one page to another but there is a slight difference in both. Consider we have a form containing user name and password fields. On submit it goes to page named as submit.php. Now the behavior of form with these two get and post methods will be different as described below:

Form Post Method

<form action="submit.php" method="post">
<!-- some Data, lets say
User name field
Password field -->
</form>

URL displayed in address bar

https://path…/submit.php

This HTML code specifies that the form data will be submitted to the “submit.php” web page using the POST method. The way that PHP does this is to store all the “posted” values into an associative array called “$_POST“. Be sure to take notice the names of the form data names, as they represent the keys in the “$_POST” associative array.

We can get this posted data by the following method. Suppose username was the input name.

<?php
$user = $_POST['username'];
// echo "$user";
?>

Form Get Method

<form action="submit.php" method="get">
<!-- some Data, lets say
User name field
Password field -->
</form>

URL displayed in address bar In this case
https://path…/submit.php?name=king&password=1234&button=Submit

The get method is different in that it passes the variables along to the “submit.php” web page by appending them onto the end of the URL as displayed above. The question mark “?” tells the browser that the following items are variables. Now that we changed the method of sending information, we must change the “submit.php” code to use the “$_GET” associative array.

<?php
$user = $_GET['username'];
// echo "$user";
?>

So by Get method URL will contain posted data also in address bar as you can see ?name=king&password=1234&button=Submit with actual document path. I mean the form is useless if its a login form and you are displaying sensitive data in address bar, what the user is posting!
Be careful about using login form or password field to not code the from method ‘get’.
It must be post in this case.

Related Posts

Previous post:

Next post: