posix_getpwuid() - posix函数(可移植操作系统接口)
posix_getpwuid()
(PHP 4, PHP 5, PHP 7)
Return info about a user by user id
说明
posix_getpwuid(int $uid): arrayReturns an array of information about the user referenced by the given user ID.
参数
$uidThe 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$uidparameter 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 use of posix_getpwuid()
以上例程的输出类似于:
Array ( [name] => tom [passwd] => x [uid] => 10000 [gid] => 42 [gecos] => "tom,,," [dir] => "/home/tom" [shell] => "/bin/bash" )
参见
posix_getpwnam()
Return info about a user by username- POSIX man page GETPWNAM(3)
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').
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. Works in PHP 5.3.19, under linux of course.. not tested on anything else.
Here's a fairly safe way to get the username from uid even if the posix extension isn't installed.
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. Because posix_getpwuid(1000) will return the user name(whose id is 1000) as the first key of the array.
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.
To get the name of the owner of a file you can use something like this: (I'm sure you can accomplish this in many ways, this is a way I understood and hope you too :-)). Rolf
On Windows, posix_getpwuid() is not implemented , but if you just want the username of the current user, you can use get_current_user().
内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)