php $_GET and $_POST functions

PHP provides two special functions called $_GET & $_POST, this functions are used to get the values which are passed from a user-filled form, like user registration or login form etc.

php $_GET and $_POST functions:

     

PHP provides two special functions called $_GET & $_POST, this functions are used to get the values which are passed from a user-filled form, like user registration or login form etc.

$_GET is less secure and effective than $_ POST, $_GET has few limitations like it can send about 100 words of data at a time, whereas $_POST does not have such kind of limitations, we can send any amount of data using this variable. $_POST is more secure than $_GET in the means of that whenever we send data from one web page to another using $_GET variable we can see the values in address bar, on the contrary $_POST does not allow any thing to be displayed in address bar of the browser.

Above mentioned difference is visible but subtle difference between these two is that $_GET method is idempotent that means according to HTTP specifications GET request is done when the request would not be changed on the other hand POST requests are not idempotent, it should be used when there would have any possibility for changes. One GET request is for one URL and since GET method does not change,  browsers cached the response pages. On the other hand responses for POST method are not stored on the browser's cache memory.

POST requests are ideal for queries whose response pages could change like placing an order, updating database etc. Whereas GET requests are ideal for those queries whose responses could not change over a period of time.

Let's check the difference with the help of examples:

Example for $_GET:

phpGet.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>Insert title here</title>

</head>

<body>

<div align="center" class="post" dir="ltr" id="post" style="background-color:gray" style="border:medium">

<form name="post" method="post" action="phpPost.php">

<font color="white">Enter your name:</font><input type="text" name="name"><br/>

<font color="white">Enter Your age:</font> <input type="text" name="age"><br/>

<input type="submit" value="submit"></input><br/>

</form>

</div>

</body>

</html>

phpGet.php

<?php

$name=$_GET['name'];

$age=$_GET['age'];

echo "Howdy $name your age is: $age";

?>

Output:

Howdy sdfsdf your age is: 13

and the browser's address bar will be displayed as follows:

http://localhost/php/phpGet.php?name=sdfsdf&age=13

On the other hand if you send these data using $_POST method, process and output will be:

There will be one small change in the html file that would be method="post" as follows:

phpPost.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>Insert title here</title>

</head>

<body>

<div align="center" class="get" dir="ltr" id="get" style="background-color:gray" style="border:medium">

<form name="get" method="POST" action="phpGet.php">

<font color="white">Enter your name:</font><input type="text" name="name"><br/>

<font color="white">Enter Your age:</font> <input type="text" name="age"><br/>

<input type="submit" value="submit"></input>

</form>

</div>

</body>

</html>

phpPost.php

<?php

$name=$_POST['name'];

$age=$_POST['age'];

echo "Howdy $name your age is: $age";

?>

Output:

Output will be same as before but the difference between these two will be reflected in the browser's address bar:

Howdy DSFSD your age is: 42

Address bar will look as below:

http://localhost/php/phpGet.php

Ads