Visit Session data exists in the server’s temporary file directory Ruby class Class Enable a new CGI session and return the corresponding CGI::Session object. The option can be an optional hash with the following values: Serial number Method description 1 2 3 4
CGI::Session
Persistent session state can be saved for the user and the CGI environment, and the session needs to be closed after use, which ensuresthat the data is written to the storage, and you need to delete the data when the session is complete. 6.39.1. Example #
#!/usr/bin/rubyrequire'cgi'require'cgi/session'cgi=CGI.new("html4")sess=CGI::Session.new(cgi,
"session_key"=>"a_test","prefix"=>"rubysess.")lastaccess=sess["lastaccess"].to_ssess["lastaccess"]=
Time.nowifcgi['bgcolor'][0]=~/[a-z]/sess["bgcolor"]=cgi['bgcolor']endcgi.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}"} } }
"/cgi-bin/test.cgi?bgcolor=red"
Jumps to the page that specifies the background color.
prefix
parameter specifies the prefix for the session and will be used as the prefix for the temporary file. This way you can easily identify different session temporary files on the server.CGI::Session class #
CGI::Session
maintains a persistent state between the user and the CGI environment. The session can be in memory or on the hard drive.Class method #
CGI::Session
provides a simple way to create
session
:CGI::Session::new(cgi[,option])
session_key
:Key name save session defaults to
\_session_id
.
session_id
:Unique session ID Automatic generation
new_session
:If the
true
To create a new for the current session
Session
id
. If for
false
, through
session_id
use existing
session
identification. If this parameter is omitted, an existing session is used if available, otherwise a new one is created.
database_manager
:For Savin
sessions
can be a class of
CGI::Session::FileStore
or
CGI::Session::MemoryStore
. Default is
FileStore
.
tmpdir
: For
FileStore
, is storage directory for
session
.
prefix
: For
FileStore
, is the prefix of the
session
file.Instantiation method #
[
]
Returns the value of the given key. View the instance.
[
]=
Sets the value of the given key. View the instance.
delete
Call the delete method of the underlying database management. ForFileStore, delete the physical file that contains session. For MemoryStore,remove session data from memory.
update
Call the update method of the underlying database management. ForFileStore, write the session to disk. It has no effect on MemoryStore.