通过 Web 服务器在 php 中运行 shell 时确定确切的用户


Determine the exact user when running shell in php via the web server?

当通过网络访问php页面时:

<?php
print '<pre>'."'n";
print 'Current script owner: '."'n";
print get_current_user()."'n";
print "'n";
print '$USER: '."'n";
passthru('print $USER');
print "'n";

我得到输出:

Current script owner: 
danny
$USER: 

为什么 shell 用户不等于当前脚本所有者?通过 Web 服务器在 php 脚本中运行 shell 时如何确定用户?

USER 环境变量由login程序填充。由于 PHP 是作为后台守护程序运行的,因此通常不会设置变量。您可以使用putenv进行设置:

$user = posix_getpwuid(posix_geteuid());
putenv('USER='.$user['name']);