in reply to Re^3: How to keep an object between HTTP requests?
in thread How to keep an object between HTTP requests?

I tried to use CGI::Session together with CGI for keeping CustomObject between web page updates:

/cgi-bin/session:

#!/usr/bin/perl -wT use strict; use warnings; use CGI; use CGI::Session; use CustomObject; my $cgi = new CGI; my $session = new CGI::Session("driver:File", $cgi, {Directory => "/tm +p"}); print $cgi->header(-type => "text/html", -charset => "utf-8"), $cgi->start_html("Session management"); # Initialize session object. unless ($session->param("custom_object")) { my $customObject = new CustomObject("test_login", "test_password") +; $session->param("custom_object", $customObject); print $cgi->p("Custom object initialized"); } my $customObject = $session->param("custom_object"); print $cgi->p("Login: " . $customObject->login()), $cgi->p("Password: " . $customObject->password()), $cgi->end_html();

/usr/lib/perl5/site_perl/CustomObject.pm:

package CustomObject; sub login { my $self = shift; return $self->{LOGIN}; } sub new { my ($className, $login, $password) = @_; my $self = bless { LOGIN => $login, PASSWORD => $password }, $className; return $self; } sub password { my $self = shift; return $self->{PASSWORD}; } 1;

All I get is that CustomObject is instantiated each web page update and the /tmp directory is populated with cgisess_* files.

Replies are listed 'Best First'.
Re^5: How to keep an object between HTTP requests?
by Herkum (Parson) on Feb 15, 2010 at 18:12 UTC

    Let me ask you a simple question, how is your session ID being stored on the web page?

    I don't see your session ID being stored as a cookie, nor as a hidden variable anywhere. How is the web page supposed to know which session you are referring too?

    Figure out how to manage your session ID on the web page and then use that ID to retrieve a specific session. Then you will be alright.

Re^5: How to keep an object between HTTP requests?
by Corion (Patriarch) on Feb 15, 2010 at 10:02 UTC

    Are you sure that your session works without your object?

      I use the object instantiation exactly to find out if the session is kept between page updates which I can't see. What should I do?

        Print debug information to your log file (via warn for example) whenever you find an existing session and when you create a new session. Inspect the HTTP traffic to see whether the cookies get sent back and forth. Check whether your browser has stale cookies or block cookies.