http://qs1969.pair.com?node_id=589535


in reply to Sessions problems

Hi,

The question you should do is: Where am I calling the cookie method? The answer is:
$cookie = $query->cookie( -name => $session->name, -value => $session->id );
So, the next question is... Where is the $query variable created? (That I can't answer as it isn't created in the code you submitted.)

Answer this question, and probably you'll have the solution to you problem.

Note: use strict; use warnings; are your friends.

TheMage
Talking Web

Replies are listed 'Best First'.
Re^2: Sessions problems
by Anonymous Monk on Dec 13, 2006 at 10:47 UTC
    Thanks for the advice. I have now moved the session creation code to after the validation checking on the params that i am giving to the session. I was already using use strict; and have added use warnings; I have also changed the $query variable to $q so that it has the value $q = new CGI, created earlier as suggested by dorward. So the code reads as
    if($ENV{REQUEST_METHOD} eq 'POST') { $q = new CGI; $username = $q->param('user_name'); $pword = $q->pword'pwd'); } ##### validate username and pword ##### my $cgi; #Create a new session $session = new CGI::Session("driver:File", undef, {Directory=> +'./tmp/sessions'}); #Allow cgi-session read the cookie at the next reques $cookie = $q->cookie( -name => $session->name, -value => $session->id ); print $q->header( -cookie=>$cookie); $name = $cgi->param($username); $session->param($username, $name);
    Should the value for $cgi at:
    $name = $cgi->param($username);
    be $q as well or rather my $cgi = new CGI? Currently i have it defined at the beginning of the script as my $cgi; It is not causing an issue as at the moment i am still getting the error "Can't call method "cookie" on an undefined value at" Again i would really appreciate your help and suggestions with this problem thanks

      I presume you only seting $q when it's a POST request to make sure that users only login with POST, but i think you'll still want to have a my $q = CGI->new() since you may want to, say, set $q->cookie or do a $q->header 'location: http://elsewhere.com/' etc regardless of wether the user has posted the form...

      CGI::param 'foo' will give you back foo from get/post-age, and will play nice and give you an undef if the form wasn't posted or that field wasn't supplied.

      If you're not unconditionally putting something in $q then you can't trust $q to be defined and so you should check that there's something there, every time you want to call something on it. It's only safe to do $q->somestuff() if ref $q ie you can only use methods on $q if it is an object (ie if it's a blessed reference) HTH

      @_=qw; ask f00li5h to appear and remain for a moment of pretend better than a lifetime;;s;;@_[map hex,split'',B204316D8C2A4516DE];;y/05/os/&print;
        I actually have one more question with regards to retrieving the value in the cookie from a different cgi file. I am using the sample code form the CPAN tutorial
        $name = $session->param($username); printf "<input type=\"text\" name=\$username\" value=\"%s\" />", $name +;
        Just wondering should as in the creating the session, $name here be defined as my $name ->new CGI() or just my $name also the value that i passed into the session was
        $name = $query->param($username); $session->param($username, $name);
        so in retrieving the cookie am i right to put $username into the "param" field in the line $name = $session->param($username); and into the name field in the line
        printf "<input type=\"text\" name=\$username\" value=\"%s\" />", $name +;
        Thanks again
        Adding a  my $q = CGI->new() seemed to do the trick, thanks for all the help, appreciate it!
      Yes, all the uses of $cgi should be tranformed to $q.

      TheMage
      Talking Web