stumbler has asked for the wisdom of the Perl Monks concerning the following question:
Hello
I wrote a sample code for trying CGI::Session and it works quite well but I don't understand some aspects of it, which is confusing.
All I am doing is to pass a variable from one page to another (posting to the same script) using CGI::Session. Below is the sample code..
Script
#! /usr/bin/perl -wT use strict; use lib "..."; use CGI qw/:standard :nosticky :delete_all :escapeHTML :html3 :all/; use CGI::Session; use CGI::Carp 'fatalsToBrowser'; use HTML::Template; my $q = CGI -> new(); my $session = CGI::Session->load() or die CGI::Session->errstr; if ( $session->is_expired ) { print $session->header(); print "Your session expired, inevitably!"; exit(0); } elsif ( $session->is_empty ) { $session = new CGI::Session(); } $session->save_param(); ###$session->load_param(); 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(); print $q->header( -cookie => $cookie ); my $template = HTML::Template->new( filename => 'test_cgi_session.tmpl +' ); $template->param( heading => "Test Session" ); $template->param( nextpage => $nextpage ); $template->param( user => $user ); $template->param( sessionid => $sid ); print $template->output();
Template File
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" + /> <title><tmpl_var name=heading></title> </head> <body> <script>javascript:history.forward()</script> <script> function submitform( button ) { alert ( 'button is ' + button ); document.data.button.value = button; document.data.submit(); return false; } </script> <form method="post" action="<tmpl_var name=nextpage>" enctype="applica +tion/x-www-form-urlencoded" name="data"> <input type="hidden" name="button" value="" /> <br> <br> <table border="0" align="center"> <tr><td> <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" /> </tr></td> <tr align="center"> <td><input type="submit" name="submit" value="submit" onclick="return +submitform('submit')" /></td> </tr> </table> <br> <br> </form> </body> </html>
On loading the script, the username textbox is 'blank' and the sessionid textbox has the id that is generated. Once I enter a value in the username textbox and submit, the same name appears in the next screen and a new session id is displayed as well. Everything looks good but my doubts are:-
1) I understand the reason for the generation of new id everytime because I have included '$session->delete()' at the end of the script (if I comment ' $session->delete();' , and execute the script, the session id doesn't change. I presume it would change if I execute the script after 5 minutes as I had set the 'expire' option as '5' )
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 )
3) Also, I am not sure why I don't need '$session->load_param(); ' call?
I am really not sure if I am using CGI::Session correctly, although I am getting the desired results. (The idea is to use CGI::Session instead of passing variables from one page to another as 'hidden variables' )Please help.
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?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Confusion using CGI Session
by shmem (Chancellor) on Dec 07, 2006 at 00:26 UTC | |
by Anonymous Monk on Dec 07, 2006 at 01:54 UTC | |
by stumbler (Acolyte) on Dec 07, 2006 at 12:35 UTC | |
by shmem (Chancellor) on Dec 07, 2006 at 23:19 UTC |