CakeFest 2024: The Official CakePHP Conference

posix_getpwuid

(PHP 4, PHP 5, PHP 7, PHP 8)

posix_getpwuid指定 ID のユーザーに関する情報を返す

説明

posix_getpwuid(int $user_id): array|false

指定したユーザー ID のユーザーについての情報を配列で返します。

パラメータ

user_id

ユーザー ID。

戻り値

返される連想配列の要素は次のようになります。

ユーザー情報配列
要素 説明
name 要素 name はユーザー名を有しています。これは、通常、 実際の完全な名前ではなく16文字未満のユーザーの"ハンドル名"となります。
passwd 要素passwd には暗号化されたユーザーのパスワードが含まれます。 シャドウパスワードを使用しているシステムでは、アスタリスクが代わりに 返されます。
uid ユーザーID。これは、この関数をコールする際に使用するパラメータ user_idと同じとなり、このため冗長になります。
gid ユーザーのグループID。 実際のグループ名を調べたりそのグループのメンバーの一覧を 得るには関数posix_getgrgid()を使用してください。
gecos GECOS は旧式の項であり、Honeywell バッチ処理プログラムの finger 情報フィールドを参照します。 しかし、このフィールドはまだ生きており、その内容はPOSIXで 規定されています。 このフィールドには、カンマで区切られた ユーザーのフルネーム、オフィスの電話番号、家の電話番号に関する リストが含まれています。多くのシステムでは、ユーザーのフルネーム のみが利用可能です。
dir この要素には、ユーザーのホームディレクトリへの絶対パスが含まれています。
shell shell 要素には、ユーザーのデフォルトシェルの実行ファイルへの絶対パスが 含まれています。
この関数は、失敗時に false を返します。

例1 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"
)

参考

  • posix_getpwnam() - 指定した名前のユーザーに関する情報を返す
  • POSIX man ページ GETPWNAM(3)

add a note

User Contributed Notes 7 notes

up
8
ddascalescu at gmail dot com
15 years ago
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').
up
0
Anonymous
11 years ago
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.
up
0
mehmet at karakaya dot us
18 years ago
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.
up
0
Nikolai-Zujev-(at)-Gmail-dot-Com
19 years ago
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.
up
0
rolf dot winterscheidt at rowitech dot de
20 years ago
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
up
-1
martin at arp242 dot net
12 years ago
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;
}
?>
up
-12
ddascalescu at gmail dot com
15 years ago
On Windows, posix_getpwuid() is not implemented , but if you just want the username of the current user, you can use get_current_user().
To Top