Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

CGI Session - Refresh Problem

by d0353101 (Novice)
on Apr 24, 2008 at 09:29 UTC ( [id://682593]=perlquestion: print w/replies, xml ) Need Help??

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

Hi, I am having a plain html page (session.html) with UserName and password and SUBMIT button. After pressing SUBMIT button, it will open a page (query.cgi). On this page I am creating a session and writing the cookie.
2 Separate problems :
-----------
1. Cookies are not being written in Cookies folder.
2. If I press refresh button on browser for "query.cgi" it creates a new session. I want the same session after refresh button is pressed. Please helpme to achieve this. ( I dont want to use cookie)
###############- START Code for (session.html) -################
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML><HEAD> <TITLE> Modify User </TITLE> </HEAD> <BODY> <form name="modifyuser" action="query.cgi" method="get" > <BR> User id : <input type="text" name="lg_name" size=30> Password<input type="text" name="lg_password" size=30> <input type="submit" value="SUBMIT" > </form> </BODY> </HTML>

###############- END Code for (session.html) -################
###############- START Code for (query.cgi) -################
<BR> #!/perl/bin/perl -wT use CGI qw(:standard); use CGI::Carp qw(warningsToBrowser fatalsToBrowser); use strict; use DBI; use vivek::session; use CGI::Session; use CGI; my $cgi = new CGI; print $cgi->header; print $cgi->start_html("query.cgi"); my $sid; my $session; my $f_name; my $lg_psswd ; my $lg_name = $cgi->param("lg_name"); print "<BR> lg_name --- $lg_name <BR>"; $session = new CGI::Session(undef, $cgi, {Directory=>'/tmp'}); my $cookie = $cgi->cookie(CGISESSID => $session->id ); print "cookies is : $cookie <BR>" ; print $cgi->header(-cookie=>$cookie); $sid = $session->id(); print "<BR>sid = $sid <BR>"; print $cgi->end_html;

###############- END Code for ((query.cgi) -##################
OUT PUT FROM QUERY.CGI : http://localhost/~administrator/Session_managemnt/sm_15apr/query.cgi?lg_name=asas&lg_password=asasa
######################################################
lg_name --- asas cookies is : CGISESSID=f459ae140d01a8ed184d33c689816399; path=/ Set-Cookie: CGISESSID=f459ae140d01a8ed184d33c689816399; path=/ Date: T +hu, 24 Apr 2008 09:13:42 GMT Content-Type: text/html; charset=ISO-8859-1 sid = f459ae140d01a8ed184d33c689816399

######################################################

Replies are listed 'Best First'.
Re: CGI Session - Refresh Problem
by rhesa (Vicar) on Apr 24, 2008 at 10:37 UTC
    You only get one chance to print the HTTP headers, so your second print $cgi->header isn't going to be effective.

    The solution is to first create/retrieve the session, then bake the cookie, and only then print the header:

    #!/perl/bin/perl -wT use strict; use CGI; use CGI::Session; my $cgi = CGI->new; my $session = CGI::Session->new( undef, $cgi, {Directory=>'/tmp'} +); my $cookie = $cgi->cookie(CGISESSID => $session->id ); print $cgi->header(-cookie=>$cookie); print $cgi->start_html("query.cgi"); print "cookies is : $cookie <BR>" ; my $sid = $session->id(); print "<BR>sid = $sid <BR>"; print $cgi->end_html;
      Dear Rhesa, Many thanks for making this correction !! It worked like wonder.

      It resolved one query but still same Old questions are there:
      1. I am having administrator privilleges. On which path cookies are being stored. Cookies are not being stored at "C:\Documents and Settings\Administrator\Cookies"

      2. ( I do NOT want to use cookie)
      If I press refresh button on browser for "query.cgi" it creates a new session. I want the same session after refresh button is pressed. ( I do NOT want to use cookie) Please help me to achieve this.
        Those questions are answered in the manual, as well as in the excellent tutorial.

        1. You set the directory where sessions are stored with the Dir parameter;
        2. Instead of printing $cgi->header(...), you can also use $session->header(). That contains the logic for whether you need a new cookie or not.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://682593]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2024-04-25 22:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found