Computers Windows Internet

Difference between get and post. Using the GET and POST methods. More about HTTP

What they have in common is that they work the same way. There is technically no difference between them. But there are ideological differences.

I'll cover them in the context of PHP. Please note that the HTTP protocol is indirectly related to PHP because it was created for exchange html pages PHP just extends the capabilities of both.

GET request is used to receive data and POST to send. (As a reminder, they technically work the same way).

Therefore, in the context of PHP, based on this ideology, they did the following:
1. Superglobal arrays ($ _GET, $ _POST) are created by default every time you start PHP.
2. If there is a question mark (?) In the query string. Then everything that counts after him parameters GET request, they are presented in the format "key" = "value" and the ampersand character (&) is used as a separator
Example:
GET /index.php?name=Andrey&surname=Galkin
this is a query string, there are 2 parameters. these parameters will end up in the $ _GET array.
3. $ _POST is filled in another way. the contents of this array are populated from the "request headers". That is, from a place hidden from view in an explicit form. All the routine of creating such headers is taken over by the browser. Although sometimes something is edited manually in the headings.

Most often, post request is used in forms (for submitting data).

For example we have a login form 2 fields login and password.

Let's imagine we are using the GET method. Then, when submitting the form, we will go to the following address /login.php?login=Andrey&password=123 agree that it is not at all safe to transfer such information in this way. Anyone can open your browser and starting to enter the site address, he can see your passwords and logins from history.

But if we specified with the POST method, then we would receive the following request:
POST /login.php (login = Andrey & password = 123) what would be hidden in brackets and not saved in the browser in any way.

In general, summing up:
GET is to get a specific page in a specific way (sorting, current blog page, search string, etc.).
POST - for sending data that does not affect the display of the page, in the sense that this data only affects the result of the script execution (logins, passwords, credit card numbers, messages, etc.).

And one more good news, they can be combined, for example
POST /index.php?page=login (login = Andrey & password = 123) I think I've already explained enough what will come of it and what parameters will get into which array.

This post is intended to explain the principles of transferring data on the Internet using two main methods: GET and POST. I wrote it as a supplement to the instructions for the shift work generator for those who are hardly interested in the details ☺.

Go to the following address (this is for visual explanation): http://calendarin.net/calendar.php?year=2016 Pay attention to the address bar of the browser: calendarin.net/calendar.php ? year = 2016 The main file is named, followed by a question mark (?) And a "year" parameter with a value of "2016". So, all that follows the question mark is a GET request. It's simple. To pass more than one parameter, but several, then they need to be separated by an ampersand (&). Example: calendarin.net/calendar.php ? year = 2016 & display = work-days-and-days-off

The main file is still named, followed by a question mark (?), Then - the parameter "year" with the value "2016", then - the ampersand (&), then - the parameter "display" with the value "work-days-and-days -off ".

GET parameters can be changed directly in address bar browser. For example, changing the value "2016" to "2017" and pressing the key, you will go to the calendar for 2017.

This is the transmission of data in a hidden way (the page address does not change); that is, you can see what was transmitted only with the help of a program (script). For example, in the following tool for counting characters in a text, the source data is transmitted using the POST method: http://usefulonlinetools.com/free/character-counter.php

If you have any questions, comments and my E-mail at your service.

In addition to the GET method, which we discussed in the previous post, there is another method for sending a request via the HTTP protocol - the POST method. The POST method is also very often used in practice.

If, in order to access the server using the GET method, it was enough for us to type the request into the URL, then in the POST method everything works according to a different principle.

In order to fulfill this kind of request, we need to click on the button with the type = "submit" attribute, which is located on the web page. Note that this button is located in the element

with the method attribute set to post.

Consider this HTML:

Enter text:


If the user enters any text into the text field and clicks on the "Submit" button, then the text variable with the value of the content entered by the user will be sent to the server.

POST and GET requests in simple words

This variable will be sent by the POST method.

If you write in the form like this:

Then the data will be sent using the GET method.

If, in the case of a GET request, the amount of data that we could transfer was limited by the length of the browser's address bar, then in the case of a POST request, there is no such limitation, and we can transfer significant amounts of information.

Another difference between the POST method and GET, the POST method hides all the variables passed to it and their values ​​in its body (Entity-Body). In the case of the GET method, they were stored in the request string (Request-URI).

Here's an example of a POST request:

POST / HTTP / 1.0 \ r \ n
Host: www.site.ru \ r \ n
Referer: http://www.site.ru/index.html\r\n
Cookie: income = 1 \ r \ n
Content-Type: application / x-www-form-urlencoded \ r \ n
Content-Length: 35 \ r \ n
\ r \ n
login = Dima & password = 12345

Thus, by transmitting data using the POST method, it will be much more difficult for an attacker to intercept it, because they are hidden from view, so the POST method is considered safer.

In addition, the POST method can transfer not only text, but also multimedia data (pictures, audio, video). There is a special Content-Type parameter that determines the kind of information that needs to be transferred.

Finally, the POST variable is used to retrieve the data transmitted by this method on the server.

Here's an example of processing in PHP:

echo $ _POST [‘text’];
?>

In the last post, we decided that the browser (client) sends HTTP requests to the server, and the server sends HTTP responses to the client. These requests and responses are processed according to certain rules. There is, something like the syntax, how and in what sequence should be written. There must be a well-defined structure.

Let's take a closer look at this structure, which is used to build requests and responses in the HTTP protocol.

An HTTP request consists of three main parts, which are in the exact order shown below. There is an empty line between the headers and the body of the message (as a separator), it is a line feed character.

Empty string (separator)

Post and Get requests, what's the difference between them and which is better and for what purposes?

message body (Entity Body) - optional parameter

Query string- specifies the transfer method, the URL to access, and the version of the HTTP protocol.

Headings- describe the body of messages, transmit various parameters and other information and information.

message body- this is the data itself, which is transmitted in the request. The body of the message is optional and can be omitted.

When we receive a response request from the server, the body of the message is most often the content of the web page. But, when making requests to the server, it can also sometimes be present, for example, when we transfer the data that we filled out in the form feedback to the server.

In more detail, each element of the request, we will consider in the following notes.

Let's take one real server request as an example. I have highlighted each part of the request with its own color: the request line is green, the headers are orange, the message body is blue.

Browser request:

Host: webgyry.info

Cookie: wp-settings

Connection: keep-alive

In the following example, the message body is already present.

Server response:

Content-Type: text / html; charset = UTF-8

Transfer-Encoding: chunked

Connection: keep-alive

Keep-Alive: timeout = 5

X-Pingback: //webgyry.info/xmlrpc.php

Untitled document

These are the messages that are exchanged between the client and the server over the HTTP protocol.

By the way, do you want to know if there is any sense in some element on your site using the "goals" of Yandex Metrics and Google Analytics?

Take away what does NOT work, add what works and double your bottom line.

A course on setting up Yandex Metrica goals ..

A course on setting up Google Analytics goals ..

The HTTP client sends a request to the server in the form of a request message, which has the following format:

  • Request string (required)
  • Title (optional element)
  • Empty string (required)
  • Message body (optional)

Let's take a look at each of these elements separately.

Query string

The request string begins with a method token, followed by the request URI and protocol version. Elements are separated from each other by spaces:

Let's consider this element in more detail.

Request method

This element specifies the method to be called on the server side on the specified URI.

There are eight methods in HTTP:

  • HEAD
    Used to get the status line and title from the server by URI. Doesn't change data.
  • GET
    Used to receive data from the server at the specified URI. Doesn't change data.
  • POST
    Used to send data to the server (such as developer information, etc.) using HTML forms.
  • PUT
    Replaces all previous data on the resource with the new loaded data.
  • DELETE
    Removes all current data on the resource specified by the URI.
  • CONNECT
    Establishes a tunnel connection to the server at the specified URI.
  • OPTIONS
    Describes the connection properties for the specified resource.
  • TRACE
    Provides a message containing a reverse trace of the location of the specified resource URI.

Request URI

URI (Uniform Resource Identifier) ​​is the identifier of the resource to which the request is sent. The most common URI format is shown below:

‘*’ used when the HTTP request is not specific to a specific resource, but to a server. Used only when the method does not need to be applied to the resource. For example,

absolute uri used when an HTTP request is made to a proxy. The proxy is requested to pass the request from the available cache and returns a response. For example:

asbolutny_path | a source used by most chatso.

Learning to work with GET and POST requests

A specific resource of a specific server is requested. For example, a client wants to get a resource from the server through port 80. The resource address is “www.proselyte.net” and sends the following request:

Requesting header fields

Header fields allow the client to communicate Additional information about the request and about itself to the server. These fields act as request modifiers.

Below is a list of the most important header fields that can be used:

  • Accept-Charset
  • Accept-Encoding
  • Accept-Language
  • Authorization
  • Expect
  • If-Match
  • If-Modified-Since
  • If-None-Match
  • If-Range
  • If-Unmodified-Since
  • Range
  • Referer
  • User-Agent

If we want to implement our own client and our own web server, then we can create our own header fields.

Example HTTP request

This concludes our study of HTTP requests.
In the next article, we'll look at HTTP responses.

One of the ways how you can send an HTTP request to the server is a GET request. This method is the most common and most often requests to the server are made using it.

The easiest way to create a GET request is to type the URL into your browser's address bar.

The browser will transmit the following information to the server:

GET / HTTP / 1.1
Host: webgyry.info
User-Agent: Mozilla / 5.0 (Windows NT 6.1; rv: 18.0) Gecko / 20100101 Firefox / 18.0
Accept: text / html, application / xhtml + xml, application / xml; q = 0.9, * / *; q = 0.8
Accept-Language: ru-RU, ru; q = 0.8, en-US; q = 0.5, en; q = 0.3
Accept-Encoding: gzip, deflate
Cookie: wp-settings
Connection: keep-alive

The request consists of two parts:

1. request line

2.headers (Message Headers)

Please note that the GET request does not have a message body. But, this does not mean that with its help we cannot transfer any information to the server.

Difference between GET and POST methods

This can be done using special GET parameters.

To add GET parameters to the request, you need to put a "?" Sign at the end of the URL. and after it, start asking them according to the following rule:

parameter_name1 = parameter_value1 & parameter_name2 = parameter_value2 & ...

The separator between the parameters is the "&" sign.

For example, if we want to pass two values ​​to the server, the username and his age, then this can be done with the following line:

http://site.ru/page.php?name=dima&age=27

When done this request, the data goes into the so-called QUERY_STRING environment variable, from which it can be retrieved on the server using the server-side web programming language.

Here's an example of how this can be done in PHP.

echo "Your name:". $ _GET ["name"]. "
»;
echo "Your age:". $ _GET ["age"]. "
»;
?>

The $ _GET ["parameter_name"] construct allows you to display the value of the passed parameter.

As a result of executing this code in the browser, the following will be displayed:

Your name: dima
Your age: 27

we also make a request to the server using the GET method.

You may have noticed that on most sites you can contemplate the following addresses:

Http: //site/index.php? Blog = 2

Here, even without knowing php, you can guess that we are referring to the file index.php But few people know what comes after the question mark. It's pretty simple: ? blog = 2 this is the declaration of the global variable "$ _GET [" blog "]" with the value "2". Thus, I pass a variable to the script that is responsible for displaying information from the database. Let's write a small script in which you will clearly see everything:

if (isset ($ _ GET ["blog"])) (
echo $ _GET ["blog"];
}
?>

We use the condition operator if () as the condition is the following line:

Isset ($ _ GET ["blog"])

isset () allows you to find out if the variable that is specified in parentheses exists, that is, the condition that I described in the code sounds like this: If the variable $ _GET ["blog"] exists, then display the contents of this variable on the screen. Here's what happened:

I think it's clear A global variable is being created $ _GET with the identifier that we declared in the address bar ( in this case with the identifier "blog")

Now I want to clarify one point. Suppose we need to declare two variables, how do we do this? The first variable is declared after the question mark "?" The second variable is declared after such a "&" ( To be honest, I don't know what this sign is), here's an example of declaring three variables:

Http: //site/index.php? A = 1 & b = 2 & c = 3

Here is the output code:

if (isset ($ _ GET ["a"]) AND isset ($ _ GET ["b"]) AND isset ($ _ GET ["c"])) (
echo $ _GET ["a"]. "
";
echo $ _GET ["b"]. "
";
echo $ _GET ["c"]. "
";
}
?>

The condition goes like this:

If there is a global variable $ _GET ["a"] and a global variable $ _GET ["b"] and a global variable $ _GET ["c"] then display them, here is the result:

Forms

Before we get to post requests, you need to understand what forms are? Why is it necessary? Because the global variable $ _POST [""] is created through the forms. What is a form? These are fields for entering some kind of information by the user. Fields are in one line, large fields, there are also radio buttons, check boxes. Let's sort everything out in order ...

Form is a tag:


form elements

The form has attributes, I will list the most common ones:

Let's create a form:


form elements

As a handler file, I put the file test.php as it is in it that I write examples for you. I put the method of sending post as these methods are used in 99.9% of cases. I also gave our form a name - form

Now let's dive into the world of form elements. The first thing you need to understand is that almost all elements are tags. the only difference is in the attribute type at these tags. Let me list the form elements used:

I'm sure you have come across such fields more than once, so here, as they say: "no comments"

Now let's put together a short training questionnaire that we will work with next. Our task is to compose a small questionnaire that will tell us the name of the person who filled out, gender, what country he is from, his favorite color and a text field where the user can add something about himself. That's what I did:

Your Surname First Name Patronymic:

What's your gender:
M
F

What country are you from



Favorite color (s):

Black:
Red:
White:
Another:

About myself:




Note that almost every tag has an attribute value, what is it for? It records the data that you are going to transfer to another page. I hope it is clear

Now, if we run this code in a browser, we will see the following:

For the form, I used the attribute action with the meaning test.php this means, as I said, that the data from the form will be passed to the test.php file.

POST request

Now let's write php code that will allow us to see the information we entered. Where is the data stored? In the case of the get request, our data was stored in the global variable $ _GET [""]. When a post request is made, the data will be in the global variable $ _POST [""]. V square brackets, it is necessary to register, as in the case of the global variable get, the identifier. The question is, where can I get this identifier? That's why we need the name attribute on form elements! It is these names that serve as a key for us in the global array post. Well, let's start describing the script:

if (isset ($ _ POST ["submit"])) (
echo "Name:". $ _ POST ["fio"]. "
";
echo "Sex:". $ _ POST ["sex"]. "
";
echo "Country of residence:". $ _ POST ["city"]. "
";

Echo "Favorite color (s):
";
echo $ _POST ["color_1"]. "
";
echo $ _POST ["color_2"]. "
";
echo $ _POST ["color_3"]. "
";
echo $ _POST ["color_4"]. "
";
echo "About me:". $ _ POST ["about"]. "


";
}
?>

The if condition we have written says: If there is a global variable $ _POST ["submit"], then we display the data on the screen. This global variable is created if we clicked on the submit button, that's what in this example the name attribute is required in the button. You may well be wondering why the name attribute is optional for the button? It's pretty simple. Usually the programmer does not track the pressing of the button, but tracks the data sent. For correct work, for example, contact forms, it is necessary to track not the pressing of a button, but the correctness of the information input, and find out whether this information was entered at all. In our example, we did not check the sent data, but simply tracked the button click, to simplify the example ... Here's what we got:

Conclusion

Well, today we have analyzed two methods of transferring data between scripts, as well as gallops got acquainted with forms. I really hope that this information will be useful to you at least somewhere. If you have any questions or thoughts, write comments. Good luck, I have everything for today!

P.S .: Would you like to computer games become even more realistic? Directx 11 for windows 7 can be downloaded for free on Windows in! Enjoy great graphics!

There are two concepts common to almost all programs - processing input data and outputting results. On this page, we will focus on handling the input of CGI programs. First, where does the input come from, and second, how is the input passed to the server. To write effective CGI programs, you must have a clear understanding of these things.

A bit about HTTP

The server accepts three types of requests: GET, POST, and HEAD. The program request to the web server looks like this:

GET /index.html HTTP / 1.0

The first part, in this case GET, is the request method, the second, index.html, is the requested URL, and the third, HTTP / 1.0, is the protocol used by the client.

The two main request methods are GET and POST. These are the same methods that are available to you when creating a form. The HEAD method is rarely used by the browser because it only requests the response header, and the response body is not sent in this case. For example, to check if the page has changed, the browser may ask for a header, but this does not generate a full communication.

GET method

By default, the request uses the GET method. The POST method is only used when explicitly specified in the form request. It is very important for a CGI programmer to understand that when a GET request is made, the form data is sent to the server along with the URL. Web servers that support CGI copy this data into an environment variable named QUERY_STRING. After that, the CGI program is responsible for getting the data from the environment variable and processing it.

The URL with the query string looks like this:

Http://www.domen-name.com/login.pl?nick=maks&psw=parol

Sign? separates the query string from the actual URL of the resource; nick and psw are variables passed to the server, maks and parol are their values, respectively.

POST method

The POST method is used when explicitly specified in the form's METHOD attribute. Unlike the GET method, POST does not place data in the URL, but in the body of the request. A POST request is a lot like an HTTP response. The first line is a standard HTTP request that specifies the POST method. It may contain the necessary additional headers, separated from the request body by an empty line.

When using the POST method, the request body is passed to the program as standard input.

Choosing between GET and POST

It is clear that when developing forms, the CGI programmer will be faced with the question of which of these methods to use. In most cases, both methods are applicable and both will work well. However, there are situations when the use of one method or another gives certain advantages.

Let's look at a few situations where it makes sense to prefer the GET or POST method.

  • If you want your program to be called by reference, the GET method should be preferred.
  • If you don't want the arguments passed to your program to be written to the server log file, use the POST method. For example, if a form requires a username and password, you probably don't want the names and passwords to be saved in the report file. Also, it is not wise to pass the password as part of the URL.
  • If your form has significant dimensions, for example, it has text boxes with notes and comments, you should use the POST method. Generally speaking, you can use the GET method in this case too, but then you may encounter URL size restrictions that are different for different operating systems and browsers (limited by the size of environment variables). It's easier to use the POST method.
  • If your form contains a file field, use the POST method. In addition, in this case, you need to set the value of the ENCTYPE attribute to multipart / form-data.

Description

The method attribute tells the server about the method of the request.

Syntax

...

The values

The value of the method attribute is case-insensitive. There are two methods - get and post.

Get This method is one of the most common and is designed to get the required information and transfer data in the address bar. Pairs "name = value" are appended in this case to the address after the question mark and separated by an ampersand (the & symbol). The convenience of using the get method is that the address with all parameters can be used repeatedly, saving it, for example, to browser bookmarks, and also changing the parameter values ​​directly in the address bar. post The post method sends data to the server in a browser request. This allows more data to be sent than is available to the get method because it has a 4K limit. Large amounts of data are used in forums, postal services, database populations, file transfers, etc.

Required Attribute

Default value

HTML5 IE Cr Op Sa Fx

FORM tag, method attribute