Monday, 22 April 2013

PHP $_GET & PHP $_POST


 The $_GET Variable

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.

When using method="get" in HTML forms, all variable names and values are displayed in the URL.

The get method is not suitable for very large variable values. It should not be used with values exceeding 2000 characters.


·   The GET method produces a long string that appears in your server logs, in the browser's Location: box.

·  The GET method is restricted to send upto 1024 characters only.

·  Never use GET method if you have password or other sensitive information to be sent to the server.

·  GET can't be used to send binary data, like images or word documents, to the server.

· The data sent by GET method can be accessed using QUERY_STRING environment variable.

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


Example:

<?php
  if( $_GET["name"] || $_GET["age"] )
  {
     echo "Welcome ". $_GET['name']. "<br />";
     echo "You are ". $_GET['age']. " years old.";
     exit();
  }
?>

<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>


Output:

Welcome John!

You are 28 years old.



The $_POST Variable

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.

The predefined $_POST variable is used to collect values from a form sent with method="post".

Information sent from a form with the POST method is invisible to others and has no limits on the amount of information to send.

Note: However, there is an 8 MB max size for the POST method, by default (can be changed by setting the post_max_size in the php.ini file).
  

Example:

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

<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>


Output:

Welcome John!

You are 28 years old.






0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More