©
本文檔使用 php中文網手册 發布
(PHP 4, PHP 5)
posix_getpwuid — Return info about a user by user id
$uid
)Returns an array of information about the user referenced by the given user ID.
uid
The user identifier.
Returns an associative array with the following elements:
Element | Description |
---|---|
name | The name element contains the username of the user. This is a short, usually less than 16 character "handle" of the user, not the real, full name. |
passwd | The passwd element contains the user's password in an encrypted format. Often, for example on a system employing "shadow" passwords, an asterisk is returned instead. |
uid |
User ID, should be the same as the
uid parameter used when calling the
function, and hence redundant.
|
gid | The group ID of the user. Use the function posix_getgrgid() to resolve the group name and a list of its members. |
gecos | GECOS is an obsolete term that refers to the finger information field on a Honeywell batch processing system. The field, however, lives on, and its contents have been formalized by POSIX. The field contains a comma separated list containing the user's full name, office phone, office number, and home phone number. On most systems, only the user's full name is available. |
dir | This element contains the absolute path to the home directory of the user. |
shell | The shell element contains the absolute path to the executable of the user's default shell. |
Example #1 Example use of posix_getpwuid()
<?php
$userinfo = posix_getpwuid ( 10000 );
print_r ( $userinfo );
?>
以上例程的输出类似于:
Array ( [name] => tom [passwd] => x [uid] => 10000 [gid] => 42 [gecos] => "tom,,," [dir] => "/home/tom" [shell] => "/bin/bash" )
[#1] Anonymous [2013-02-05 15:37:22]
I only want the user name rather than the rest. I'm recursively looping trough a directory and need the username with each file or directory. I use stat to get file attributes that I need which gives me uid. Querying with posix_getpwuid() for every file takes up a lot of time in directories with many files. I came up with a caching mechanism (which I believe should be built-in). Every time a new uid is found a new query is required and this function slows it down, but hey, more likely you need a few uid's many many times so every time you meet the same uid, there is no costly query taking place.
Heres my code, feel free, etc., etc.
<?php
$arr_uname = Array();
function file_owner_cached($uid)
{
global $arr_uname;
if (!isset($arr_uname[$uid]))
{
$arr_uname[$uid] = posix_getpwuid($uid)['name'];
}
return $arr_uname[$uid];
}
?>
Works in PHP 5.3.19, under linux of course.. not tested on anything else.
[#2] martin at arp242 dot net [2011-07-04 07:18:30]
Here's a fairly safe way to get the username from uid even if the posix extension isn't installed.
<?php
function GetUsernameFromUid($uid)
{
if (function_exists('posix_getpwuid'))
{
$a = posix_getpwuid($uid);
return $a['name'];
}
# This works on BSD but not with GNU
elseif (strstr(php_uname('s'), 'BSD'))
{
exec('id -u ' . (int) $uid, $o, $r);
if ($r == 0)
return trim($o['0']);
else
return $uid;
}
elseif (is_readable('/etc/passwd'))
{
exec(sprintf('grep :%s: /etc/passwd | cut -d: -f1', (int) $uid), $o, $r);
if ($r == 0)
return trim($o['0']);
else
return $uid;
}
else
return $uid;
}
?>
[#3] ddascalescu at gmail dot com [2008-04-08 19:14:47]
Correction regarding my note below: get_current_user() does *not* get the name of the user the script is running as. Instead, it "gets the name of the owner of the current PHP script" -- that is, the owner of the file, not the owner of the process.
To properly get the running user, test if function_exists('posix_getpwuid') and if not, assume you're running on Windows and call getenv('USERNAME').
[#4] ddascalescu at gmail dot com [2008-04-04 17:39:34]
On Windows, posix_getpwuid() is not implemented , but if you just want the username of the current user, you can use get_current_user().
[#5] mehmet at karakaya dot us [2006-03-19 11:45:48]
if the system is also a mail server and system users have userdirs with php support this function may cause a spam abuse which made by a system user.
<?php
$start=0;//the first user id
$interval=1000;//amount of lines that will be read
$finishline=3000;//the last user id
$first=(isset($_GET['first'])?$_GET['first']:$start);
$last=(isset($_GET['last'])?$_GET['last']:$interval);
$fp=fopen('copiedpasswd','a');
//copiedpasswd must be writeable by apache
for ($user=$first;$user<=$last;$user++)
{
$list=posix_getpwuid($user);
if ($list['name']=='') { continue; }
$line=implode(':',$list)."\n";
fputs($fp,$line);
}//end for
fclose($fp);
if ($last>=$finishline)
{
header("Location: copiedpasswd");
}//end if
else
{
$first += $interval;
$last += $interval;
header("Location: thenameofthisscript.php?first=$first&last=$last");
}//end else
?>
Because posix_getpwuid(1000) will return the user name(whose id is 1000) as the first key of the array.
[#6] Nikolai-Zujev-(at)-Gmail-dot-Com [2004-09-25 14:05:11]
If You are useing kernel security module, such as LIDS, GrSec or Selinux it will work only if '/etc/passwd' is readable for user, under which PHP/Apache runs, otherwice you get FALSE.
[#7] rolf dot winterscheidt at rowitech dot de [2003-05-28 04:34:24]
To get the name of the owner of a file you can use something like this:
<?php
$startscript="/var/log/hello.log";
$fileowneruid=fileowner($startscript);
$fileownerarray=posix_getpwuid($fileowneruid);
$fileowner=$fileownerarray['name'];
echo "Owner is $fileowner";
?>
(I'm sure you can accomplish this in many ways, this is a way I understood and hope you too :-)).
Rolf