Easy Tutorial
❮ Ruby Intro Ruby File Methods ❯

Ruby CGI Cookie

The HTTP protocol is stateless. However, for a commercial website, it needs to maintain session information across different pages.

For example, during the registration process on a website, a user may need to navigate through pages while ensuring that previously filled information is not lost.

In such cases, cookies effectively solve the problem for us.


How Do Cookies Work?

Almost all website designers use cookies when designing websites because they want to provide a more friendly and humane browsing environment for users, and also to collect visitor information more accurately.

Writing and Reading

The Cookies collection is a data collection attached to the Response and Request objects, and needs to be prefixed with Response or Request when used.

The syntax for sending cookies to the client is typically:

When setting for a non-existent Cookies collection, it creates one on the client machine. If the cookie already exists, it will be replaced. Since cookies are part of the HTTP transmission header sent to the client, the code to send cookies to the client is usually placed before the HTML tag of the document sent to the browser.

If a user wants to read cookies, they must use the Cookies collection of the Request object, with the following method: It is important to note that data exchange between the browser and the server through the Cookies collection can only occur before the browser starts receiving data from the server. Once the browser begins to receive data, the data exchange of cookies stops. To avoid errors, set response.Buffer=True before the program.

Collection Properties

-1. Expires Property: This property is used to set an expiration period for cookies. Within this period, the saved cookies can be called whenever the webpage is opened. If this period passes, the cookies are automatically deleted. For example: Setting the validity period of the cookies until April 1, 2004, at which time they will be automatically deleted. If no validity period is set for a cookie, its lifecycle starts when the browser is opened and ends when the browser is closed. Each run will end its lifecycle, and the next run will restart it.

-2. Domain Property: This property defines the uniqueness of data transmission by cookies. If a cookie is to be sent only to the Sohu homepage, the following code can be used:

-3. Path Property: Defines that cookies are sent only to specified path requests. If the Path property is not set, the default path of the application is used.

-4. Secure Property: Specifies whether cookies can be read by users.

-5. Name=Value: Cookies are set and retrieved in the form of key-value pairs.


Handling Cookies in Ruby

You can create a cookie object named 'cookie' and store text information in it, send this information to the browser, and call CGI.out to set the cookie header:

Example

#!/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 look up the cookie value as follows:

Example

#!/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 is instantiated with 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.
❮ Ruby Intro Ruby File Methods ❯