Your version seems to make the most sense to me so far...
I need a few things explained though.
These are the lines:
my $user = $justloggedin;
my $newcookie = $query->cookie(
-name => 'arbitrarycookiename',
-value => $user,
-expires => '+15m'); #no activity in 15min...gone!
my $user = $query->cookie('arbitrarycookiename'); #read it
if ($user) { $stillloggedin } else { &loggin() }
Otherwise, I'm pretty sure I understand most of it...what are $query and $stillloggedin doing? (&loggin() is trying to log in if it didn't work, right?)