Saturday, February 3, 2018

PHP Get & Post Method Tutorial Learne

Get & post method

The client browser uses two methods to send information from the client to the web server. These methods are GET and POST. However, before sending the information, the browser first encodes the information using the coding scheme called URL encoding. In this coding scheme, key-value pairs, which are here name-value pairs, are combined together in such a way that different name-value pairs are separated by & (ampersand). Any whitespaces are replaced by the plus sign (+) and non-alphanumeric characters are replaced with their hexadecimal values. Once the encoding process is over, the data is sent to the web server.

The GET Method 

When the GET method is used for sending information to the web server, it send the same along with the page URL. The URL is appended by the encoded information and the question mark sign (?) separates the former from the latter. Some of the key things to note about the use of this method include –

●    Since this method appends the page URL, the sent information will also appear in logs of the server.

●       The maximum number of characters that can be sent using the GET method is 1024 characters.

●           There is an inherent issue of compromised confidentiality with this method. Therefore, its use must be avoided when you are sending passwords or critical information to the web server.

●             Another inherent limitation of the GET method is that documents and images cannot be sent to the server using this method.

●             Besides the above-mentioned, it is also possible to access the data sent using this method by retrieving the QUERY_STRING environment variable. Therefore, data is not safe.

●             It is possible to access all the sent information using this method by accessing $_GET, which an associative array provided by PHP for this purpose.

You can try the sample code given below to understand how this method is used.

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

The POST Method 

An alternative to the GET method is POST method. This method makes use of HTTP headers to transfer information from the client to the server. The encoded information is put into the header. Some of the things that you must know about the use of this method for information transfer has been listed below –

●   Unlike GET method, there is no limitation on the number of characters that can be sent using this method.

●    It is possible to send binary as well as ASCII data using this method.

●     As far as the security of data is concerned, data security totally lies in the hands of the HTTP protocol. You can ensure higher security of data by using HTTP Secure.

●     All the data using the POST method can be accessed using the $_POST associative array.

You can try the sample code given below to understand how this method is used.

<?php 

if( $_POST["name"] || 

$_POST["age"] ) { if (preg_match("/[^A-Za-z'-]/",$_POST['name'] )) 



die ("Name is invalid!"); 



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> 

The $_REQUEST Variable 

The contents of $_GET and $_POST are available in $_REQUEST. Besides this, $_REQUEST also contains data associated with $_COOKIE. The form data sent to the web server using both the methods namely GET and POST can be accessed by accessing this variable. Here $_PHP_SELF variable contains the name of self script in which it is being called.

0 comments:

Please comment and follow this site blog inbox