Ruby CGI Cookies
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 a page during the website registration process, but must ensure that the previously filled in information is not lost.
In this case, Cookie helped us solve the problem very well.
How do cookies work?
Almost all website designers use cookies when designing websites, because they want to provide users who browse the website with a more friendly and humanistic browsing environment, and at the same time, they can also collect more accurately Visitor information.
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 add Response or Request in front.
The syntax used to send Cookies to the client is usually:
When setting a set of Cookies that does not exist, it will be created on the client. If the Cookies already exist, they will be replace. Since Cookies are sent to the client as part of the header information of HTTP transmission, the code for sending Cookies to the client is generally placed before the tag of the HTML file sent to the browser.
If the user wants to read Cookies, he must use the Cookies collection of the Request object. The usage method is: It should be noted that the browser can only communicate with the Server before the server has downloaded any data to the browser. Perform data exchange of Cookies collection. Once the browser starts to receive the data downloaded by the Server, the data exchange of Cookies will stop. In order to avoid errors, response.Buffer=True must be added in front of the program and.
Attributes of the collection
##1.Expires attribute: This attribute is used to set a period for Cookies. During the period, just open the web page. Call the saved cookies. If this period expires, the cookies will be automatically deleted. like: The validity period of cookies is set to April 1, 2004, and they will be automatically deleted at that time. If a cookie does not have a validity period set, its life cycle starts from opening the browser and ends when the browser is closed. The life cycle will end after each run and will start again the next time it runs.
2.Domain attribute: This attribute defines the uniqueness of the data transmitted by Cookies. If you only send certain Cookies to _blank"> Sohu homepage, you can use the following code:
3.Path attribute: defines that Cookies are only sent to For a specified path request, if the Path attribute is not set, the default path of the application software is used
- ##4.Secure attribute:
Specifies whether Cookies can be used. User reads.
##5. Name=Value: - Cookies are set and retrieved in the form of key-value pairs
##.
#Handling Cookies in Ruby
#!/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 we return to this page and find 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
CGI::Cookie object contains the following parameters when instantiated:
Parameters | 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 name of the cookie. |
secure | Specifies whether cookies are transmitted over a secure HTTPS connection. |