2013-10-29 30 views
0

當通過網絡訪問PHP頁面:確定通過web服務器在php中運行shell時的確切用戶?

<?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時確定用戶?

+0

爲什麼_should_ shell用戶等於腳本的所有者?如果你想獲得進程的用戶名,在文檔頁面的註釋中有很多建議:http://us3.php.net/manual/en/function.get-current-user.php – Barmar

+0

我發現' posix_getpwuid(posix_geteuid())'也是danny和'shell_exec('whoami')'是空的。如何通過web php執行系統命令時配置shell用戶? –

+0

'$ USER'和'whoami'由'login'填充。由於PHP是作爲守護進程運行的,因此它不會獲得這些變量。 – Barmar

回答

2

USER環境變量由login程序填寫。由於PHP作爲後臺守護程序運行,因此通常不會設置該變量。您可以使用putenv來設置:

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

我已經成功了,並感謝你的提示。但posix_getpwuid(posix_geteuid())是一個包含info的數組;因此正確的腳本應該是:'<?php $ user = posix_getpwuid(posix_geteuid());運行putenv( 'HOME =' $用戶[ 'DIR']);運行putenv( 'USER =' $用戶[ '名稱']);' –

相關問題