Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

session script

by rjsaulakh (Beadle)
on Jun 08, 2005 at 11:21 UTC ( #464634=perlquestion: print w/ replies, xml ) Need Help??
rjsaulakh has asked for the wisdom of the Perl Monks concerning the following question:

i m trying to maintain session variables. in my script i am successfully able to generate a session file when ever a user enters his login details in the login page . i m also able to successfull generate the cookie containg the session id to be sent to the user browser .

my problem starts from that i am not able to fetch the cookie sent to the user and take the session id from it second problem is that when ever user goes to another page it doesnt reintialize the existing session but creates a new one which should not happen </p.

i want to go step by step so please guide me where i am doing mistake in fetching a cookie and getting sid from it

#!c:\Perl\bin\perl.exe -w use CGI::Carp qw(fatalsToBrowser); use CGI qw/:standard/; use CGI::Cookie; use Data::Dumper; use CGI::Session; my $cgi = new CGI; print $cgi->header; my $sid; ##session id my $session_dir = "C:\\raman"; ## place where CGI Session files are s +tored #here i am just trying to test whether i am able to fetch the cookie #getting success i can do some more things %cookies = fetch CGI::Cookie; $id = $cookies{'sessionID'}->value; print $id ; my $session = new CGI::Session("driver:File", $cgi, {Directory=>$sessi +on_dir}) or die $CGI::Session::errstr; $sid=$session->id(); $cookie = new CGI::Cookie(-name => 'sessionID', -value => $sid, -expires => '+3h' ); print $cgi->header( -cookie=>$cookie); $session->save_param($cgi, ["username", "password"]); $x= $session->save_param($cgi);

Comment on session script
Download Code
Re: session script
by terce (Friar) on Jun 08, 2005 at 11:37 UTC
    Please encapsulate this with an explanation or at least a link to another node - it'll be meaningless to anyone who hasn't been logged in and watching the CB.
Re: session script
by TheStudent (Scribe) on Jun 08, 2005 at 12:05 UTC
    and your question is?


Re: session script
by Fletch (Chancellor) on Jun 08, 2005 at 12:28 UTC

    To quote the bowl of petunias, "Oh no, not again."

    We're looking for people in ATL

Re: session script
by atcroft (Monsignor) on Jun 08, 2005 at 12:48 UTC

    Perhaps the following code segment will suggest something to you. In the segment (which was in my /cgi-bin/ path when testing, thus the path values in the cookies), I set cookies if not previously set, retrieved the cookies' values if they existed, and printed them using Data::Dumper for examination.

    #!/usr/bin/perl use strict; use warnings; use CGI; use CGI::Cookie; use CGI::Session; use Data::Dumper; my $q = new CGI; my $session = new CGI::Session( undef, $q, { Directory => q{/tmp'} } ); $Data::Dumper::Sortkeys = 1; my %cookies = fetch CGI::Cookie; my (@cookielist); my $t = ( !exists( $cookies{settime} ) ) ? time() : $cookies{settime}->value; push @cookielist, new CGI::Cookie( -name => 'test', -path => q{/cgi-bin/}, -value => ( !exists( $cookies{test} ) ? scalar gmtime : $cookies{test}->value ) ); push @cookielist, new CGI::Cookie( -name => 'settime', -path => q{/cgi-bin/}, -value => ( !exists( $cookies{settime} ) ? $t : $cookies{settime}->value ) ); push @cookielist, new CGI::Cookie( -name => 'difftime', -path => q{/cgi-bin/}, -value => time() - $t ); print $q->header( -cookie => \@cookielist ); print qq{<html>\n<body>\n}; printf <<TEST, join ( "<BR>\n", map { $_ . " -&gt; " . $cookies{$_} } sort +keys %cookies ); %s TEST print qq{<pre>\n}; print Data::Dumper->Dump( [ \%cookies, \@cookielist ], [qw(*cookies *cookielist)] ); print qq{\n}; print qq{</pre>\n}; print qq{</body>\n</html>\n};

    Hope that helps.

      the following code poses some syntax error need some help
      printf <<TEST, join ( "<BR>\n", map { $_ . " -&gt; " . $cookies{$_} } sort +keys %cookies ); %s TEST
        #!/usr/bin/perl use strict; use warnings; my %cookie = ( one => 1, two => 2, ); for my $key (sort keys %cookie){ my $value = $cookie{$key}; print "$key $value </br>\n"; }

      <atcroft> thx for ur code but can you add some comments so that i can really understand it better thx again

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://464634]
Approved by tcf03
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (10)
As of 2014-09-03 07:49 GMT
Find Nodes?
    Voting Booth?

    My favorite cookbook is:

    Results (35 votes), past polls