Notty
Notty

Reputation:

Can I find which user the web server is being run as in PHP?

I'm writing a simple web app in PHP that needs to have write access to a directory on the server, if the directory isn't writable I want to display an error message explaining to set the owner of the directory to whoever the webserver is being run as (eg. www-data, nobody) but as most people don't know this it would be nice to be able to tell them who to chown the directory to. Is it possible to find this from PHP?

Upvotes: 6

Views: 19582

Answers (9)

Royce Williams
Royce Williams

Reputation: 1639

I don't have a Windows system to test on - but under Unix-likes, get_current_user() returns the expected username (the user running the web server).

Upvotes: 0

Yuseferi
Yuseferi

Reputation: 8670

In some servers using echo exec('whoami'); or exec('whoami',$out); echo $out; returns nothing but in every situation you can get the current user with get_current_user

<?php
echo get_current_user();
?>

Upvotes: 0

gnud
gnud

Reputation: 78528

On Unix platforms, this solution might work even with safe mode on, provided that the posix extension is installed or compiled in.

$user = posix_getpwuid(posix_geteuid());
echo $user['name'];

Docs are here.

Upvotes: 3

Ayman Hourieh
Ayman Hourieh

Reputation: 137156

<?php
  echo exec('whoami');
?>

Upvotes: 19

Jordan S. Jones
Jordan S. Jones

Reputation: 13883

If on linux/unix:

<?php
$temp = tmpfile();
print_r(posix_getpwuid(fileowner($temp)));
?>

Upvotes: 4

gnarf
gnarf

Reputation: 106332

That error message falls apart in a windows server environment. "Please make sure the web server has write access to the directory" is probably a better error message.

Upvotes: 0

Ben Blank
Ben Blank

Reputation: 56572

The quick-and-dirty trick I use is to write a file to /tmp/. It should be created with the user and group used by the PHP process.

Upvotes: 3

BCS
BCS

Reputation: 78575

If you can run some bash, you can use whoami or you might be able to poke around in /proc

Upvotes: 0

karim79
karim79

Reputation: 342635

Check out shell_exec()

Upvotes: 2

Related Questions