# phpman > man > CGI::Session::Driver

## NAME
    [CGI::Session::Driver](https://www.chedong.com/phpMan.php/perldoc/CGI%3A%3ASession%3A%3ADriver/markdown) - [CGI::Session](https://www.chedong.com/phpMan.php/perldoc/CGI%3A%3ASession/markdown) driver specifications

## SYNOPSIS
        require [CGI::Session::Driver](https://www.chedong.com/phpMan.php/perldoc/CGI%3A%3ASession%3A%3ADriver/markdown);
        @ISA = qw( [CGI::Session::Driver](https://www.chedong.com/phpMan.php/perldoc/CGI%3A%3ASession%3A%3ADriver/markdown) );

## DESCRIPTION
    [CGI::Session::Driver](https://www.chedong.com/phpMan.php/perldoc/CGI%3A%3ASession%3A%3ADriver/markdown) is a base class for all [CGI::Session](https://www.chedong.com/phpMan.php/perldoc/CGI%3A%3ASession/markdown)'s native drivers. It also documents
    driver specifications for those willing to write drivers for different databases not currently
    supported by [CGI::Session](https://www.chedong.com/phpMan.php/perldoc/CGI%3A%3ASession/markdown).

## WHAT IS A DRIVER
    Driver is a piece of code that helps [CGI::Session](https://www.chedong.com/phpMan.php/perldoc/CGI%3A%3ASession/markdown) library to talk to specific database engines,
    or storage mechanisms. To be more precise, driver is a .pm file that inherits from
    [CGI::Session::Driver](https://www.chedong.com/phpMan.php/perldoc/CGI%3A%3ASession%3A%3ADriver/markdown) and defines retrieve(), store() and remove() methods.

  BLUEPRINT
    The best way of learning the specs is to look at a blueprint of a driver:

        package [CGI::Session::Driver::your_driver_name](https://www.chedong.com/phpMan.php/perldoc/CGI%3A%3ASession%3A%3ADriver%3A%3Ayourdrivername/markdown);
        use strict;
        use base qw( [CGI::Session::Driver](https://www.chedong.com/phpMan.php/perldoc/CGI%3A%3ASession%3A%3ADriver/markdown) [CGI::Session::ErrorHandler](https://www.chedong.com/phpMan.php/perldoc/CGI%3A%3ASession%3A%3AErrorHandler/markdown) );

        sub init {
            my ($self) = @_;
            # optional
        }

        sub DESTROY {
            my ($self) = @_;
            # optional
        }

        sub store {
            my ($self, $sid, $datastr) = @_;
            # Store $datastr, which is an already serialized string of data.
        }

        sub retrieve {
            my ($self, $sid) = @_;
            # Return $datastr, which was previously stored using above store() method.
            # Return $datastr if $sid was found. Return 0 or "" if $sid doesn't exist
            }

        sub remove {
            my ($self, $sid) = @_;
            # Remove storage associated with $sid. Return any true value indicating success,
            # or undef on failure.
        }

        sub traverse {
            my ($self, $coderef) = @_;
            # execute $coderef for each session id passing session id as the first and the only
            # argument
        }

        1;

    All the attributes passed as the second argument to [CGI::Session](https://www.chedong.com/phpMan.php/perldoc/CGI%3A%3ASession/markdown)'s new() or load() methods will
    automatically be made driver's object attributes. For example, if session object was initialized
    as following:

        $s = [CGI::Session](https://www.chedong.com/phpMan.php/perldoc/CGI%3A%3ASession/markdown)->new("driver:your_driver_name", undef, {Directory=>'/tmp/sessions'});

    You can access value of 'Directory' from within your driver like so:

        sub store {
            my ($self, $sid, $datastr) = @_;
            my $dir = $self->{Directory};   # <-- in this example will be '/tmp/sessions'
        }

    Optionally, you can define "init()" method within your driver to do driver specific global
    initialization. "init()" method will be invoked only once during the lifecycle of your driver,
    which is the same as the lifecycle of a session object.

    For examples of "init()" look into the source code of native [CGI::Session](https://www.chedong.com/phpMan.php/perldoc/CGI%3A%3ASession/markdown) drivers.

## METHODS
    This section lists and describes all driver methods. All the driver methods will receive driver
    object ($self) as the first argument. Methods that pertain to an individual session (such as
    "retrieve()", "store()" and "remove()") will also receive session id ($sid) as the second
    argument.

    Following list describes every driver method, including its argument list and what step of
    session's life they will be invoked. Understanding this may help driver authors.

### retrieve
        Called whenever a specific session is requested either via "[CGI::Session](https://www.chedong.com/phpMan.php/perldoc/CGI%3A%3ASession/markdown)->new()" or
        "[CGI::Session](https://www.chedong.com/phpMan.php/perldoc/CGI%3A%3ASession/markdown)->load()" syntax. Method should try to retrieve data associated with $sid and
        return it. In case no data could be retrieved for $sid 0 (zero) or "" should be returned.
        undef must be returned only to signal error. Error message should be set via set_error(),
        which can be inherited from [CGI::Session::ErrorHandler](https://www.chedong.com/phpMan.php/perldoc/CGI%3A%3ASession%3A%3AErrorHandler/markdown).

        Tip: set_error() always returns undef. Use it for your advantage.

### store
        Called whenever modified session data is to be stored back to disk. This happens whenever
        [CGI::Session](https://www.chedong.com/phpMan.php/perldoc/CGI%3A%3ASession/markdown)->flush() is called on modified session. Since [CGI::Session](https://www.chedong.com/phpMan.php/perldoc/CGI%3A%3ASession/markdown)->DESTROY() calls
        flush(), store() gets requested each time session object is to be terminated.

        " store() " is called both to store new sessions and to update already stored sessions. It's
        driver author's job to figure out which operation needs to be performed.

        $datastr, which is passed as the third argument to represents already serialized session
        data that needs to be saved.

        store() can return any true value indicating success or undef on failure. Error message
        should be passed to set_error()

### remove
        Called whenever session data is to be deleted, which is when [CGI::Session](https://www.chedong.com/phpMan.php/perldoc/CGI%3A%3ASession/markdown)->delete() is
        called. Should return any true value indicating success, undef on failure. Error message
        should be logged in set_error().

### traverse
        Called only from within [CGI::Session](https://www.chedong.com/phpMan.php/perldoc/CGI%3A%3ASession/markdown)->find(). Job of traverse() is to call \&coderef for
        every single session stored in disk passing session's id as the first and only argument:
        "$coderef->( $sid )"

### init
        Optional. Called whenever driver object is to be initialized, which happens only once during
        the lifecycle of [CGI::Session](https://www.chedong.com/phpMan.php/perldoc/CGI%3A%3ASession/markdown) object. Here you can do driver-wide initialization, such as to
        open connection to a database server.

    DESTROY($self)
        Optional. Perl automatically calls this method on objects just before they are to be
        terminated. This gives your driver chance to close any database connections or close any
        open file handles.

  NOTES
    *   All driver .pm files must be lowercase!

    *   DBI-related drivers are better off using [CGI::Session::Driver::DBI](https://www.chedong.com/phpMan.php/perldoc/CGI%3A%3ASession%3A%3ADriver%3A%3ADBI/markdown) as base, but don't have
        to.

## BACKWARDS COMPATIBILITY
    Version 4.0 of [CGI::Session](https://www.chedong.com/phpMan.php/perldoc/CGI%3A%3ASession/markdown)'s driver specification is NOT backward compatible with the previous
    specification. If you already have a driver developed to work with the previous version you're
    highly encouraged to upgrade your driver code to make it compatible with the current version.
    Fortunately, current driver specs are a lot easier to adapt to.

    For support information see [CGI::Session](https://www.chedong.com/phpMan.php/perldoc/CGI%3A%3ASession/markdown)

## LICENSING
    For support and licensing see [CGI::Session](https://www.chedong.com/phpMan.php/perldoc/CGI%3A%3ASession/markdown).

