Does it work anytime after an immediate login?
What is the chain of function calls here; none of them currently call any other, so I can't see the process.
Only thing that stands out is this:
foreach($args as $data)
$args[$data] = $result[$data];
It is a completely useless traversal; assigning to $args cannot be done as func_get_arg[s] is incapable of retrieving data as reference.