YouTip LogoYouTip

Ruby Cgi Sessions

Ruby CGI Session

Ruby CGI Session

-- Learning is not just about technology, but also about dreams!

Ruby Tutorial

Ruby Advanced Tutorial

Ruby CGI Cookie

Ruby Sending Email – SMTP

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.

Ruby CGI Cookie

Ruby Sending Email – SMTP

iFlytek Star Coding Plan includes free model call quota, DeepSeek, GLM, Kimi, MiniMax, one-stop experience and deployment platform. Configuration Guide Β₯3.9/ month Subscribe Now

Click to Share Notes

Cancel

Write notes...

Image URL

Image Description

Image Size Γ—

Share Notes

  • Nickname Nickname (Required)
  • Email Email (Required)
  • Reference URL Reference URL

Category Navigation

← Ruby Sending EmailRuby Cgi Cookies β†’