in reply to Get the currently logged in User name

So, there's a web client somewhere that sends a request to your apache web server, and to satisfy the request, the web server runs your script, is that it? Did the web client have to perform a login to be able to send that request?

If so, then whatever web application is receiving the request and running your script knows the user-ID for it. You could update your script so that it takes a command-line option, like '--userid=some_username_string', instead of calling getpwuid or whatever to set the name of the user. This way, when the web-app runs your script, it can say who the user is supposed to be. (But when you run it from a command-line shell, and don't provide that option string, it just uses the original method to figure out who you are.)

If the web app does not require a login, then there's no telling who the "user" is. I think the best you can do (if the web app is a perl cgi script) is look at $ENV{REMOTE_ADDR} to get the IP address of the web client.