PHP – GET & POST Methods,There are approaches the browser customer can send records to the net server.
- The GET Method
- The POST Method
Before the browser sends the statistics, it encodes it the usage of a scheme referred to as URL encoding. In this scheme, name/value pairs are joined with same symptoms and unique pairs are separated by using the ampersand.
name1=value1&name2=value2&name3=value3
Spaces are removed and replaced with the + person and any other nonalphanumeric characters are replaced with a hexadecimal values. After the information is encoded it’s miles sent to the server.
PHP – GET & POST Methods,The GET Method
The GET method sends the encoded person statistics appended to the web page request. The web page and the encoded facts are separated via the ? Individual.
http://www.test.com/index.htm?name1=value1&name2=value2
- The GET technique produces an extended string that appears for your server logs, in the browser’s Location: field.
- The GET method is limited to send upto 1024 characters most effective.
- Never use GET technique when you have password or other touchy records to be despatched to the server.
- GET can’t be used to ship binary facts, like photos or word files, to the server.
- The statistics despatched with the aid of GET method may be accessed the use of QUERY_STRING surroundings variable.
- The PHP provides $_GET associative array to get admission to all the despatched information using GET technique.
Try out following example by putting the source code in test.php script.
<?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>
It will produce the following result −
PHP – GET & POST Methods,The POST Method
The POST approach transfers information through HTTP headers. The information is encoded as described in case of GET technique and placed right into a header known as QUERY_STRING.
- The POST technique does not have any restriction on data size to be sent.
- The POST method may be used to send ASCII as well as binary statistics.
- The information sent by using POST technique goes via HTTP header so security relies upon on HTTP protocol. By the use of Secure HTTP you may make certain that your records is comfortable.
- The PHP presents $_POST associative array to get right of entry to all the despatched facts the usage of POST approach.
Try out following instance by means of placing the supply code in check.Hypertext Preprocessor script.
<?php
if( $_POST["name"] || $_POST["age"] ) {
if (preg_match("/[^A-Za-z'-]/",$_POST['name'] )) {
die ("invalid name and name should be alpha");
}
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>
It will produce the following result −
The $_REQUEST variable
The PHP $_REQUEST variable carries the contents of both $_GET, $_POST, and $_COOKIE. We will speak $_COOKIE variable whilst we are able to give an explanation for approximately cookies.
The PHP $_REQUEST variable can be used to get the end result from shape statistics despatched with both the GET and POST methods.
Try out following instance through placing the supply code in test.Personal home page script.
<?php
if( $_REQUEST["name"] || $_REQUEST["age"] ) {
echo "Welcome ". $_REQUEST['name']. "<br />";
echo "You are ". $_REQUEST['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>
Here $_PHP_SELF variable incorporates the call of self script in which it’s miles being referred to as.
It will produce the following result −