lib/CPAN/lib/CGI/Session/ID/incr.pm
author Colas Nahaboo <colas@nahaboo.net>
Sat, 26 Jan 2008 15:50:53 +0100
changeset 0 414e01d06fd5
permissions -rw-r--r--
RELEASE 4.2.0 freetown
     1 package CGI::Session::ID::incr;
     2 
     3 # $Id: incr.pm 351 2006-11-24 14:16:50Z markstos $
     4 
     5 use strict;
     6 use File::Spec;
     7 use Carp "croak";
     8 use Fcntl qw( :DEFAULT :flock );
     9 use CGI::Session::ErrorHandler;
    10 
    11 $CGI::Session::ID::incr::VERSION = '4.20';
    12 @CGI::Session::ID::incr::ISA     = qw( CGI::Session::ErrorHandler );
    13 
    14 
    15 sub generate_id {
    16     my ($self, $args) = @_;
    17 
    18     my $IDFile = $args->{IDFile} or croak "Don't know where to store the id";
    19     my $IDIncr = $args->{IDIncr} || 1;
    20     my $IDInit = $args->{IDInit} || 0;
    21 
    22     sysopen(FH, $IDFile, O_RDWR|O_CREAT, 0666) or return $self->set_error("Couldn't open IDFile=>$IDFile: $!");
    23     flock(FH, LOCK_EX) or return $self->set_error("Couldn't lock IDFile=>$IDFile: $!");
    24     my $ID = <FH> || $IDInit;
    25     seek(FH, 0, 0) or return $self->set_error("Couldn't seek IDFile=>$IDFile: $!");
    26     truncate(FH, 0) or return $self->set_error("Couldn't truncate IDFile=>$IDFile: $!");
    27     $ID += $IDIncr;
    28     print FH $ID;
    29     close(FH) or return $self->set_error("Couldn't close IDFile=>$IDFile: $!");
    30     return $ID;
    31 }
    32 
    33 
    34 1;
    35 
    36 __END__;
    37 
    38 =pod
    39 
    40 =head1 NAME
    41 
    42 CGI::Session::ID::incr - CGI::Session ID driver
    43 
    44 =head1 SYNOPSIS
    45 
    46     use CGI::Session;
    47     $session = new CGI::Session("id:Incr", undef, {
    48                                 Directory   => '/tmp',
    49                                 IDFile      => '/tmp/cgisession.id',
    50                                 IDInit      => 1000,
    51                                 IDIncr      => 2 });
    52 
    53 =head1 DESCRIPTION
    54 
    55 CGI::Session::ID::incr is to generate auto incrementing Session IDs. Compare it with L<CGI::Session::ID::md5|CGI::Session::ID::md5>, where session ids are truly random 32 character long strings. CGI::Session::ID::incr expects the following arguments passed to CGI::Session->new() as the third argument.
    56 
    57 =over 4
    58 
    59 =item IDFile
    60 
    61 Location where auto incremented IDs are stored. This attribute is required.
    62 
    63 =item IDInit
    64 
    65 Initial value of the ID if it's the first ID to be generated. For example, if you want the ID numbers to start with 1000 as opposed to 0, that's where you should set your value. Default is C<0>.
    66 
    67 =item IDIncr
    68 
    69 How many digits each number should increment by. For example, if you want the first generated id to start with 1000, and each subsequent id to increment by 10, set I<IDIncr> to 10 and I<IDInit> to 1000. Default is C<1>.
    70 
    71 =back
    72 
    73 =head1 LICENSING
    74 
    75 For support and licensing information see L<CGI::Session|CGI::Session>
    76 
    77 =cut