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;
}
?>
posix_getpwuid
(PHP 4, PHP 5)
posix_getpwuid — Kullanıcı kimliği belirtilen kullanıcı hakkında bilgi döndürür
Açıklama
array posix_getpwuid
( int
$uid
)
uid kullanıcı kimlikli kullanıcı hakkında bilgiyi
bir dizi içinde döndürür.
Değiştirgeler
-
uid -
Kullanıcı kimliği.
Dönen Değerler
Şu elemanları içeren bir ilişkisel dizi döner:
| Anahtar | Açıklama |
|---|---|
| name | Kullanıcının kullanıcı ismi. Kullanıcının gerçek ismi değil, en fazla 16 karakter uzunlukta olabilen ve sistemde oturum açarken kullandığı kısa adıdır. |
| passwd | Kullanıcının parolası bu elemanda şifreli olarak bulunur. Sistemde "shadow" parolalar kullanılıyorsa sadece bir * imi bulunur. |
| uid |
Kullanıcı kimliği. İşlevi çağırıken uid
değiştirgesine atananla aynı olmalıdır.
|
| gid | Kullanıcının grup kimliği. Grup ismini ve üye listesini öğrenmek için posix_getgrgid()işlevini kullanabilirsiniz. |
| gecos | GECOS, Honeywell sistemlerden kalma artık kullanılmayan bir alan olmakla beraber içeriği POSIX tarafından şekillendirilmiştir.Alan birbirlerinden virgül ile ayrılmış olarak kullanıcının tam ismini, iş ve ev telefonlarını ve büro numarasını içerebilir. Çoğu sistemde kullanıcının tam ismi için kullanılmaktadır. |
| dir | Kullanıcının ev dizininin mutlak yolunu içerir. |
| shell | Kullanıcının öntanımlı kabuğunu çalıştıran dosyanın mutlak yolu. |
Örnekler
Örnek 1 - posix_getpwuid() örneği
<?php
$userinfo = posix_getpwuid(10000);
print_r($userinfo);
?>
Yukarıdaki örnek şuna benzer bir çıktı üretir:
Array
(
[name] => tom
[passwd] => x
[uid] => 10000
[gid] => 42
[geocs] => "tom,,,"
[dir] => "/home/tom"
[shell] => "/bin/bash"
)
Ayrıca Bakınız
- posix_getpwnam() - Kullanıcı adı belirtilen kullanıcı hakkında bilgi döndürür
- POSIX getpwnam(3) kılavuz sayfası
martin at arp242 dot net
04-Jul-2011 07:18
ddascalescu at gmail dot com
08-Apr-2008 07:14
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').
ddascalescu at gmail dot com
04-Apr-2008 05:39
On Windows, posix_getpwuid() is not implemented , but if you just want the username of the current user, you can use get_current_user().
mehmet at karakaya dot us
19-Mar-2006 11:45
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
/* settings for start point and where to stop */
$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);
/* getting and writing the user info line by line */
$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);
/* control or forwarding in order to prevent prescription */
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.
Nikolai-Zujev-(at)-Gmail-dot-Com
25-Sep-2004 02:05
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.
rolf dot winterscheidt at rowitech dot de
28-May-2003 04:34
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
rcgraves+php at brandeis dot edu
21-Feb-2000 05:54
Returns an array containing the elements of the password structure. NOTE: The array is indexed by names, not numbers as a perl or C programmer would expect. The array elements are:
$_["name"] string userid (joeschmo)
$_["passwd"] string crypted password (or "x" if shadowed)
$_["uid"] integer uidnumber (e.g. 0 for root)
$_["gid"] integer primary gidnumber (e.g. 0 for wheel/root)
$_["gecos"] string name (Joseph P. Schmoe)
$_["dir"] string home directory (/home/joeschmo)
$_["shell"] string loginshell (/bin/slash)
