PHP GET & POST

PHP GET & POST

PHP GET

PHP’s $_GET variable is used to collect names and data from a form using the method GET. This variable is an array of variable names and their values that are sent by HTTP GET method. It basic purpose is to collect data from a form. When the $_GET variable is used, all variables used are displayed in the URL and hence it should not be used for collecting secured information like passwords. The variables have a limited length.

Example:
<form action="sample.php" method="get">
Name: <input type="text" name="name"/>
Age: <input type="text" name="age" />
<input type="submit" />
</form>

On clicking submit the URL resembles to :
http://www.mysamplesite.com/sample.php?name=jim&age=37

The sample.php file can be used for catching the form data using $_GET
Hello <?php echo $_GET["name"]; ?>.<br />
You are <?php echo $_GET["age"]; ?> years old!

PHP POST

PHP’s $_POST variable is used to collect names and data from a form using the method POST. This variable is an array of variable names and their values that are sent by HTTP POST method. It basic purpose is to collect data from a form. When the $_ POST variable is used, all variables used are NOT displayed in the URL . Also, they have no restrictions to the length of the variables.

Example:
<form action="sample.php" method="post">
Name: <input type="text" name="name" />
Age: vinput type="text" name="age" />
<input type="submit" />
</form>

On clicking submit the URL resembles to:
http://www.mysamplesite.com/sample.php

The sample.php file can be used for catching the form data using $_POST
Hello <?php echo $_POST["name"]; ?>.<br />
You are <?php echo $_POST["age"]; ?> years old!

Explain when to use GET or POST. Provide example for both the ways.

Both GET and POST are used to collect data from a form. However, when security is desired $_POST should be used. When the $_ POST variable is used, all variables used are NOT displayed in the URL. Also, they have no restrictions to the length of the variables. GET should be used when the interaction with the form is more like a question. For e.g. firing a query etc. POST should be used more often when the interaction is more like an order or the interaction changes the state of the resource.

POST example:
<form action="sample.php" method="post">
Name: <input type="text" name="name" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>

On clicking submit the URL resembles to: http://www.mysamplesite.com/sample.php

The sample.php file can be used for catching the form data using $_POST
Hello <?php echo $_POST["name"]; ?>.<br />
You are v?php echo $_POST["age"]; ?> years old!

GET Example:
<form action="sample.php" method="get">
Name: <input type="text" name="name" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>

On clicking submit the URL resembles to : http://www.mysamplesite.com/sample.php?name=jim&age=37

The sample.php file can be used for catching the form data using $_GET
Hello <?php echo $_GET["name"]; ?>.<br />
You are <?php echo $_GET["age"]; ?> years old!
PHP server-side validation
Server side validations work when the client side validations fail to work ( if the java script is turned off).
PHP cookies
A cookie is used for identification purposes. It is more commonly used to identify a user in a session. It is a small file the application inserts on the users computer......
PHP sessions
PHP sessions introduction - When a user logs in an application, his details are usually stored in a session variable.....
Post your comment