Get and Post Methods in PHP:

You already know that to create a form in HTML we use form element. But with the help of HTML form element we can only create a form but how to send data to server and get back to the client.

In PHP we have two methods that are use to send the data from client browser to the web server. The methods are:

  • Get Method
  • Post Method

Both methods are use to send data to the server.

 The Get Method in PHP:

The get method is use to send data to the server. The get method transfers the data via URL.The data you are sending will be shown in the URL and the information is separated by “?” like as follows

http://www.example.php/index.html?name1=value1&name2=value2

There are some points that you should know when you are using get method:

  • The information is always shown in the URL
  • In this method you have restriction that you can send only 2000 characters.
  • If you want to send sensitive data like password than do not use get method.
  • If you are using get method than you can access the data by Query String (?).
  • You have to use $_GET variable to fetch the data.

 Example: The code is divided in two parts HTML Coding & PHP coding

            First we are creating a HTML page and save it with index.html name:

Now we are creating “send.php” page because the action is send.php

 The Post Method in PHP:

The post method is also use send data to the web server. In the post method the data transfers via HTTP Request. If you are sending information by Post method than it will not display on URL.

There are some things that you should know when you are using post method:

  • The information is not shown in the URL
  • This method does not have any restriction to send data
  • If you want to send sensitive data like password than always use post method because it is secure.
  • You have to use $_POST variable to fetch the data.

 Example: The code is divided in two parts HTML Coding & PHP coding

First we create a HTML form and save as “index.html”

Now we will create a PHP page and save it as “send.php”

 Difference between Get & Post Method:

If the get and post method both are used to send information or data on web server than what is the difference between them.

Here are some basic and very common difference between get and post methods.

  • The main difference is that get method have some restriction of sending data whereas in post method you can send unlimited information for example if you want to send a file which occupy 4kb space than you can not use get method for transferring that file but with post method you can easily send that file to server.
  • The data will be display on URL when you use get method and if you want to send sensitive data like password than use only Post method because it is more secure than get.

 When to use get & when to use post methods?

  • If you want to display data in URL at that time use Get Method and when you don’t want to display data on URL at that time use Post method.

Leave a Reply

Your email address will not be published. Required fields are marked *