Ruby CGI Cookie

The HTTP convention is a stateless convention. In any case, for a business site, it needs to keep up session data between various pages.

On the off chance that the client needs to hop to the page amid the site enlistment process, yet additionally to guarantee that the data filled in before isn't lost.

For this situation, the treat is beneficial for us to tackle the issue.

How does Cookie work?

Practically all web specialists use treats when structuring their sites, since they need to give a more easy to use and socially vivid perusing condition for clients perusing the site, just as progressively exact accumulation of guests' data.

Write and read

The Cookies gathering is an accumulation of information appended to the Response object and the Request object. It should be gone before by a Response or Request.

The linguistic structure for sending treats to customers is usually:

At the point when set to a non-existing treat gathering, it will be made at the customer, and if the treat as of now exists, it will be supplanted. Since the treat is sent to the customer as a major aspect of the HTTP transport header, the code to send the treat to the customer is ordinarily set before the tag of the HTML record sent to the program.

In the event that the client needs to peruse the treats, they should utilize the Cookie gathering of the Request object. The use technique is: Note that the program can trade information with the treat accumulation just before the server has downloaded any information to the program. When the program begins to get the information downloaded by the server, the information trade of the treats is halted. To keep away from blunders, add response.Buffer=True to the program and before it.

Set properties

  • 1.Expires attribute: This ascribe is utilized to set a due date for treats. You can call the spared treats as long as you open the website page inside as far as possible. On the off chance that this period terminates, the treats will be erased consequently. For example, Treats are set to lapse on April 1, 2004 and will be consequently erased. On the off chance that a treat does not have a substantial period, its life cycle begins from opening the program and finishes when the program is shut. The existence cycle will finish after each run and the following run will begin once more.
  • 2.Domain attribute: This trait characterizes the uniqueness of the treat to exchange information. In the event that you just pass a treat to the _blank"> Sohu landing page, you can utilize the accompanying code:
  • 3.Path property: characterizes that the treat is just sent to the predetermined way demand. In the event that the Path property isn't set, the default way of the application is utilized.
  • 4.Secure Attributes: Specifies whether treats can be perused by the client.
  • 5, Name=Value: Cookies are set and recovered as key-esteem sets.

  • Handling Cookies in Ruby

    You can make an article considered treat and store the content data, send it to the program, and call CGI.out to set the header 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('cookies' => cookies) do cgi.head + cgi.body { "Cookie Stored" } end

    Next we go back to this page and look for the cookie value as follows:


    #!/usr/bin/ruby require "cgi" cgi= CGI.new("html4") cookie = cgi.cookies['mycookie'] cgi.out('cookies' => cookies) do cgi.head + cgi.body { cookie[ 0] } end

    CGI::Cookie objects are instantiated with the following parameters:

    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 for the cookie.
    domain specifies the domain name of the cookie.
    secure specifies whether cookies are transmitted over a secure HTTPS connection.

welookups is optimized for learning.© welookups. 2018 - 2019 All Right Reserved and you agree to have read and accepted our term and condition.