PHP Get and Post | Ways to use $_GET and $_POST for forms data

Introduction to PHP GET and POST

We see a lot of forms while surfing the internet. Like creating an account with facebook, creating an email account or subscribe to a newsletter by name/email etc. When we fill the forms with data, the PHP provides methods to receive it just like other web-based languages.

In PHP, the $_GET and $_POST superglobals are used to collect the form data in the form of arrays.

Let us go through these methods in order to understand how to use and learn the differences between the two.

The Get method

The GET PHP method sends information by appending variables to the end of URL. The page and information are separated by a ‘?’ (question mark) character. You can use PHP Get method ($_GET) to catch the sent data.

The URL would look like this in the address bar after Submitting information by using the Get method:

Home

GET method example

Following example shows using the GET method with form’s get method.

After entering the name and age in the above example, when you click the submit button, it will come back to the same page and display your name and age. If you look at the address bar, you will see the following:

For example, if you entered the name as ‘Mike’ and age ’30’ while your domain is “localhost” and page name is index.php then this is how the address bar would look:

http://localhost/index.php?yourname=Mike&yourage=30

Following are the points to be noticed as using the Get in above example:

  1. In the HTML form, the method= ”GET” is used.
  2. When receiving information at the server side, you have to use this:
  3. $_GET to get variable names sent by forms. e.g. $_GET[‘yourname’]You may assign this to a variable and save it to database e.g. $name=$_GET[‘yourname’];
  4. The HTML form data will be visible in the address bar as using the get method. Where “?” is used to separate from domain and variables are separated by the “&” characters.

Where to use the GET method

  1. The amount of data that can be sent is limited in the GET method. It is about 2000 characters limit.
  2. The GET method cannot send binary data.
  3. As variables are visible in the address bar, this is visible to anyone so considered as un-secure. This may be helpful in case you want to offer bookmarking your site with non-sensitive data.
  4. Get should not be used for sensitive information like passwords.
  5. PHP $_GET is used to retrieve sent data along with using the form’s get method.

PHP Post method

As opposed to GET method used in the forms, the information sent by using the POST PHP method is not visible to the users. The data sent by using post method of forms is received by the $_POST method as shown below:

POST method Example

After entering the name and age in the above page, when you click the submit button, it will come back to the same page and display your name and age. You can see, the address bars will show just the .php file name.

Following things should be noticed in the above example:

  1. In the HTML form, the method=”POST” is used.
  2. We used $_POST to get the variable names sent by the form. e.g. $_POST[‘yourname’]. 
  3. Form variables will not be visible in the address bar, unlike the GET method.

Where to use the POST method

  1. The amount of data that can be sent is not limited in the POST method.
  2. The POST method can send binary data. So this method is used to upload files like images, word document, pdfs, or other formats.
  3. The POST should be used for sensitive information like passwords etc.
  4. Your users won’t be able to bookmark the page with submitted information to be used in next visits as information is not visible.
  5. You have to use the PHP Post method to retrieve the information sent by web forms.

Related: PHP $_Request variable


Was this article helpful?

Related Articles

Leave A Comment?