RedJeep has asked for the wisdom of the Perl Monks concerning the following question:
### 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;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: CGI Session not passing session info between pages
by bliako (Abbot) on Apr 01, 2018 at 09:32 UTC | |
by bliako (Abbot) on Apr 02, 2018 at 09:49 UTC | |
by poj (Abbot) on Apr 02, 2018 at 12:21 UTC | |
|
Re: CGI Session not passing session info between pages
by poj (Abbot) on Apr 01, 2018 at 14:14 UTC | |
|
Re: CGI Session not passing session info between pages
by RedJeep (Sexton) on Apr 02, 2018 at 03:07 UTC | |
| A reply falls below the community's threshold of quality. You may see it by logging in. |