从 PHP 读取用户主目录

发布于 2024-08-20 06:39:05 字数 621 浏览 2 评论 0原文

我无法找到一种方法来使用 CGI (suPHP) 在 PHP 中获取用户的主目录(例如 /home/jack;无论 bash 中的 ~ 指向什么)。 $_ENV 数组为空,并且 getenv('HOME') 不返回任何内容。

我想要这样做的原因是,在没有配置的情况下,我想在 /home/user/.myappnamehere 中找到我的应用程序使用的变量文件,就像大多数 Linux 应用程序所做的那样。


我已经构建了一些东西,但它不是最好的;虽然它可以工作,但它对系统做了很多假设(例如 /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;
        }
    }

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;
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浮华 2024-08-27 06:39:05

如果安全模式被禁用,请尝试这个

$homedir = `cd ~ && pwd`;

If safemode is disabled, try this one

$homedir = `cd ~ && pwd`;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文