Search code examples
phpenvironment-variableshome-directory

Read user home directory from PHP


I cannot find a way to get the user's home directory (e.g. /home/jack; whatever ~ in bash points to) in PHP using CGI (suPHP). The $_ENV array is empty, and getenv('HOME') returns nothing.

The reason I want to do this is that in absense of configuration saying otherwise, I want to find variable files used by my application in /home/user/.myappnamehere, as most Linux applications do.


I've built something, but it's not the best; While it works, it assumes a lot about the system (e.g. the presence of /etc/passwd)

 $usr = get_current_user();
    $passwd = file('/etc/passwd');
    $var = false;
    foreach ($passwd as $line) {
        if (strstr($line, $usr) !== false) {
            $parts = explode(':', $line);
            $var = realpath($parts[5].'/.report');
            break;
        }
    }

Solution

  • I think you want the result of either: http://us.php.net/manual/en/function.getmyuid.php or http://us.php.net/manual/en/function.posix-getuid.php sent to http://us.php.net/manual/en/function.posix-getpwuid.php