Pages

Tuesday 19 June 2012

GET POST meathods in PHP

                      $_GET AND $_POST Methods   

In PHP, the predefined $_GET variable is used to collect values in a form with method="get".
In PHP, the predefined $_POST variable is used to collect values in a form with method="post".
This means both the methods solve the same purpose in PHP.
                                   
                                            $_GET VS $_POST

It's not a matter of security. The HTTP protocol defines GET type requests as being idempotent, while POST may have side effects. In plain English that means that GET is used for viewing something, without changing it, while POST is used for changing something. For example, a search page should use GET, while a form that changes your password should use POST.

Also, note that PHP confuses the concepts a bit. A POST request gets input from the querystring and through the request body. A GET request just gets input from the querystring. So a POST request is a superset of a GET request; You can use $_GET in a POST request, and it may even make sense to have a parameter with the same name in $_POST and $_GET, meaning different things. For example, let's say you have a form for editing an article. The article-id may be on the querystring (So, available through $_GET['id']), but let's say that we want to change the article-id. The new id may then be present in the request body (Eg. $_POST['id']). OK, perhaps not the best example, so, the difference between the two.

$_GET METHOD

The predefined $_GET variable is used to collect values in a form with method="get"

Information sent from a form with the GET method is visible to everyone (it will be displayed in the browser's address bar) and has limits on the amount of information to send.
Let us Know discuss a example using the get method.
Below is the simple html form which act as input for our PHP code to Work.
NOTE:-A simple HTML page is only way of Inputting data or for PHP script to work

<html>
<body>
<form action="<?php $_PHP_SELF ?>" method="GET">
Name: <input type="text" name="name" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
</body>
</html>
Know we write the PHP script working behind the scene:-
<?php
if( $_GET["name"] || $_GET["age"] )
{
echo "Welcome ". $_GET['name']. "<br />";
echo "You are ". $_GET['age']. " years old.";
exit();
}
?>
$_POST METHOD
The POST method transfers information via HTTP headers. The information is encoded as described in case of GET method and put into a header called QUERY_STRING.

The POST method does not have any restriction on data size to be sent.


The POST method can be used to send ASCII as well as binary data.


The data sent by POST method goes through HTTP header so security depends on HTTP protocol. By using Secure HTTP you can make sure that your information is secure.


The PHP provides $_POST associative array to access all the sent information using GET method.

NOW we will see a post example.
Below is HTML form for input purpose:-
<html>
<body>
<form action="<?php $_PHP_SELF ?>" method="POST">

Name: <input type="text" name="name" />
Age: <input type="text" name="age" />

<input type="submit" />
</form>
</body>
</html>

Now is the PHP Script ;-
<?php
if( $_POST["name"] || $_POST["age"] )
{
echo "Welcome ". $_POST['name']. "<br />";
echo "You are ". $_POST['age']. " years old.";
exit();
}
?>





No comments:

Post a Comment