Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Ruby CGI Cookies


May 12, 2021 Ruby


Table of contents


Ruby CGI Cookies

The HTTP protocol is a stateless protocol. But for a business website, it needs to keep session information between pages.

If the user needs to jump the page during the website registration process, but also to ensure that the information department filled in before is lost.

Cookies in this case are a good way to help us solve the problem.


How do cookies work?

Almost all web designers use cookies when designing their websites because they want to provide a friendlier, cultural environment for users to browse the site, while also collecting visitors' information more accurately.

Write and read

A cookie collection is a collection of data attached to a Response object and a Request object, which needs to be preceded by a Response or Request.

The syntax used to send cookies to clients is typically:

When you set up a collection of cookies that do not exist, they are created on the client and replaced if the cookie already exists. Since cookies are sent to the client as part of the header information transmitted by HTTP, the code for sending cookies to the client is generally placed before the TAG on the HTML file sent to the browser.

If the user wants to read cookies, the cookie collection of the Request object must be used by: It is important to note that the browser can only exchange cookies with the server until the server has not downloaded any data to the browser, and once the browser starts receiving the data downloaded by the server, the data exchange of cookies stops, in order to avoid errors, to add response to the program and in front of it. B uffer=True。

The property of the collection

  • 1.Expires Property: This property is used to set a period of time for cookies, during which you can call a saved cookie as long as you open the web page, and if you have passed this period the cookie will be automatically deleted. F or example: Set the validity of cookies until 1 April 2004, when they will be automatically deleted. I f a cookie does not have a set expiration date, its life cycle starts when the browser is opened and ends when the browser is closed, the life cycle ends after each run, and the next run starts again.
  • 2.Domain properties: This property defines the uniqueness of cookies to transmit data. I f you only send a cookie to _blank", you can use the following code:
  • 3.Path Properties: Defines that cookies are issued only to specified path requests, and if the Path property is not set, the application's default path is used.
  • 4.Secure properties: Specify whether cookies can be read by the user.
  • 5、Name=Value : Cookies are set and retrieved in the form of key pairs.

Ruby handles cookies

You can create an object called a cookie and store text information, send that information to your browser, and call CGI.out to set the head of the cookie:

#!/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 go back to this page and look for cookie values, as follows:

#!/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: The cookie object is instantiated with the following parameters:

Parameters Describe
name Specify the name of the cookie.
value Specify the value of the cookie.
expire Specify the validity of the cookie.
path Specify the server path for the cookie.
domain The domain name that specifies the cookie.
secure Specify whether cookies are transmitted over a secure HTTPS connection.