getlogin
This function returns the current login from /etc/utmp, if any. If null, use getpwuid. For example:
$login = getlogin || (getpwuid($<))[0] || "Intruder!!";