PHP GET and POST Method

When we are using html form, we can send data to another page using two different methods

  • POST method
  • GET method

Both serves same purpose, but the internal mechanism is bit different. When using GET method key value pairs are passing through browser navigation bar. But for POST method a visitor cannot see the data through browse, basically it sending via HTTP headers.

GET Method

Spaces are removed and replaced with the + character or  any other non alphanumeric characters are replaced with a hexadecimal values. Values are URL encoded for better handling.

A sample GET method request will look like this

http://domian.com/index.php?categroy=outdoor&product=barbiedoll

The encoded string should pass after a question mark, otherwise we cannot parse the GET data.

  • GET method produces a long string that we can see on the browser navigation bar.
  • We can send upto 1024 characters using GET method.

See the below examples to get an understanding how GET method sending data. If you don’t specify a method in the form, default is GET.

This will create a encoded URL like

http://domain.com/formget.php?fistname=John&lastname=Doe&email=john@examle.com&mobile=909909

So your form will be sending all data to formget.php, which is a server side script. Next, we will see how to retrieve these GET values in a server side script, like PHP.

formget.php

When using GET method all the form values are exposed to public, so never use the GET method for sensitive data like passwords, or credit card information etc.

POST method

Using POST method is more safer than GET method and more data can transfer using POST method, there is no restriction for sending data. POST method transfers the data via HTTP headers.

See below an example of POST method form, you have to explicitly add the form method in order to do POST form

So in formpost.php, you can get the detials as follows

Conclusion

Both methods are using for transferring data from form to server side scripts. Do not use GET method while you dealing with sensitive data like passwords, credit card information.

You may be interested in this simple login script script, which uses these methods in a application

 

 

 

 

Editorial Staff
 

Editorial Staff at tutsplanet is a dedicated team to write various tutorial articles.