Hello. I'm new to CGI::Session. I've looked through the documentation but have become stuck. I seem to have a problem with correctly passing session info from one page to another. Your help is appreciated.

I will include code for two pages. I have stripped out most of the code that is not relevant to this question.

login.pl ... on this page I am attempting to setup a new session.

page.pl ... on this page I am attempting to grab that session info to confirm that the user is logged in.

If it matters I am Win 10, IIS, Perl 5, SQLite. The database connections seem to work because I am able to query the database and see new sessions being stored.

My test goes as...

1. Go to login.pl (I have rigged the page to set a login... again trying to solve one problem at a time) and on this page the user_logged_in parameter is set as true.

2. Go to page.pl to see if the session data, particularly user_logged_in parameter was accessed.

Result: When I do this and go to page.pl I see a new Session ID and it does not get the user_logged_in parameter as being set as true.

login.pl
### Setup environment use CGI; use CGI::Carp qw(fatalsToBrowser); use CGI::Session; use CGI::Session qw/-ip-match/; use DBI; use strict; use warnings; my $dbfile = "/inetpub/wwwroot/data/people1.db"; my $dsn = "dbi:SQLite:dbname=$dbfile"; my $user = ""; my $password = ""; my $dbh = DBI->connect($dsn, $user, $password, { PrintError => 0, RaiseError => 1, AutoCommit => 1, FetchHashKeyName => 'NAME_lc', }); ### Setup session data connection my $cgi = new CGI; my $session = CGI::Session->new("driver:sqlite", undef,{DataSource=>'/ +inetpub/wwwroot/data/session_management.db'}) or die (CGI::Session->errstr); my $cookie = $cgi->cookie(CGISESSID => $session->id ); print $cgi->header(-cookie=>$cookie); ### Test set variables # Skip the logic for authenticating a user for testing purposes only. $session->param("user_logged_in", 'true'); ### Grab SID my $sid = $session->id(); ###For testing only. Basically in previous line did I correctly set t +he parameter to a flag a user as logged in. my $session_status = $session->param("user_logged_in"); my $login_trials = init(); sub init { #if ( $session->param("~logged-in") ) { # return 1; # if logged in, don't bother going further # } # if we came this far, the login/psswds do not match # the entries in the database my $trials = $session->param("~login-trials") || 0; return $session->param("~login-trials", ++$trials); } ### Print HTML document my $html = "Content-Type: text/html <HTML> <HEAD> <TITLE>Session Test</TITLE> </HEAD> <BODY> <H4>Create a new session</H4> <P> <p> Session ID: $sid; <p> Session status: $session_status; <p> login_trials: $login_trials; <p> </BODY> </HTML>"; print $html;

page.pl

### Setup environment use CGI; use CGI::Carp qw(fatalsToBrowser); use CGI::Session; use CGI::Session qw/-ip-match/; use DBI; use strict; use warnings; my $dbfile = "/inetpub/wwwroot/data/people1.db"; my $dsn = "dbi:SQLite:dbname=$dbfile"; my $user = ""; my $password = ""; my $dbh = DBI->connect($dsn, $user, $password, { PrintError => 0, RaiseError => 1, AutoCommit => 1, FetchHashKeyName => 'NAME_lc', }); ### Setup session data connection my $cgi = new CGI; my $sid = $cgi->cookie("CGISESSID") || undef; my $session = CGI::Session->new("driver:sqlite", $sid,{DataSource=>'/i +netpub/wwwroot/data/session_management.db'}) or die (CGI::Session->errstr); ### Grab variables from the session my $sid = $session->id(); my $session_status = $session->param("user_logged_in"); my $login_trials = init(); sub init { #if ( $session->param("~logged-in") ) { # return 1; # if logged in, don't bother going further # } # if we came this far, the login/psswds do not match # the entries in the database my $trials = $session->param("~login-trials") || 0; return $session->param("~login-trials", ++$trials); } ### Print HTML document my $html = "Content-Type: text/html <HTML> <HEAD> <TITLE>Session Test</TITLE> </HEAD> <BODY> <H4>Check existing session data and status</H4> <P> <p> Session ID: $sid; <p> Session status: $session_status; <p> login_trials: $login_trials; <p> </BODY> </HTML>"; print $html;

In reply to CGI Session not passing session info between pages by RedJeep

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.