Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

CGI::Session small problem...

by pedrete (Novice)
on Feb 24, 2013 at 13:04 UTC ( #1020396=perlquestion: print w/replies, xml ) Need Help??
pedrete has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks...

May i ask for 10nanoseconds of your wisdom, please???

I am having a small problem with CGI::Session and i am stuck... (for 3 days now)

I have an html page (login.html) that asks for user and password. When entered, checks for them, if they are correct then it creates a sesion and launches checks if the session is valid. if so, then it shows the webpage but if not, it redirects to login.html

The problem is that it seems that the session created by $sesion=new CGI::Session(); is always empty when checked by . why???

Curiously, if the user enters the process twice (with correct credentials of course) then it works!!!
#!/usr/bin/perl use warnings; use CGI; use CGI::Session; $cgi=new CGI; if (CredentialsOK($user,$pwd)){ $sesion=new CGI::Session(); $sesion->param('userlogged',$user); print $sesion->header(-location=>""); $sesion->flush(); } else { print $cgi->redirect(-uri=>"login.html"); }
#!/usr/bin/perl use warnings; use CGI; use CGI::Session; use HTML::Template; $cgi=new CGI; $sesion=CGI::Session->load($cgi); $plantilla=HTML::Template->new(filename => 'web.html'); $sesion=CGI::Session->load(); if (($sesion->is_expired) || ($sesion->is_empty)) { print $cgi->redirect(-uri=>"login.html"); } else { print $cgi->header(); print $plantilla->output(); }

Thanks to all in advance!!


Replies are listed 'Best First'.
Re: CGI::Session small problem...
by moritz (Cardinal) on Feb 24, 2013 at 13:12 UTC

      Thanks MOtirz

      I think the cookie gets to the user browser when i use the line
      print $sesion->header(-location=>"");

      And in fact i have checked and the cookie in the browser is ok... ans so in the server /tmp folder...



        You should monitor the HTTP requests with a browser extension such as LiveHTTPHeaders or just capture the network traffic with Ethereal. Paste the whole conversation from the first request (login.html) to the one hitting; we could perhaps figure out what's wrong. (Use a dummy username + password.)

Re: CGI::Session small problem...
by fishmonger (Chaplain) on Feb 24, 2013 at 17:17 UTC

    Why are you attempting to load the session twice?

    Remove the second call to CGI::Session->load() and add the strict pragma and fix the var declarations as needed (i.e., declare them with the 'my' keyword)

      Another suggestion to try is put your $sesion->header(...) statement after the flush statement

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1020396]
Approved by moritz
[robby_dobby]: marto: and, I'm not sure I'd be around here long enough for YAPC::EU
[marto]: Corion nice, what are you plans for Christmas?
[robby_dobby]: Corion: Nice, you just won't be around to deal with the mess - enjoy your vacation :-)
[robby_dobby]: marto: Sure, will do. Thanks!
[Corion]: marto: No great plans - I'll meet with my sister, my brother and my mother, but that's all :)
[Corion]: ... and also a visit to my godson+family of course :)
Corion orders some gifts online :)

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (7)
As of 2017-12-15 10:41 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (431 votes). Check out past polls.