Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Hello friends. I am on Perl v5.24.3. Running on Windows IIS server. The web site uses Perl CGI. It is a new but simple web site and Perl CGI has worked well so far. During testing I found what appears to me to be an odd error. When a user logins the Perl web page works fine. Sometimes when a user is not logged and goes to the same page (or any other .pl page that requires login to be true, then an error is kicked off. The error is... Use of uninitialized value $session_status ... And I have no idea why I am getting this error when "I think" I am doing this correctly.

This error when run from command line..
Mon Oct 28 21:57:11 2019 template_requires_login_simple2.pl: Use of uninitialized value $session_status in string eq at C:\inetpub\wwwroot\template_requires_login_simple2.pl line 89.

This error from IIS header analysis..
HTTP2_SESSION_RECV_INVALID_HEADER
--> error = "Invalid character in header name."
--> header_name = "mon oct 28 23"<br> --> header_value = "06:35 2019 template_requires_login_simple2.pl: Use of uninitialized value $session_status in string eq at C:\\inetpub\\wwwroot\\template_requires_login_simple2.pl line 89."
I have reviewed the documentation and cannot determine why sometimes an error is produced when the user is not logged in and goes to a page that requires a login. I'd like a user to receive a nice message saying 'please login'. The error never occurs when the user is logged in.

I look forward to your insight.

Thank you,
---Robert

#!/usr/bin/perl ### Setup environment use CGI; use CGI::Carp qw(fatalsToBrowser); use CGI::Session; use CGI::Session qw/-ip-match/; use DBI; use strict; use warnings; $CGI::POST_MAX = 1024 * 1024 * 10; # max 10MB posts $CGI::DISABLE_UPLOADS = 1; # no uploads ######################################################### ### Setup variables my $title = "Template with Login"; my $error_message = ""; my $html = ""; ######################################################### ### Setup variables 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); $session->flush(); ### Grab SID and determine session_status my $sid = $session->id(); my $session_status = "false"; $session_status = $session->param("user_logged_in"); ### Initialize my $x = init($session, $cgi); my $login_trials_count = $session->param("~login-trials"); sub init { my ($session, $cgi) = @_; if ( $session->param("~logged-in") ) { return 1; # Verify if user is not logged. } my $trials = $session->param("~login-trials") || 0; return $session->param("~login-trials", ++$trials); } ############################################### my $profile_lg_name = ""; my @fail_message = ""; ######################################################### ### Fetch info from session profile if ($session_status eq 'true') { $profile_lg_name = $session->param("profile_lg_name"); login_is_ok(); } else { push @fail_message, "Not logged in. Err 200"; login_is_not_ok(); } ######################################################### ### Print HTML document sub login_is_ok { ### The following sets up CGI. $CGI::POST_MAX = 1024 * 1024 * 10; # max 10MB posts $CGI::DISABLE_UPLOADS = 1; # no uploads my $q = CGI->new; my $entry_type = ''; $entry_type = $q->param('entry_type') || 0; $html = " <HEAD> <meta name=\"viewport\" content=\"width=device-width, initial-scal +e=1\"/> <link href=\"style2.css\" rel=\"stylesheet\" type=\"text/css\"> <link rel=\"shortcut icon\" type=\"image/x-icon\" href=\"favicon. +ico\" /> <meta http-equiv=\"refresh\" content=\"7200; URL=logout.pl\" /> <title>$title</title> </head> <h3>$title</h3> Welcome : $profile_lg_name <br> You are now logged in. </BODY> </HTML>"; print $html; $dbh->disconnect; exit(); } sub login_is_not_ok { ### Print HTML document $html = " <HTML> <HEAD> <TITLE>Template</TITLE> <meta http-equiv=\"refresh\" content=\"2\; URL=user_login.html\" /> </HEAD> <BODY> <P> Please log in. Redirecting to log in page. <p> </BODY> </HTML>"; print $html; }

In reply to Perl v5 CGI error Use of uninitialized value $session_status by RedJeep

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2023-02-07 02:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer not to run the latest version of Perl because:







    Results (38 votes). Check out past polls.

    Notices?