Home Php php $_GET and $_POST functions

 
 

Share on Google+Share on Google+

php $_GET and $_POST functions

Advertisement
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

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: December 17, 2009

Related Tutorials

Discuss: php $_GET and $_POST functions   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:4
drar kidane
May 18, 2013
Designer

thanks very much, the idea is clearness as a result i understand more points on your posted
Santu Nath
June 22, 2012
php $_get and $_post

thank you rose india;Defination and examples are very clear and meaningful.Also eassy to understand.
Sachin Kolige
August 16, 2012
Mistake in the file name

Hi, I am a mewbie to the world of php and I guess there is a mistake in the file name given in the example. You have given the file name as phpPost and demonstrated the get function and vice-versa. It is it misleading. so please take a look at the title and the method attribute used in form tag. -Sachin
Ganesh prasad
August 24, 2012
scripting language

I m student ,so we just about scripting language, that's it.
DMCA.com