<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: session code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 25 Jul 2008 00:09:51 GMT</pubDate>
    <description>DZone Snippets: session code</description>
    <item>
      <title>Run TCPServer as a simple Web server</title>
      <link>http://snippets.dzone.com/posts/show/5345</link>
      <description>A TCPServer accepts incoming TCP connections. Here is a Web server that listens on a given port and returns the time.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;require 'socket'&lt;br /&gt;port = (ARGV[0] || 80).to_i&lt;br /&gt;server = TCPServer.new('localhost', port)&lt;br /&gt;while (session = server.accept)&lt;br /&gt;  puts "Request: #{session.gets}"&lt;br /&gt;  session.print "HTTP/1.1 200/OK\r\nContent-type: text/html\r\n\r\n"&lt;br /&gt;  session.print "&lt;html&gt;&lt;body&gt;&lt;h1&gt;#{Time.now}&lt;/h1&gt;&lt;/body&gt;&lt;/html&gt;\r\n"&lt;br /&gt;  session.close&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;This code was copied from &lt;a href="http://www.rubycentral.com/pickaxe/lib_network.html"&gt;Programming Ruby: The Pragmatic Programmer's Guide&lt;/a&gt; [rubycentral.com] while looking for information on Ruby CGI global variables.</description>
      <pubDate>Thu, 10 Apr 2008 14:05:56 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5345</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>Persistent Rails cookie session</title>
      <link>http://snippets.dzone.com/posts/show/5173</link>
      <description>Session cookies, the Rails-2 kind, are transient because that's safer.  In some applications safety isn't important.  The following makes the session cookies persist for a year.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class ApplicationController &lt; ActionController::Base&lt;br /&gt;  before_filter :update_session_expiration_date&lt;br /&gt;&lt;br /&gt;private&lt;br /&gt;  def update_session_expiration_date&lt;br /&gt;    unless ActionController::Base.session_options[:session_expires]&lt;br /&gt;      ActionController::Base.session_options[:session_expires] = 1.year.from_now&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 25 Feb 2008 12:23:27 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5173</guid>
      <author>remvee (Remco van 't Veer)</author>
    </item>
    <item>
      <title>A caching current user method call</title>
      <link>http://snippets.dzone.com/posts/show/4922</link>
      <description>A simple method that returns the current user and caches the result to reduce database hits.  This assumes you're storing user information in User and that the current user is identified by the session variable user_id.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class ApplicationController &lt; ActionController::Base&lt;br /&gt;  def current_user&lt;br /&gt;    session[:user_id] ? @current_user ||= User.find(session[:user_id]) : nil&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 24 Dec 2007 03:19:52 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4922</guid>
      <author>mgreenly (Michael Greenly)</author>
    </item>
    <item>
      <title>Compress your ActiveRecord sessions</title>
      <link>http://snippets.dzone.com/posts/show/3924</link>
      <description>Using ActiveRecordStore and your sessions are getting too big?  Try this!&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# in environment.rb or some file you require&lt;br /&gt;require 'zlib'&lt;br /&gt;CGI::Session::ActiveRecordStore::Session.class_eval {&lt;br /&gt;  class &lt;&lt; self&lt;br /&gt;    def marshal_with_compression(data)&lt;br /&gt;      Zlib::Deflate.deflate(marshal_without_compression(data))&lt;br /&gt;    end&lt;br /&gt;    def unmarshal_with_compression(data)&lt;br /&gt;      unmarshal_without_compression(Zlib::Inflate.inflate(data))&lt;br /&gt;    end&lt;br /&gt;    alias_method_chain :marshal, :compression&lt;br /&gt;    alias_method_chain :unmarshal, :compression&lt;br /&gt;  end&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;# in migration&lt;br /&gt;def self.up&lt;br /&gt;  change_column :sessions, :data, :binary&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;def self.down&lt;br /&gt;  change_column :sessions, :data, :text&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 27 Apr 2007 16:11:46 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3924</guid>
      <author>seancribbs (Sean Cribbs)</author>
    </item>
    <item>
      <title>Getting the _session_id from SWFUpload (Flash 8 multiple file uploader)</title>
      <link>http://snippets.dzone.com/posts/show/3118</link>
      <description>It appears that Ruby's CGI::Session class will not use the _session_id in the query string when the Request is a POST.&lt;br /&gt;&lt;br /&gt;Normally, a POST-type request occurs when a form is submitted to the server (e.g. a Rails application).  In this scenario, there is an easy workaround since we can send the _session_id as a hidden field.&lt;br /&gt;&lt;br /&gt;With Flash 8, however, there is no way to add a 'hidden field' to the multi-part form data, thus Rails will fail to recognize the _session_id in the query string portion of our request.&lt;br /&gt;&lt;br /&gt;Here is a hackish work-around:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# The following code is a work-around for the&lt;br /&gt;# Flash 8 bug that prevents our multiple file uploader&lt;br /&gt;# from sending the _session_id.  Here, we hack the&lt;br /&gt;# Session#initialize method and force the session_id&lt;br /&gt;# to load from the query string via the request uri. &lt;br /&gt;# (Tested on Lighttpd)&lt;br /&gt;&lt;br /&gt;class CGI::Session&lt;br /&gt;  alias original_initialize initialize&lt;br /&gt;  def initialize(request, option = {})&lt;br /&gt;    session_key = option['session_key'] || '_session_id'&lt;br /&gt;    option['session_id'] =&lt;br /&gt;      request.env_table["REQUEST_URI"][0..-1].&lt;br /&gt;      scan(/#{session_key}=(.*?)(&amp;.*?)*$/).&lt;br /&gt;      flatten.first&lt;br /&gt;    original_initialize(request, option)&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 10 Dec 2006 03:14:32 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3118</guid>
      <author>canadaduane (Duane Johnson)</author>
    </item>
  </channel>
</rss>
