Ruby CGI Session
-- Learning is not just about technology, but also about dreams!
- Home
- HTML
- JavaScript
- CSS
- Vue
- React
- Python3
- Java
- C
- C++
- C#
- AI
- Go
- SQL
- Linux
- VS Code
- Bootstrap
- Git
- Local Bookmarks
Ruby Tutorial
- Ruby Tutorial
- Ruby Introduction
- Ruby Environment
- Ruby Installation - Linux
- Ruby Installation - Windows
- Ruby Chinese Encoding
- Ruby Command Line Options
- Ruby Environment Variables
- Ruby Syntax
- Ruby Data Types
- Ruby Classes and Objects
- Ruby Class Case Study
- Ruby Variables
- Ruby Operators
- Ruby Comments
- Ruby Conditional Statements
- Ruby Loops
- Ruby Methods
- Ruby Blocks
- Ruby Modules
- Ruby Strings
- Ruby Arrays
- Ruby Hashes
- Ruby Date & Time
- Ruby Ranges
- Ruby Iterators
- Ruby File I/O
- Ruby File Class and Methods
- Ruby Dir Class and Methods
- Ruby Exceptions
Ruby Advanced Tutorial
- Ruby Object-Oriented
- Ruby Regular Expressions
- Ruby Database Access - DBI Tutorial
- Ruby Mysql
- Ruby CGI Programming
- Ruby CGI Methods
- Ruby CGI Cookie
- Ruby CGI Session
- Ruby Sending Email - SMTP
- Ruby Socket Programming
- Ruby XML, XSLT and XPath Tutorial
- Ruby Web Service
- Ruby Multithreading
- Ruby JSON
- Ruby RubyGems
Ruby CGI Session
CGI::Session can maintain persistent session state for users and the CGI environment. Sessions need to be closed after use to ensure data is written to storage. After the session is complete, you need to delete this data.
Example
#!/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.to_s
sess = Time.now
if cgi['bgcolor'] =~ //
sess = cgi['bgcolor']
end
cgi.out{
cgi.html {
cgi.body("bgcolor" => sess){
"The background of this page" +
"changes based on the 'bgcolor'" +
"each user has in session." +
"Last access time: #{lastaccess}"
}
}
}
Visiting "/cgi-bin/test.cgi?bgcolor=red" will redirect to a page with the specified background color.
Session data is stored in the server's temporary file directory. The prefix parameter specifies the session prefix, which will be used as the prefix for temporary files. This allows you to easily identify different session temporary files on the server.
CGI::Session Class
CGI::Session maintains persistent state between the user and the CGI environment. Sessions can be in memory or on the hard disk.
Class Methods
The Ruby class CGI::Session provides a simple method to create a session:
CGI::Session::new(cgi[, option])
Starts a new CGI session and returns the corresponding CGI::Session object. The option is an optional hash that can have the following values:
- session_key: The key name to save the session. Defaults to _session_id.
- session_id: A unique session ID. Auto-generated.
- new_session: If true, creates a new session ID for the current session. If false, uses an existing session identifier via session_id. If omitted, uses an existing session if available, otherwise creates a new one.
- database_manager: The class used to store sessions. Can be CGI::Session::FileStore or CGI::Session::MemoryStore. Defaults to FileStore.
- tmpdir: For FileStore, the directory for storing session files.
- prefix: For FileStore, the prefix for session files.
Instance Methods
| No. | Method Description |
|---|---|
| 1 | Returns the value for the given key. See example. |
| 2 | = Sets the value for the given key. See example. |
| 3 | delete Calls the delete method of the underlying database manager. For FileStore, deletes the physical file containing the session. For MemoryStore, removes the session data from memory. |
| 4 | update Calls the update method of the underlying database manager. For FileStore, writes the session to disk. For MemoryStore, has no effect. |
Click to Share Notes
Write notes...
Image URL
Image Description
Share Notes
- Nickname Nickname (Required)
- Email Email (Required)
- Reference URL Reference URL
YouTip