Ruby CGI Session

CGI::Session can save persistent session state for users and CGI environments. It needs to be closed after the session is used. This ensures that data is written to the storage. When the session is completed, you need to delete the data.


#!/usr/bin/ruby require 'cgi' require 'cgi/session' cgi= CGI.new("html4") sess = CGI::Session.new( cgi , "session_key" => "a_test", "prefix" => "rubysess.") lastaccess = sess["lastaccess"].to_s sess["lastaccess"] = Time.now if cgi['bgcolor'][0] =~ /[az]/ sess["bgcolor"] = cgi[' bgcolor'] end cgi.out{ cgi.html { cgi.body ("bgcolor" => Sess["bgcolor "]){ "The background of this page" + "changes based on the 'bgcolor' " + "each user has in session." + "Last access time: #{lastaccess}" } } }

Access "/cgi-container/test.cgi?bgcolor=red" to hop to the page with the predefined foundation shading.

The session information exists in the transitory record catalog of the server. The prefix parameter indicates the prefix of the session and will be utilized as a prefix for the brief record. Along these lines you can without much of a stretch distinguish distinctive session transitory records on the server.

CGI::Session class

CGI::Session keeps up a tireless condition of client and CGI condition. The session can be in memory or on the hard drive.

Class Method

The Ruby Class CGI::Session gives a simple method to make a session:

CGI::Session::new( cgi[, option])

Enable another CGI session and return the relating CGI::Session object. The alternative can be a discretionary hash, which can be the accompanying qualities:

  • session_key: The key name spare session defaults to _session_id.
  • session_id: Unique session ID. Programmed generation
  • new_session: If genuine, makes another Session id for the present session. Assuming false, utilize the current session ID with session_id. On the off chance that this parameter is precluded, the current session is utilized if accessible, generally another one is made.
  • database_manager: The class used to spare sessions, which can be CGI::Session::FileStore or CGI::Session::MemoryStore. The default is FileStore.
  • tmpdir: For FileStore, the wrong catalog for the session.
  • prefix: For FileStore, the prefix for the session document.

Instantiation method

serial numbermethod description
1[ ]
Returns the estimation of the given key. View the occasion.
2[ ]=
Set the estimation of the given key. View the occasion.
Invoke the erase strategy for the basic database the board. For FileStore, erase the physical document containing the session. For MemoryStore, expel session information from memory.
Invoke the update strategy for the hidden database the board. For FileStore, compose the session to circle. No impact on MemoryStore.

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