CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Advanced: Re: [webhay] return value from function: Edit Log



Jasmine
Administrator / Moderator

Jan 1, 2003, 10:31 AM


Views: 1501
Re: [webhay] return value from function

To answer your question....

Code
sub gensid {  
my $sid = '';
my @alpha= qw/ q w e r t y u i o p l k j h g f d s
a m n b v c x z 6 7 8 9 0 5 4 3 2 1 / ;

$sid .= $alpha[ int rand @alpha ] for 1 .. 32;
return $sid;
}

my $sid = gensid();

But I recommend using either [url=http://search.cpan.org/author/SHERZODR/CGI-Session-3.11/]CGI::Session or one of options shown below instead of just using random characters to create session ids (I'm assuming you're creating session ids because of the $sid).

From CGI Programming with Perl:


Code
use Digest::MD5; 

my $md5 = new Digest::MD5;
my $remote = $ENV{REMOTE_ADDR} . $ENV{REMOTE_PORT};
my $id = $md5->md5_base64( time, $$, $remote );
$id =~ tr|+/=|-_.|; # Make non-word characters URL-friendly


or what Apache::Session uses:


Code
use MD5; 

sub generate_id {
return substr(MD5->hexhash(time(). {}. rand(). $$. 'blah'), 0, 16);
}


References:

  • [url=http://perlmonks.org/index.pl?node_id=10102]Re: Simple Session?
  • [url=http://perlmonks.org/index.pl?node_id=36996](Ovid) Re: sessions: MD5 versus random strings


Please note that this question belonged in the Beginners forum -- it is not an Advanced question.


(This post was edited by Jasmine on Jan 1, 2003, 10:31 AM)


Edit Log:
Post edited by Jasmine (Administrator) on Jan 1, 2003, 10:31 AM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives