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

Hello,

I'm starting to learn Dancer web framework. One of the first things I'm testing is sessions. The sessions are created in the session folder, but every time I access to the web page, It creates a new one and the visits number doesn't increase.

My code is:

get '/usuario' => sub { my $number_visits; if ( not session('visits') ) { session visits => 1; $number_visits = 1; } else { $number_visits = int ( session('visits') ) + 1; session visits => $number_visits; } template 'user_get' => { num_of_visits => $number_visits, }; };

Can you see what I'm doing wrong?

Thank you

Replies are listed 'Best First'.
Re: Dancer Sessions
by CountZero (Bishop) on Feb 28, 2011 at 13:44 UTC
    Which session-engine are you using?

    Update: I have tried your script in a simple Dancer test application and it works fine using the YAML session engine.

    I even tried opening multiple sessions (one in FireFox, one in IE) and the page-visit counters in each browser were kept independent of each other without any problem.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: Dancer Sessions
by locked_user sundialsvc4 (Abbot) on Feb 28, 2011 at 20:16 UTC

    If sessions keep reappearing ... no matter how or why ... the basic problem is that the session information is not being successfully sent to the browser and then returned by the browser with the next request.   (So the server always thinks that every request is new.)   A look at the actual HTML transfers, e.g. using a client-side debugger on the web-browser, ought to quickly reveal the problem.