Ruby CGI Cookie
The HTTP protocol is a stateless protocol. But for a commercial website, it needs to maintain session information between different pages.
For example, when a user needs to navigate between pages during the registration process on a website, it must ensure that previously filled-in information is not lost.
In this case, Cookies help us solve the problem very well.
How do Cookies work?
Almost all website designers use Cookies when designing websites, because they all want to provide users browsing the website with a friendlier, humanized browsing environment, and at the same time collect visitors' information more accurately.
Writing and Reading
The Cookies collection is a data collection attached to the Response object and the Request object. When using it, you need to prefix it with Response or Request.
The syntax used to send Cookies to the client is usually:
When setting a Cookies collection that does not exist, it is created on the client machine. If the Cookies already exists, it will be replaced. Since Cookies are sent to the client as part of the HTTP transmission header information, the code for sending Cookies to the client is generally placed before the `<HTML>` tag of the HTML file sent to the browser.
If the user wants to read Cookies, they must use the Cookies collection of the Request object. The usage is: Note that the browser can only exchange Cookies collection data with the Server before the server has downloaded any data to the browser. Once the browser starts receiving data downloaded by the Server, the Cookies data exchange stops. To avoid errors, add `response.Buffer=True` before the program and the `<HTML>` tag.
Collection Properties
- 1. Expires property:This property is used to set a period for Cookies. Within the period, as long as the web page is opened, the saved Cookies can be called. If this period is exceeded, the Cookies are automatically deleted. For example: Set the validity period of Cookies to April 1, 2004, and they will be automatically deleted then. If a Cookie does not have a validity period set, its lifecycle starts when the browser is opened and ends when the browser is closed. After each run, the lifecycle ends, and the next run starts anew.
- 2. Domain property:This property defines the uniqueness of the data transmitted by Cookies. If you only want to send a Cookie to the Sohu homepage, you can use the following code:
- 3. Path property:It defines that Cookies are only sent to requests for the specified path. If the Path property is not set, the default path of the application software is used.
- 4. Secure property: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 named cookie and store text information, send that information to the browser, and call CGI.out to set the cookie header:
Example
Next, we return to this page and look up the cookie value, as shown below:
Example
When the CGI::Cookie object is instantiated, it contains the following parameters:
| Parameter | Description |
|---|---|
| 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 of the cookie. |
| domain | Specifies the domain of the cookie. |
| secure | Specifies whether the cookie is transmitted over a secure HTTPS connection. |