you'll never know exactly as http is a status-less protocol
only thing you can do:
add a new column in your usertable: name=last_action; typ=datetime
now in your function for checking the session do something like that:
function logged_in()
{
$sql='UPDATE user SET UserSession=NULL WHERE (last_action + INTERVAL 15 MINUTE) < NOW()';
mysql_query($sql);
$sql="SELECT userid FROM user WHERE UserSession='".session_id()."'";
return (mysql_num_rows(mysql_query($sql)) > 0);
}