home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / xampp / xampp-perl-addon-1.4.9-installer.exe / Incr.pm < prev    next >
Encoding:
Perl POD Document  |  2002-11-27  |  3.4 KB  |  138 lines

  1. package CGI::Session::ID::Incr;
  2.  
  3. # $Id: Incr.pm,v 3.1 2002/11/27 12:26:05 sherzodr Exp $
  4.  
  5. use strict;
  6. use File::Spec;
  7. use Carp "croak";
  8. use Fcntl (':DEFAULT', ':flock');
  9.  
  10. use vars qw($VERSION);
  11.  
  12. ($VERSION) = '$Revision: 3.1 $' =~ m/Revision:\s*(\S+)/;
  13.  
  14. sub generate_id {
  15.     my ($self, $options) = @_;
  16.  
  17.     my $IDFile = $options->[1]->{IDFile} or croak "Don't know where to store the id";
  18.     my $IDIncr = $options->[1]->{IDIncr} || 1;
  19.     my $IDInit = $options->[1]->{IDInit} || 0;
  20.  
  21.     unless (sysopen(FH, $IDFile, O_RDWR|O_CREAT, 0644) ) {
  22.         $self->error("Couldn't open IDFile=>$IDFile: $!");
  23.         return undef;
  24.     }
  25.     unless (flock(FH, LOCK_EX) ) {
  26.         $self->error("Couldn't lock IDFile=>$IDFile: $!");
  27.         return undef;
  28.     }
  29.     my $ID = <FH> || $IDInit;
  30.     unless ( seek(FH, 0, 0) ) {
  31.         $self->error("Couldn't seek IDFile=>$IDFile: $!");
  32.         return undef;
  33.     }
  34.     unless ( truncate(FH, 0) ) {
  35.         $self->error("Couldn't trunated IDFile=>$IDFile: $!");
  36.         return undef;
  37.     }
  38.     $ID += $IDIncr;
  39.     print FH $ID;
  40.     unless ( close(FH) ) {
  41.         $self->error("Couldn't close IDFile=>$IDFile: $!");
  42.         return undef;
  43.     }
  44.  
  45.     return $ID;
  46. }
  47.  
  48.  
  49. 1;
  50.  
  51. =pod
  52.  
  53. =head1 NAME
  54.  
  55. CGI::Session::ID::Incr - CGI::Session ID driver
  56.  
  57. =head1 SYNOPSIS
  58.  
  59.     use CGI::Session qw/-api3/;
  60.  
  61.     $session = new CGI::Session("id:Incr", undef,
  62.                             {   Directory   => '/tmp',
  63.                                 IDFile      => '/tmp/cgisession.id',
  64.                                 IDInit      => 1000,
  65.                                 IDIncr      => 2 });
  66.  
  67. =head1 DESCRIPTION
  68.  
  69. CGI::Session::ID::Incr is to generate auto incrementing Session IDs. Compare it with CGI::Session::ID::MD5, where session ids are truely random 32 character long strings.
  70.  
  71. CGI::Session::ID::Incr expects the following arguments passed to CGI::Session->new() as the third argument
  72.  
  73. =over 4
  74.  
  75. =item "IDFile"
  76.  
  77. Location where auto incremened IDs are stored. This attribute is required.
  78.  
  79. =item "IDInit"
  80.  
  81. 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 0.
  82.  
  83. =item "IDIncr"
  84.  
  85. 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 'IDIncr' to '10'. Default is 1.
  86.  
  87. =back
  88.  
  89. =head1 COPYRIGHT
  90.  
  91. Copyright (C) 2002 Sherzod Ruzmetov. All rights reserved.
  92.  
  93. This library is free software. You can modify and distribute it under the same terms as Perl itself.
  94.  
  95. =head1 AUTHOR
  96.  
  97. Sherzod Ruzmetov <sherzodr@cpan.org>
  98.  
  99. Feedbacks, suggestions and patches are welcome.
  100.  
  101. =head1 SEE ALSO
  102.  
  103. =over 4
  104.  
  105. =item *
  106.  
  107. L<MD5|CGI::Session::ID::MD5> - MD5 ID generator
  108.  
  109. =item *
  110.  
  111. L<CGI::Session|CGI::Session> - CGI::Session manual
  112.  
  113. =item *
  114.  
  115. L<CGI::Session::Tutorial|CGI::Session::Tutorial> - extended CGI::Session manual
  116.  
  117. =item *
  118.  
  119. L<CGI::Session::CookBook|CGI::Session::CookBook> - practical solutions for real life problems
  120.  
  121. =item *
  122.  
  123. B<RFC 2965> - "HTTP State Management Mechanism" found at ftp://ftp.isi.edu/in-notes/rfc2965.txt
  124.  
  125. =item *
  126.  
  127. L<CGI|CGI> - standard CGI library
  128.  
  129. =item *
  130.  
  131. L<Apache::Session|Apache::Session> - another fine alternative to CGI::Session
  132.  
  133. =back
  134.  
  135.  
  136. =cut
  137.  
  138.