Ruby CGI Cookie
The HTTP protocol is a stateless protocol. But for a commercial website, it needs to maintain session information between different pages.
If the user needs to jump to the page during the website registration process, but also to ensure that the information filled in before is not lost.
In this case, the cookie is very good for us to solve the problem.
How does Cookie work?
Write and read
The Cookies collection is a collection of data attached to the Response object and the Request object. It needs to be preceded by a Response or Request.
The syntax for sending cookies to clients is usually:
When set to a non-existing cookie collection, it will be created at the client, and if the cookie already exists, it will be replaced. Since the cookie is sent to the client as part of the HTTP transport header, the code to send the cookie to the client is typically placed before the tag of the HTML file sent to the browser.
If the user wants to read the cookies, they must use the Cookie collection of the Request object. The usage method is: Note that the browser can exchange data with the cookie collection only before the server has downloaded any data to the browser. Once the browser starts to receive the data downloaded by the server, the data exchange of the cookies is stopped. To avoid errors, add response.Buffer=True to the program and before it.
- 1.Expires attribute: This attribute is used to set a deadline for cookies. You can call the saved cookies as long as you open the webpage within the time limit. If this period expires, the cookies will be deleted automatically. Such as: Cookies are set to expire on April 1, 2004 and will be automatically deleted. If a cookie does not have a valid period, its life cycle starts from opening the browser and ends when the browser is closed. The life cycle will end after each run and the next run will start again.
- 2.Domain attribute: This attribute defines the uniqueness of the cookie to transfer data. If you only pass a cookie to the _blank"> Sohu homepage, you can use the following code:
- 3.Path property: defines that the cookie is only sent to the specified path request. If the Path property is not set, the default path of the application is used.
- 4.Secure Attributes: Specifies whether cookies can be read by the user.
- 5, Name=Value: Cookies are set and retrieved in the form of key-value pairs.
Handling Cookies in Ruby
You can create an object called cookie and store the text information, send it to the browser, and call CGI.out to set the header of the cookie:
Next we go back to this page and look for the cookie value as follows:
CGI::Cookie objects are instantiated with the following parameters:
|name||specifies the name of the cookie.|
|value||specifies the value of the cookie.|
|expire||specifies the validity period of the cookie.|
|path||specifies the server path for the cookie.|
|domain||specifies the domain name of the cookie.|
|secure||specifies whether cookies are transmitted over a secure HTTPS connection.|