in reply to Confusion using CGI Session

Have a look (again ;-) at the CGI documentation.
-nosticky

By default the CGI module implements a state-preserving behavior called "sticky" fields. The way this works is that if you are regenerating a form, the methods that generate the form field values will interrogate param() to see if similarly-named parameters are present in the query string. If they find a like-named parameter, they will use it to set their default values.

Sometimes this isn't what you want. The -nosticky pragma prevents this behavior. You can also selectively change the sticky behavior in each element that you generate.

--shmem

_($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                              /\_¯/(q    /
----------------------------  \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

Replies are listed 'Best First'.
Re^2: Confusion using CGI Session
by Anonymous Monk on Dec 07, 2006 at 01:54 UTC

    Thanks. Have I used CGI::Session correctly? Do I have to use $session->load_param(); to retrieve the variables?

Re^2: Confusion using CGI Session
by stumbler (Acolyte) on Dec 07, 2006 at 12:35 UTC

    Update:

    I updated with the '-nosticky' pragma but it still behaves the same way it did earlier. Hence, I tend to think that it is something else other than 'sticky' or 'nosticky' option. Any thoughts?

      Sorry for my completely bogus first reply *blush*

      While it sounds good, it doesn't point you into the right direction.

      CGI::Session encapsulates CGI to store parameters, cookies and so on in a serialized data structure, so you don't need to use both of them. Choose one, use one.

      Maybe you mended your confusion in the meantime - anyways:

      2) I am confused as to how the username is picked up correctly, if I use'$session->delete();' in the script? (I don't see the 'cgisess_............' in the /tmp folder and I am sure it is getting deleted )

      You grab the params before the call to $session->delete()

      my $user = $session->param( "user" ); my $sid = $session->id(); my $cookie = $q->cookie( -name => $session->name(), -value => $sid, -expires => "+5" ); my $nextpage = 'test_cgi_session.pl'; $session->delete();

      pass them to your template

      $template->param( user => $user ); $template->param( sessionid => $sid );

      and have them stuck into the value attribute of your inputs

      <input type="input" name="user" value="<tmpl_var name=user>" /> </tr></td> <tr><td> <input type="input" name="sessionid" value="<tmpl_var name=sessionid>" + size="100" />

      so it would be rather odd for $user not to show up in the text field.

      3) Also, I am not sure why I don't need '$session->load_param(); ' call?

      You need the $session->load_param() call if the only value passed into your cgi is the session ID, and you want to restore the params from the previously stored serialized data structure, i.e. if there wasn't any user name passed in, and you want the username to be passed to subsequent pages nonetheless - just the opposite of what you expected your script to do ;-)

      I am really not sure if I am using CGI::Session correctly

      Those who are sure, aren't ;-)

      Look for the method 'dump' in CGI::Session and include dump($session) in your output to see what's in that session thing.

      --shmem

      _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                    /\_¯/(q    /
      ----------------------------  \__(m.====·.(_("always off the crowd"))."·
      ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}