Class: Rack::PHPSession

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/php-session.rb

Overview

Exposes a PHP Session in Rack Applications as a Hash

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ PHPSession

Returns a new instance of PHPSession.

Parameters:

  • options (Hash) (defaults to: {})

    the options to configure the middleware

Options Hash (options):

  • :session_name (String) — default: 'PHPSESSID'

    The name of the PHP Session

  • :session_file_path (String) — default: '.'

    The folder where PHP Session files are stored



11
12
13
14
# File 'lib/rack/php-session.rb', line 11

def initialize(app, options = {})
  @app = app
  @options = { :session_name => 'PHPSESSID', :session_file_path => '.' }.merge(options)
end

Instance Method Details

#call(env, options = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rack/php-session.rb', line 16

def call(env, options = {})
  req = Request.new(env)

  session_id = req.cookies[@options[:session_name]]
  session_file = ::File.join(@options[:session_file_path], "sess_#{session_id}")

  if ::File.exists?(session_file)
    contents = ::File.read(session_file)
    env['php.session'] = (contents.nil? || contents.empty?) ? Hash.new : PHP.unserialize(contents)
  end
  
  @app.call(env)
end