note 104737 added to function.posix-getpwuid
| From: | martin at arp242 dot net | Date: | Mon, 04 Jul 2011 14:18:30 +0000 |
| Subject: | note 104737 added to function.posix-getpwuid | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-181226@lists.php.net to get a copy of this message | ||
Here's a fairly safe way to get the username from uid even if the posix extension isn't
installed.
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;
}
----
Server IP: 91.199.167.223
Probable Submitter: 80.246.203.133
----
Manual Page -- http://www.php.net/manual/en/function.posix-getpwuid.php
Edit -- https://master.php.net/note/edit/104737
Del: integrated -- https://master.php.net/note/delete/104737/integrated
Del: useless -- https://master.php.net/note/delete/104737/useless
Del: bad code -- https://master.php.net/note/delete/104737/bad+code
Del: spam -- https://master.php.net/note/delete/104737/spam
Del: non-english -- https://master.php.net/note/delete/104737/non-english
Del: in docs -- https://master.php.net/note/delete/104737/in+docs
Del: other reasons-- https://master.php.net/note/delete/104737
Reject -- https://master.php.net/note/reject/104737
Search -- https://master.php.net/manage/user-notes.php