GET and POST methods in PHP

Tram Ho

When you access to use the website, you are a Client. When you log in or register for an account, or you post comments, the data will be sent from the client to the Server, so how does the Server receive your information? The server will be received through POST and GET methods.

There are two ways to send data from the Client to the Server that is to use the GET method or the POST method, both of which are in nature that programmers only know, but their users don’t care what it is, unless they are hackers. In order not to lose any more time, we will go into understanding each other, compare them with each other and discuss when we use POST and when we use GET.

Our lesson content is as follows:

  • GET method in PHP
  • POST method in PHP
  • Comparison between POST and GET

1. GET method in PHP

The GET method is easy to see that the URL will include the data we want to send

Client submits

The GET method is a method that sends data through the URL in the Browser address bar. Server will receive that path and analyze and return the results to you. Server will analyze all the information behind the question mark (?) Which is the data that the client sends.

For example: With the URL , the server will receive the value id = 12

To transmit a lot of data to the server we use the & sign to separate pairs of values. Let’s say I want to pass id = 12 and title = 'method_get' then the URL will be of the form . Note to you that the position of the value pairs is not important, meaning that the title pair can be in front of the id pair.

The server receives the data

All the data that the client sends by the GET method is stored in a global variable that PHP creates, the variable $ _GET , which is an associative array type that stores a list of data from the client sent. the law of key => value . For example, with the URL , the data will be stored in the $ _GET variable as:

$_GET = array( 'id' => '12', 'title' => 'method_get' );

So to get data, we just need to do the following:


Create a get.php file located in the WWW folder of Vertrigo Server or the htdocs folder of Xampp, then copy this code to:

Then go to the browser to type the following path: localhost / get.php? Id = 12 & title = method_get and watch the results. Try changing or adding values ​​to see if there is anything else.

Important Note: Before getting a certain data, you must check whether it exists or not, because if you do not check it, assume the user did not pass the data but you received an error. right. To test we use the function isset ($ tenbien) in php.

For example:

PHP POST method

The POST method is more secure because the submitted data must be through an HTML form so it is hidden, meaning we cannot see those values.

Client Send Up

With the GET method, the data is seen on the URL, the POST method is the exact opposite, the POST will send the data via an HTML form and the values ​​will be defined in the input types (textbox, radio, checkbox, password, textarea, hidden) and are identified by the name of the input.

Server receives data All data sent by POST method are stored in a global variable $ _POST created by PHP itself, so to get data you just need to get in this variable. As well as note that before taking, you must use the isset ($ bien) function to check whether or not.


Step 1: You create a post.php file located in the WWW folder of Vertrigo Server or the htdocs folder of Xampp, then enter the code to create this form:

Step 2: Open your browser and type the path localhost / post.php and look at the form you just created.

Step 3: You edit the HTML code by adding a PHP script as follows:

Step 4: Refresh your browser, enter your username and password, then click the button ” Submit Data ” and see your results.

3. Comparison between POST and GET

In order for you to understand more about the POST and GET methods, let’s compare a little bit.


  • All send data to the Server


  • The POST method is more secure than GET because the data sent implicitly cannot be seen.
  • The GET method is sent explicitly, we can see it on the URL so it’s not secure.
  • The GET method is always faster than POST because the sent data is kept in the browser by the Browser. When executing with POST, the Server always executes the command and then returns it to the Client, while with GET, the Browser will check if the cache is available, if so, it will immediately return it without sending to the Server.

When to use GET – POST

  • When the data you want to SEO, you must use the GET method.
  • When the data you do not need to be secure, use the GET method, otherwise secure the data using the POST method.

For example when logging in, Comment, post messages using POST method. When retrieving the message, use the GET method …

When the request uses the select statement, then use GET, when the request uses the insert update, delete command, we should use POST.

Share the news now

Source : Viblo