http://www.perlmonks.org?node_id=462880

rjsaulakh has asked for the wisdom of the Perl Monks concerning the following question:

i have tried to create a module which when used stores session variables

please let me know if i am wrong some where in my coding

package raman; use strict; use CGI::Carp qw(fatalsToBrowser); #die "Bad error here"; use CGI; use CGI::Cookie; use CGI::Session; my $cgi = new CGI; use lib "/usr/random/directory"; our @ISA = qw(Exporter); my $session_dir = "C:\\raman"; use constant SESSION_COOKIE => "MY_SITE_SIDE"; my $sid = $cgi->cookie(SESSION_COOKIE) || $cgi->param("sid") || undef; my $session = new CGI::Session("driver:File", $cgi, { + Directory=>$session_dir } ) #incase of any error the session close giving an error or die $CGI::Session::errstr; my $cookie = $cgi->param(-name => SESSION_COOKIE, -value => $session->id, #sessiion expires after 3 hours -expires=>"+3h"); $cgi->header(-cookie=>$cookie); #print $cgi->header; #print "Your session id is ", $session->id(); # A cookie is being created which is then being send to the user brows +er #$session->param('username', 'raman'); #$session->save_param($cgi, ["username", "password"]); #print $session->param('username'); #prints raman # this line saves all the available/accessible CGI params my $x= $session->save_param($cgi); 1;

Replies are listed 'Best First'.
Re: session module
by Joost (Canon) on Jun 02, 2005 at 12:42 UTC

      my module is running fine but there are errors
      1.when i use my module in cgi program i am not able to the retrieve username where as if i paste the script in the cgi program i am able to retrieve the username

      note

      the module contains the same script which i am pasting . i just made a new module of it so that i can call it easily where i need session variables

      2.where ever i use my module in a cgi script it creates a new sesssion instead of tracking for the existing one

      please let me know if i am making any mistake or my approac is wrong

      do i use cookies to accomplish the same

      module is like this

      package raman; use CGI::Carp qw(fatalsToBrowser); #die "Bad error here"; use CGI; use CGI::Cookie; use CGI::Session; $cgi = new CGI; our @ISA = qw(Exporter); my $session_dir = "C:\\raman"; use constant SESSION_COOKIE => "MY_SITE_SIDE"; $sid = $cgi->cookie(SESSION_COOKIE) || $cgi->param("sid") || undef; $session = new CGI::Session("driver:File", $cgi, { + Directory=>$session_dir } ) #incase of any error the session close giving an error or die $CGI::Session::errstr; $cookie = $cgi->param(-name => SESSION_COOKIE, -value => $session->id, #sessiion expires after 3 hours -expires=>"+3h"); $cgi->header(-cookie=>$cookie); #print $cgi->header; #print "Your session id is ", $session->id(); # A cookie is being created which is then being send to the user brows +er #$session->param('username', 'raman'); #$session->save_param($cgi, ["username", "password"]); #print $session->param('username'); #prints raman # this line saves all the available/accessible CGI params $x= $session->save_param($cgi); 1;

      the script is like this

      #!c:\Perl\bin\perl.exe -w use strict; use CGI::Carp qw(fatalsToBrowser); #die "Bad error here"; use CGI; use CGI::Cookie; use Data::Dumper; use CGI::Session; my $cgi = new CGI; my $session_dir = "C:\\raman"; #Generating a new session #use constant SESSION_COOKIE => "MY_SITE_SIDE"; my $sid = $cgi->cookie(SESSION_COOKIE) || $cgi->param("sid") || undef; my $session = new CGI::Session("driver:File", $cgi, { + #giving the directory where the session var +iables will be stored Directory=>$session_dir } ) #incase of any error the session close giving an error or die $CGI::Session::errstr; my $cookie = $cgi->param(-name => SESSION_COOKIE, -value => $session->id, #sessiion expires after 3 hours -expires=>"+3h"); $cgi->header(-cookie=>$cookie); #print $cgi->header; #print "Your session id is ", $session->id(); # A cookie is being created which is then being send to the user brows +er $session->param('username', 'raman'); #$session->save_param($cgi, ["username", "password"]); #print $session->param('username'); #prints raman # this line saves all the available/accessible CGI params my $x= $session->save_param($cgi);
        You must be mistaken - neither the script, nor the module will actually create a persistent session.

        Since I've already explained to you two times how you could make a working session with 1/4 of the code you're using, and I've pointed you to an alternative method that will do almost all the work for you, I can't imagine that anything I'll tell you will help you in the least, since you don't appear to read anything anybody tells you.

        So, I'll tell you - again - to look at your previous threads and read the replies there . Please try to use your concentration while you do that - copy & pasting random lines of code is not the same as trying to understand a reply.

Re: session module
by bart (Canon) on Jun 02, 2005 at 12:45 UTC
    I've said it in the Chatterbox before you even started, and I'm saying it again, now: Do not use lexical (my'ed) variables, you can't access them outside of this (module) file. And I think that's one reason why you're even writing this module.
Re: session module
by PodMaster (Abbot) on Jun 02, 2005 at 12:40 UTC
    A package statement doesn't make it a module. Module's generally have subroutines and/or global variables other than @ISA.

    Your code will probably (see CGI.pm for caveats) save some CGI params the first time you use raman.

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: session module
by jpeg (Chaplain) on Jun 02, 2005 at 12:52 UTC
    Just a thought, but I doubt /usr/random/directory and C:\raman are both going to exist on the same computer.

    --
    jpg
      You'd be surprised ;-) On my test pc Perl is installed in g:\usr\bin\perl in order for my shebangs to be universal.


      Remember rule one...
Re: session module
by perl_devel (Friar) on Jun 02, 2005 at 13:19 UTC
    Check if the File created with extension .pl or .pm
    and there are some typos not sure. please convey the proper message and errors so that it can be addressed.
    Regards
    P.B.Sathish Kumar
Re: session module
by thcsoft (Monk) on Jun 02, 2005 at 13:14 UTC
    honestly: your code is ugly.

    1.) there is no need for using 'lib' without a really powerful reason. your's is none.
    2.) if you like to inherit from Exporter, you better use it.
    3.) $cgi->cookie(SESSION_COOKIE) is rubbish. btw.: what the made you think to establish a constant for a cookie's ... err what? value?

    i'd really suggest that before you dig deeper into all that miraculous CGI stuff you take a look at mod_perl and Apache2. what you are trying to construct is at least old-fashioned.

    language is a virus from outer space.