English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The HTTP protocol is a stateless protocol. However, for a commercial website, it needs to maintain session information between different pages.
If the user needs to jump pages during the website registration process, but also needs to ensure that the information filled in before is not lost.
In this case, Cookie has very well solved our problem.
Almost all website designers use Cookie during website design because they all want to provide a more user-friendly and humanized browsing environment for users visiting the website, while also being able to collect visitor information more accurately.
The Cookies collection is an attribute of the Response object and Request object, and it needs to be prefixed with Response or Request when used.
The syntax for sending Cookies to the client machine is usually: }}
When setting for a non-existing Cookies collection, it will be created on the client machine. If the Cookies already exists, it will be replaced. Since Cookies are sent to the client machine as part of the HTTP header information, the code to send Cookies to the client machine is generally placed before the tag in 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 method of use is: It should be noted that the exchange of Cookies collection data between the browser and the Server can only be done before any data is downloaded to the browser by the Server. Once the browser starts receiving the data downloaded by the Server, the exchange of Cookies data stops. To avoid errors, response.Buffer=True should be added to the program and before.
1.Expires attribute :This attribute is used to set a time limit for Cookies. As long as the web page is opened within the time limit, the saved Cookies can be called. If the time limit is exceeded, the Cookies will be automatically deleted. For example: Set the expiration date of Cookies to2004Year4Month1Day, it will be automatically deleted. If a Cookies does not have an expiration date, its lifetime will start from opening the browser and end when the browser is closed. The lifetime will end after each run, and it will start anew the next time it runs.
2.Domain attribute :This attribute defines the uniqueness of the data transmitted by Cookies. If only a certain Cookies is sent to the_blank">Sohu homepage, you can use the following code:
3.Path attribute :It defines that Cookies are only sent to the specified path requests. If the Path attribute is not set, the default path of the application software is used.
4.Secure attribute :Specify whether Cookies can be read by the user.
5Name=Value : Cookies are set and retrieved in the form of key-value pairs.
You can create an object named cookie to store text information, send this information to the browser, and set the cookie header using CGI.out:
#!/usr/bin/ruby require "cgi" cgi = CGI.new("html4") cookie = CGI::Cookie.new('name' => 'mycookie', 'value' => 'Zara Ali', 'expires' => Time.now + 3600) cgi.out('cookie' => cookie) do cgi.head + cgi.body { "Cookie stored" } end
Next, let's return to this page and search for the cookie value, as shown below:
#!/usr/bin/ruby require "cgi" cgi = CGI.new("html4") cookie = cgi.cookies['mycookie'] cgi.out('cookie' => cookie) do cgi.head + cgi.body { cookie[0] } end
The CGI::Cookie object contains the following parameters when instantiated:
Parameters | Description |
---|---|
name | Specify the name of the cookie. |
value | Specify the value of the cookie. |
expire | Specify the expiration time of the cookie. |
path | Specify the server path of the cookie. |
domain | Specify the domain of the cookie. |
secure | Specify whether cookies are transmitted through a secure HTTPS connection. |