Ruby CGI Cookie
The HTTP protocol is stateless. 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 registration process of thewebsite, but make sure that the information filled in before is not lost.
In this case, Cookie
help us solve the problem.
How does Cookie work?
Almost all website designers use Cookie
in website design. Because they all want to provide users who browse the website with a more friendly and cultural browsing environment, and at the same time collect visitors’ information more accurately.
Write and read
The Cookies
collection is attached to the Response
object and Request
object, which needs to be preceded by Response
or Request
.
The syntax used to send Cookies
to clients is usually:
When giving to the non-existent Cookies
collection is set, it is created on the client, if the Cookies
already exists, it will be replaced.
Because Cookies
are sent to the client as part of the header information for HTTP transmission, so the code for sending Cookies
to the client is usually placed before the tags in the HTML file sent to the browser.
If the user wants to read Cookies
, you must use the Request
object’s Cookies
collection, which is used by:
It is important to note that only before the server has downloaded any data to the browser can the browser communicate with Server
carry on Cookies
collection of data exchange, once the browser starts to receive``Server`` downloaded data Cookies
. The data exchange is stopped, and to avoid errors, add response.Buffer=True
.
Properties of the collection
1.``Expires`` attribute: this property is used to give
Cookies
set a deadline within which you can call the saved Cookies as long as you open theweb page. If the deadline has passed,Cookies
is automatically deleted. Such as: settingCookies
valid until April 1, 2004, when it will be automatically deleted. IfCookies
validity period isnot set, the life cycle starts from opening the browser to the end of closing the browser, the life cycle will end after each run, and the next run will start again.2.``Domain`` attribute: this property defines the
Cookies
uniqueness of transmitting data. If only a certainCookies
transmit to``_blank”>`` for Sohu’s home page, you can use the following code:3.``Path`` attribute: defined
Cookies
send only the specified path request, ifPath
property is not set, the default path of the application is used.Secure
attributes: specify inCookies
can it be read by the user.
5.``Name=Value`` :
Cookies
is set and retrieved in the form of key-value pairs.
Dealing with Cookies in Ruby
You can create a file named cookie
and store text information, send theinformation to the browser, and call the CGI.out
to set up the head of cookie
.
Example
#!/usr/bin/rubyrequire"cgi"cgi=CGI.new("html4")cookie=CGI::Cookie.new('name'=>'mycookie','value'=>'Zara
Ali','expires'=>Time.now+3600)cgi.out('cookie'=>cookie)docgi.head+cgi.body{"Cookie
stored"}end
Next, let’s go back to this page and find cookie
value, as follows:
Example
#!/usr/bin/rubyrequire"cgi"cgi=CGI.new("html4")cookie=cgi.
cookies['mycookie']cgi.out('cookie'=>cookie)docgi.head+cgi.body{cookie[0]}end
CGI::Cookie
Object instantiation includes the following parameters:
Parameters. |
Description |
---|---|
Name |
Specifies the name of the cookie. |
Value |
Specifies the value of cookie. |
Expire |
Specify the period of validity of the cookie. |
Path |
Specifies the server path for cookie. |
Domain |
Specifies the domain name of the cookie. |
Secure |
Specifies whether cookie is transmitted over a secure HTTPS connection. |