The best Ruby CGI Cookies Tutorial In 2024, In this tutorial you can learn Cookie How does it work?,Ruby processing Cookies,
HTTP protocol is a stateless protocol. But for a business website, it needs to keep session information between different pages.
If the user site registration process needs to jump page, but want to ensure that the information is not lost before filling.
In this case Cookie good to help us solve the problem.
Almost all the web designers during the design of the site use the Cookie, because they want to give the user browsing the site to provide a more friendly, human culture browsing environment, but also to more accurately collect visitor information.
Cookies set belongs to the collection of data objects and Request Response object, you need to precede it with the use of Request or Response.
The syntax for the client to send Cookies usually:
When set to a non-existent Cookies settings will be created on the client, if the Cookies already exist, it will be replaced. Since Cookies are sent as part of a client's HTTP transport header information, it is sent to the client code Cookies usually placed before the tag sent to the browser's HTML file.
Cookies If you want to read, you must use the Request object's Cookies collection, its use is: Note that, not only in the server before downloading any data to the browser, the browser and the Server to exchange data Cookies collection Once the browser starts receiving Server downloaded data, Cookies data exchange is stopped, in order to avoid errors, to add response.Buffer = True in the previous program.
You can create an object called cookie and store text messages, send the information to the browser, call CGI.out set cookie header:
#!/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
Then 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 :: Cookie object contains an instance of the following parameters:
parameter | description |
---|---|
name | It specifies the name of the cookie. |
value | The predetermined value of the cookie. |
expire | Provisions of the cookie. |
path | Provisions cookie server path. |
domain | Provisions of cookie domain. |
secure | Specifies whether connections to transfer cookie over a secure HTTPS. |