简体   繁体   English

通过PHP或Javascript获取系统用户名

[英]Get system user name through PHP or Javascript

How can I get current logged system username through PHP or JavaScript. 如何通过PHP或JavaScript获取当前登录的系统用户名。

We have an built in function in Java such as: 我们在Java中有一个内置函数,例如:

String userName = System.getProperty("user.name");

but I could not find similar command in any of the technology I need. 但是在我需要的任何技术中我都找不到类似的命令。

I need it for web application, I am using following technologies/libraries for web application HTML, CSS, Bootstrap, Ajax, Jquery, Datatables, PHP, MS SQL. 对于Web应用程序我需要它,我正在为Web应用程序HTML,CSS,Bootstrap,Ajax,Jquery,Datatables,PHP,MS SQL使用以下技术/库。

Is there a way to find system user name through any of the mentioned technology. 有没有一种方法可以通过上述任何一种技术来查找系统用户名。

The following will give you the user who is running the PHP process. 以下内容将为您提供运行PHP流程的用户。 For example, if you are running Apache, it will give you the user who owns the Apache process. 例如,如果您正在运行Apache,它将为您提供拥有Apache进程的用户。 If you run from the command line then you will get the user name of the logged in person. 如果从命令行运行,则将获得已登录用户的用户名。

If you run this through Apache (or any webserver) and that web server is running as user _www on your computer and you are logged in as user zippy , then these will return you _www , not zippy . 如果通过Apache(或任何Web服务器)运行此服务器,并且该Web服务器以_www用户的身份在计算机上运行,​​并且以zippy用户身份登录,则这些服务器将返回_www而不是zippy

On linux or macOS: 在linux或macOS上:

$processUser = exec('whoami');

Or: 要么:

$userInfo = posix_getpwuid(posix_geteuid());
$processUser = $userInfo['name'];

You could also try to see if it is available through the environment. 您也可以尝试查看该环境是否可用。

$user = $_ENV['USERNAME'] ?? '-unknown';

I used the following Java as an example: 我以以下Java为例:

public class Main {
    public static void main(String[] args) {
        String username = System.getProperty("user.name");
        System.out.println("username = " + username);
    }
}

Then I used the following PHP: 然后,我使用了以下PHP:

<?php
echo `whoami`;
echo "\n";

Then I ran on the command line and got the exact same output. 然后我在命令行上运行,并得到完全相同的输出。

On macOS and Linux, this will give you a list of all users currently logged in: 在macOS和Linux上,这将为您提供当前登录的所有用户的列表:

<?php
$users = exec('/usr/bin/users', $output);
print_r($output);

According to this Oracle Java doc System.getProperty("user.name") gives you "User account name". 根据此Oracle Java文档, System.getProperty(“ user.name”)为您提供“用户帐户名”。 I am assuming that in a web server environment, this will give you the name of the user that is running the Java or Webserver process. 我假设在Web服务器环境中,这将为您提供运行Java或Webserver进程的用户的名称。

I am not entirely sure these work on Windows if that's what you are using. 如果您正在使用Windows,我不完全确定它们是否可以在Windows上正常工作。

<?php echo get_current_user(); ?>
<?php echo getenv("username"); ?>

See phpinfo() for more informations. 有关更多信息,请参见phpinfo() Works on both Linux and Windows. 在Linux和Windows上均可使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM