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

Hi,
I have created a new session in one of my form as follows;
$log_ses= new CGI::Session(); $log_ses->param('userid',$user);
I want to access these variable in other forms. And i tried using this
$log_ses = new CGI::Session(); my $usrnm= $log_ses->param('userid'); #This did not work
Anyones help is appreciated..

Thanks in Advance, simy

2006-05-08 Added code tags and retitled by jeffa, as per Monastery guidelines
Original title: 'session variables'

Replies are listed 'Best First'.
Re: How can i access a session(CGI::Session) variable in other forms?
by derby (Abbot) on May 08, 2006 at 17:08 UTC

    Are you saving the sessionid client side? The normal way to do this is by a cookie?

    # send proper HTTP header with cookies: print $session->header();

    -derby
      See also CGI::Session::Tutorial.

      But the answer you have already been given is correct. You need some way to 'transfer' the state from one form to the next, the most common way to do this is by using a cookie to carry just the session-id form form to form. You can, if necessary, do this with query strings, but this is somewhat messier and less transparent.

      By using a framework such as CGI::Application and the session manager plug-in the whole process becomes even more transaprent.

      jdtoronto

        i tried using the "use CGI::Simple::Cookie;" I have tried this way in "form1.cgi"
        $log_ses= new CGI::Session(); $CGISESSID = $log_ses->id(); $cookie1 = new CGI::Simple::Cookie( -name=>'ID', -value=>$CGISESSID );
        In my "form2.cgi" i have used..
        %cookies = fetch CGI::Simple::Cookie; $id = $cookies{'ID'}->value;
        when i try to goto form2.cgi...it gives me the following error in the error log.. Can't call method "value" on an undefined value can someone shoot me an example how to handle cookies for session ids??? thanks, simy
      no i didnt save the session id. should i?

        I'm assuming when you say access these variable in other forms you mean in other cgi scripts (or other invocations of this one). If that's the case then, yes, you need some way to propogate the sessionid across different processes. One way is cookies, another is hidden form fields and another is url-rewritting. Cookies tends to be the easiest but some users get freaked out by cookies deny them from being set. If you have to deal with those users then you need to use one of the other approaches. Have you read through CGI::Session::Tutorial?

        -derby