Apache2-ASP

 view release on metacpan or  search on metacpan

lib/Apache2/ASP/SessionStateManager.pm  view on Meta::CPAN


package Apache2::ASP::SessionStateManager;

use strict;
use warnings 'all';
use base 'Ima::DBI';
use Digest::MD5 'md5_hex';
use Storable qw( freeze thaw );
use HTTP::Date qw( time2iso str2time );
use Scalar::Util 'weaken';


#==============================================================================
sub new
{
  my ($class, %args) = @_;
  
  my $s = bless {}, $class;
  my $conn = $s->context->config->data_connections->session;
  
  local $^W = 0;
  __PACKAGE__->set_db('Main',
    $conn->dsn,
    $conn->username,
    $conn->password
  );
  
  # Prepare our Session:
  if( my $id = $s->parse_session_id() )
  {
    if( $s->verify_session_id( $id ) )
    {
      $s->{SessionID} = $id;
      return $s->retrieve( $id );
    }
    else
    {
      $s->{SessionID} = $s->new_session_id();
      $s->write_session_cookie();
      return $s->create( $s->{SessionID} );
    }# end if()
  }
  else
  {
    $s->{SessionID} = $s->new_session_id();
    $s->write_session_cookie();
    return $s->create( $s->{SessionID} );
  }# end if()
}# end new()


#==============================================================================
sub context
{
  $Apache2::ASP::HTTPContext::ClassName->current;
}# end context()


#==============================================================================
sub parse_session_id
{
  my ($s) = @_;
  
  my $cookiename = $s->context->config->data_connections->session->cookie_name;

  no warnings 'uninitialized';
  if( my ($id) = $ENV{HTTP_COOKIE} =~ m/$cookiename\=([a-f0-9]+)/ )
  {
    return $id;
  }
  elsif( ($id) = $s->context->r->headers_in->{Cookie} =~ m/$cookiename\=([a-f0-9]+)/ )
  {
    return $id;
  }
  else
  {
    return;
  }# end if()
}# end parse_session_id()


#==============================================================================
# Returns true if the session exists and has not timed out:
sub verify_session_id
{

lib/Apache2/ASP/SessionStateManager.pm  view on Meta::CPAN

sub DESTROY
{
  my $s = shift;
  
  delete($s->{$_}) foreach keys(%$s);
}# end DESTROY()

1;# return true:

__END__

=pod

=head1 NAME

Apache2::ASP::SessionStateManager - Base class for Session State Managers.

=head1 SYNOPSIS

Within your ASP script:

  <%
    $Session->{counter}++;
    $Response->Write("You have viewed this page $Session->{counter} times.");
  %>

=head1 DESCRIPTION

The global C<$Session> object is an instance of C<Apache2::ASP::SessionStateManager>
or one of its subclasses.

It is a blessed hash that is persisted to a database.  Use it to share information across all requests for
one user.

B<NOTE:> - do not store database connections or filehandles within the C<$Session> object because they cannot be shared across
different processes or threads.

=head1 METHODS

=head2 save( )

Stores the Session object in the database.  Returns true.

=head1 CONFIGURATION

=head2 XML Config

The file C<apache2-asp-config.xml> should contain a section like the following:

  <?xml version="1.0"?>
  <config>
    ...
    <data_connections>
      ...
      <session>
        <manager>Apache2::ASP::SessionStateManager::MySQL</manager>
        <cookie_name>session-id</cookie_name>
        <cookie_domain>.example.com</cookie_domain>
        <dsn>DBI:mysql:dbname:localhost</dsn>
        <username>sa</username>
        <password>s3cr3t!</password>
        <session_timeout>30</session_timeout>
      </session>
      ...
    </data_connections>
    ...
  </config>

=head2 Database Storage

The database named in the XML config file should contain a table like the following:

  CREATE TABLE  asp_sessions (
    session_id    char(32) NOT NULL,
    session_data  blob,
    created_on    datetime default NULL,
    modified_on   datetime default NULL,
    PRIMARY KEY  (session_id)
  ) ENGINE=InnoDB DEFAULT CHARSET=latin1

=head1 BUGS

It's possible that some bugs have found their way into this release.

Use RT L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Apache2-ASP> to submit bug reports.

=head1 HOMEPAGE

Please visit the Apache2::ASP homepage at L<http://www.devstack.com/> to see examples
of Apache2::ASP in action.

=head1 AUTHOR

John Drago <jdrago_999@yahoo.com>

=head1 COPYRIGHT AND LICENSE

Copyright 2007 John Drago, All rights reserved.

This software is free software.  It may be used and distributed under the
same terms as Perl itself.

=cut



( run in 0.723 second using v1.01-cache-2.11-cpan-39bf76dae61 )