Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^3: CGI::Session "cache" issue

by Anonymous Monk
on May 23, 2011 at 19:55 UTC ( [id://906351]=note: print w/replies, xml ) Need Help??


in reply to Re^2: CGI::Session "cache" issue
in thread CGI::Session "cache" issue

Thanks for responding. Firstly note that I broke the code down, excluding un-related parts. I am actually sending the headers only once. Once I have printed the redirect header, I exit the script (exit(1)). Its doing everything fine, its just not registering that i'm logged in, if I visit the page in a particular way (i.e. from the homepage after logging in, or from a redirect index page).

If its doing everything fine, then why is there a problem?

redirect does not print a session cookie

No session cookie, means no session.

Your code fragment is too dense, but I was able to spot the problem (redirect without cookies)

If you follow the guidelines of How do I post a question effectively?, and post a small/short, self-contained program, which demonstrates the bug/problem, undesirable output/behaviour, we can better help you, and you can believe us when we do :)

I have used CGI:Session on many occassions, but never for such a task. I have read the tutorial, as well as the main documentation a few times. From what I gathered, you don't need to pass an SID, unless the user has i.e. cookies disabled. The module should handle everything automatically.

How do you think the module handles everything automatically?

The sid has to get passed somehow.

If you use header method, it goes by way of cookie. CGI::redirect() doesn't know about CGI::Session, so no cookie.

Replies are listed 'Best First'.
Re^4: CGI::Session "cache" issue
by Zhris (Initiate) on May 23, 2011 at 21:37 UTC

    Hello,

    I have followed your advice, and I seem to have got it to work as I expect. This has though confused me entirely. How was it possible that I was even able to login, if I hadn't set the cookie on redirect. And why should it have made any difference whether I called Members.pl directly (displays logged in), compared to calling a php webpage that redirects to Members.pl (displays not logged in). Considering this, I believe my "confused" understanding was justified, unless there is some logical explanation. Just to clarify, I wasn't sure what aspect of my code may have caused the problem, therefore I wanted to provide everything that revolved around CGI::Session.

    Here are the important snippets of my updated "login" code:

    #create session my $session = new CGI::Session(); #create cookie my $session_cookie = new CGI::Cookie( -name => 'CGISESSID', -value => $session->id(), -domain => 'domain.com', ); #redirect print redirect( -cookie => [$session_cookie], -uri => $referer, );

    Thanks for your advice and persistance,

    Chris

      This has though confused me entirely. How was it possible that I was even able to login, if I hadn't set the cookie on redirect. And why...

      Your confusion is understandable.

      Your code is very repetitive; there is zero abstraction.

      This makes maintenance (finding bugs ) very difficult, if not impossible.

      The code is in need of refactoring. What do I mean? See String Calculator TDD Kata done in Perl, its a ~14 minute video of an exercise in refactoring, live, as the programmer types it in.

      Then see a 7minut Introduction to Catalyst , for an easier approach to writing complex CGIs using http://www.catalystframework.org/

        Thank you for those links.

        I found the live coding video very interesting. It was kind of fun to predict what the guy was going to code next, and how it could be improved before he improved it. Although, I believe he could have improved it a little more.

        I have heard of Catalyst before, but have never looked into it. It looks like a really useful framework for my needs.

        I am going to refactor my login code, as I know theres more code than neccessary in its current state.

        You've been more than helpful. In the past, I have posted CGI::Session questions on other forums, and had no replies, and eventually figured it out in my "own" way. I'm greatly appreciative of your speedy help and advice.

        Chris

Log In?
Username:
Password:

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

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

    No recent polls found