sub check_session {

	use DBI;
	use CGI::Session;
	use CGI::Cookie;

	my %cookies   = fetch CGI::Cookie ;
	my $cgisessid = defined $cookies{'CGISESSID'} ? $cookies{'CGISESSID'}->value : undef; # This does

	use db ;
	&db_open_ro ;

	$userid       = 0 ;
	$session      = load CGI::Session("driver:MySQL", $cgisessid, {Handle=>$dbh});
	$userid       =    $session->param(-name=>'id');
	$is_logged_in =    $session->param(-name=>'_IS_LOGGED_IN');
	$username     = lc $session->param(-name=>'uname');
	$useremail    = lc $session->param(-name=>'uemail');
	$usertype     = lc $session->param(-name=>'utype');
	$orgids       =    $session->param(-name=>'oids');
	$regids       =    $session->param(-name=>'rids');
	$custids      =    $session->param(-name=>'cids');

	my  @orgids   = split(/\,/,$orgids);
	foreach (@orgids) { $glob_orgids{$userid}{$_} = 1 ; }

	my  @regids   = split(/\,/,$regids);
	foreach (@regids) { next unless $_ > 0 ; $glob_regids{$userid}{$_} = 1 ; }

	my  @custids   = split(/\,/,$custids);
	foreach (@custids) { next unless $_ > 0 ; $glob_custids{$userid}{$_} = 1 ; }

	$sessionid = $session->id();

	if ($sessionid && $is_logged_in > 0 && $userid > 0) { # if $userid and $is_logged_in is -1 not allowed in  
		$session_state = 'active' ;  
	} elsif ($session->is_expired) {
		$session_state = 'expired' ;
		$session->delete() ;
	} elsif ($session->is_empty) {
		$session_state = 'empty' ;    
	} else {
		$session_state = 'empty' ;   
		$session->delete() ;
	}

	&db_close_conn ;

} #------------------------------------------------------------------------------------------

1 ;