Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

session problem

by bigup401 (Pilgrim)
on Nov 25, 2014 at 09:53 UTC ( #1108328=perlquestion: print w/replies, xml ) Need Help??

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

i have this in my

$id = $session->id(); $session->param('logged_in', $username); $session->expire('+10m'); print "Location:$id\n\n";

i have this in my

$sid = $cgi->param('sid'); $session = CGI::Session->load($sid); if($session->is_expired) { print $cgi->redirect(""); print $cgi->header(); } elsif($session->is_empty) { print $cgi->redirect(""); print $cgi->header(); }

both and works very well but when i try to place same session code to other page inside, it just redirects me to login, it doesn't read session sid like eg like if i place session code to when i click to, it just redirects me to login and the session is still valid not expired

Replies are listed 'Best First'.
Re: session problem
by Corion (Patriarch) on Nov 25, 2014 at 10:39 UTC

    So, what does $cgi->param('sid') return in

    Have you tried debugging what your scripts think what is in the session? Most likely some value in your input data is not what you think it should be. As you already have a working example in and an example that does not wwork in, it should be easy to find out what does differently from Maybe the input to is different from the input to

    You could also consider storing the session information in a cookie instead of passing the session around as an URL parameter. Most of the common "easy" frameworks like Mojolicious and Dancer already do that for you. With them, you also don't need to split up your application into separate programs for each different page.

    Also note that in HTTP, a redirect should always be an absolute URL, not a relative URL. So you should better use Location: instead of Location:

      the thing is this, no problem with and has set the session, so welcome returns a string a983c8302e7a678a2e53c65e8bd3316 its a generated sid from to and looks like this, so that shows session has been set and generated session sid,

      so i want to retrieve same generated session sid to other pages

        So you've established that and work as you think they should.

        Have you done the same for

        As and seem to work differently, there must be some difference in what the scripts receive and/or in what they send back.

        Have you tried copying the content of over to see whether the difference is still there?

        How do you pass the session id from (say) to

Re: session problem
by Eily (Monsignor) on Nov 25, 2014 at 10:40 UTC

    You may want to start with CGI::Session::Tutorial, for exemple you could let CGI::Session handle session IDs by itself (even in the parameter list). And maybe letting CGI::Session handle the whole thing through cookies would just work for you.

    How do you "click" on ? There's not link in the code you provided. And do you know what triggers the redirection, either an empty or an expired session?

    All we see with your code here, is that you try to redirect from to (why just print the HTTP Location header field yourself when you could use CGI::redirect like in BTW?), and from to, that's neither helpful nor clear :). And your issue probably doesn't come from that bit of code.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2022-05-26 17:52 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (93 votes). Check out past polls.