Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Get the currently logged in User name

by irah (Pilgrim)
on Nov 05, 2011 at 09:45 UTC ( [id://936127]=perlquestion: print w/replies, xml ) Need Help??

irah has asked for the wisdom of the Perl Monks concerning the following question:

Greetings to Monks!,

I used few functions such as getlogin, getpwuid and in some other way including environment variable to get the currently logged in user name in Linux. Everything works when I execute the Perl script from Linux prompt.

When I call the same script from web programming language, it gives the user name as "www-data" since it Apache server running my program as www-data user.

I have to get the user name of the currently login user name instead of some other name. Is there any way to achieve this in Perl?.

Thanks in Advance!.

Replies are listed 'Best First'.
Re: Get the currently logged in User name
by moritz (Cardinal) on Nov 05, 2011 at 11:03 UTC
    I have to get the user name of the currently login user name instead of some other name.

    You seem to operate under the premise that all times, there is exactly one user logged in in an interactive session. That isn't the case, there could be none, or many different ones. What should happen in such a case?

    You might also benefit from telling us what your bigger picture is, your question sounds like an XY Problem.

    In lack of better ideas, you could parse the output for last to get a list of logged-in users, but there could be better solutions for your overall problem.

Re: Get the currently logged in User name
by graff (Chancellor) on Nov 05, 2011 at 13:25 UTC
    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.

Re: Get the currently logged in User name
by aaron_baugher (Curate) on Nov 05, 2011 at 19:53 UTC

    Functions like getlogin and getpwuid return the info for the user running the script. When you run the script at the command line, the user is you. When the web server runs the script as the request of an HTTP request, the user is the one your web server is configured to run as -- in this case, www-data.

    What current login name are you trying to get? There are many methods you can use to require visitors to your web site to login, from basic HTTP authentication using .htaccess/htpasswd (which will put the username in $ENV{REMOTE_USER}), to form- and/or cookie-based session/login systems that are common in many frameworks. Each of these will provide the username in a different way, so it will depend on what login system you use.

      (user)username(/user)
Re: Get the currently logged in User name
by doug (Pilgrim) on Nov 06, 2011 at 03:40 UTC

    This is more of a linux question than one for a perl group, but it is an easy one. Try something like

        man who

    and see if that gives you what you want.

    - doug

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://936127]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2024-04-16 19:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found