in reply to How can i access a session(CGI::Session) variable in other forms?

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
  • Comment on Re: How can i access a session(CGI::Session) variable in other forms?
  • Download Code

Replies are listed 'Best First'.
Re^2: How can i access a session(CGI::Session) variable in other forms?
by jdtoronto (Prior) on May 08, 2006 at 18:37 UTC
    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
        You might have better success were you to read the documents; a procedure which this node suggests you may have missed. In fact, one could almost conclude from your snippets that there may be a few little points about the relationship between variables and files that has escaped your recall.

        You may also have posed your latest question whilst entertaining a mis-impression of the Monastery: the monks love to help folks learn, but they do NOT comprise a free-coding service.

        But you see you didn't read the documentation. I don't know how CGI::Simple::Cookie works. In fact it doesn't does it? When I look at the documentation for CGI::Simple I see code which is very different to what you have shown us here.

        You errors are very basic. For example, in Form 2 you do not create a valid CGI::Simple object, nor do you attempt to read the cookie correctly. I suggested earlier that you read the documentation for one very sinmple reason, there is simple explanatory code with copious explanatory notes. I do not believe that the monastery was established so that we could run one on one basic programming classes for people who prefer not to RTFM.

        Please go and read the documentation and the tutorial and if you have a further issue and you have tried the sample code, THEN come back and ask a question.

        jdtoronto

Re^2: How can i access a session(CGI::Session) variable in other forms?
by simy (Initiate) on May 08, 2006 at 17:24 UTC
    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
        yes i want to access them in other CGI scripts.so you mean to say that i need to use cookies to persist the session id. I will try that... Thank you very much