The rwo code blocks above do exactly that.
No they do not prove anything, you did not show us where that code was, how it was called, nor did you call it in your example.
In the code you just showed us there are many reasons those code segments can be skipped leaving $LoggedOn_user_id set to its initial 0
regarding $LoggedOn_user_id being undef
at
you dont check to see if $session->id() ne $sid. If they are not the same it is because either the $sid never existed or that session has expired. in either of those cases a new session-collection and session-id is created and when you try to use my $username1 = $session->param("user_id"); to set $LoggedOn_user_id = $username1; it will be undef as the following code demonstrates.# $sid = '7032f2c7f5a2c721a483dc75fc29595e'; $session = new CGI::Session("driver:MySQL", $sid, {Handle=>$dbh});
result#!/usr/bin/perl use strict; use warnings; select STDOUT; $| = 1; use CGI; use CGI::Session; my $existn=0; my $sid=make_first(); sleep 10; find_if_exist ($sid); sleep 20; find_if_exist ($sid); find_if_exist('abcd'); exit; sub make_first { my $session = new CGI::Session(undef, undef, {Directory=>'.'}); my $sid=$session->id; $session->expires('+15s'); $session->param('user_id',time); $session->flush(); return $sid; } sub find_if_exist { my $sid0=shift; $existn++; print '******Exist call:'.$existn."\n"; my $session = new CGI::Session(undef, $sid0, {Directory=>'.'}); my $sid=$session->id; my $user=$session->param('user_id'); unless (defined $user) {$user='undef-as-string'; } unless ($sid0 eq $sid) {print "********different "; } print 'sid0:'.$sid0.' sid:'.$sid."\n"; print 'user:'.$user."\n\n"; }
******Exist call:1 sid0:2bd4a7c8d0001e49e97dc332d6bf619e sid:2bd4a7c8d0001e49e97dc332d6bf +619e user:1491953482 ******Exist call:2 ********different sid0:2bd4a7c8d0001e49e97dc332d6bf619e sid:bcd0c394 +67ce09c5b5be9910f9e1798b user:undef-as-string ******Exist call:3 ********different sid0:abcd sid:2dd5bc5bc5e297150b1e4c7f58166135 user:undef-as-string
In reply to Re^20: global var
by huck
in thread global var
by tultalk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |