Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

how do i check from within a php script, if a user is logged in? My current way is, to get the output of

w | grep <username>

and check, if there is output. It works, if a user is logged in via gdm, kdm or ssh or login on a tty. But what it doesn't get is, if an other user opened a terminal and run su <username>, this shell process is not reported, how do i get this?

share|improve this question
up vote 2 down vote accepted

Well, if userA uses su to become userB, then userB is still not logged in.

However, how about this:

ps -u <username>

Then you can check $? -eq 0 to see if any processes were found belonging to that user. However, if there are any long-running processes owned by that user, then it will still show them. If that's no good, you could check for their shell.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.