你能在 PHP 中获得 Windows (AD) 用户名吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/168610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can you get a Windows (AD) username in PHP?
提问by fuentesjr
I have a PHP web application on an intranet that can extract the IP and host name of the current user on that page, but I was wondering if there is a way to get/extract their Active Directory/Windows username as well. Is this possible?
我在 Intranet 上有一个 PHP Web 应用程序,可以在该页面上提取当前用户的 IP 和主机名,但我想知道是否有办法获取/提取他们的 Active Directory/Windows 用户名。这可能吗?
回答by Dylan Beattie
Check the AUTH_USERrequest variable. This will be empty if your web app allows anonymous access, but if your server's using basic or Windows integrated authentication, it will contain the username of the authenticated user.
检查AUTH_USER请求变量。如果您的 Web 应用程序允许匿名访问,这将是空的,但如果您的服务器使用基本或 Windows 集成身份验证,它将包含经过身份验证的用户的用户名。
In an Active Directory domain, if your clients are running Internet Explorer and your web server/filesystem permissions are configured properly, IE will silently submit their domain credentials to your server and AUTH_USERwill be MYDOMAIN\user.namewithout the users having to explicitly log in to your web app.
在Active Directory域,如果您的客户正在运行Internet Explorer和您的Web服务器/文件系统权限的配置正确,IE会悄悄地提交自己的域凭据到您的服务器,并AUTH_USER会MYDOMAIN\user.name而无需显式登录到你的Web应用程序的用户。
回答by Schwarz.Oz
I've got php mysql running on IIS - I can use $_SERVER["AUTH_USER"]ifI turn on Windows Authentication in IIS -> Authentication and turn off Anonymous authentication (important)
我在 IIS 上运行了 php mysql -$_SERVER["AUTH_USER"]如果我在 IIS 中打开 Windows 身份验证,我可以使用-> 身份验证并关闭匿名身份验证(重要)
I've used this to get my user and domain:
我用它来获取我的用户和域:
$user = $_SERVER['AUTH_USER'];
$userwill return a value like: DOMAIN\usernameon our network, and then it's just a case of removing the DOMAIN\from the string.
$user将返回一个值,如:DOMAIN\username在我们的网络上,然后它只是DOMAIN\从字符串中删除的情况。
This has worked in IE, FF, Chrome, Safari (tested) so far.
到目前为止,这已在 IE、FF、Chrome、Safari(已测试)中有效。
回答by warren
Look at the PHP LDAP library functions: http://us.php.net/ldap.
查看 PHP LDAP 库函数:http: //us.php.net/ldap。
Active Directory [mostly] conforms to the LDAP standard.
Active Directory [主要] 符合 LDAP 标准。
回答by CrayFishUK
If you're using Apache on Windows, you can install the mod_auth_sspi from
如果您在 Windows 上使用 Apache,则可以从以下位置安装 mod_auth_sspi
https://sourceforge.net/projects/mod-auth-sspi/
https://sourceforge.net/projects/mod-auth-sspi/
Instructions are in the INSTALL file, and there is a whoami.php example. (It's just a case of copying the mod_auth_sspi.so file into a folder and adding a line into httpd.conf.)
说明在 INSTALL 文件中,并且有一个 whoami.php 示例。(这只是将 mod_auth_sspi.so 文件复制到文件夹中并在 httpd.conf 中添加一行的情况。)
Once it's installed and the necessary settings are made in httpd.conf to protect the directories you wish, PHP will populate the $_SERVER['REMOTE_USER']with the user and domain ('USER\DOMAIN') of the authenticated user in IE -- or prompt and authenticate in Firefox before passing it in.
一旦安装完成并在 httpd.conf 中进行了必要的设置以保护您希望的目录,PHP 将$_SERVER['REMOTE_USER']在 IE 中填充经过身份验证的用户的用户和域 ('USER\DOMAIN') -- 或在 Firefox 中进行提示和身份验证在传入之前。
Info is session-based, so single(ish) signon is possible even in Firefox...
信息是基于会话的,因此即使在 Firefox 中也可以进行单(ish)登录...
-Craig
-克雷格
回答by GMR
We have multiple domains in our environment so I use preg_replace with regex to get just the username without DOMAIN\ .
我们的环境中有多个域,所以我使用 preg_replace 和正则表达式来获取没有 DOMAIN\ 的用户名。
preg_replace("/^.+\\/", "", $_SERVER["AUTH_USER"]);
回答by Eddy Goh
If you are looking for retrieving remote user IDSID/Username, use:
如果您正在寻找检索远程用户 IDSID/用户名,请使用:
echo gethostbyaddr($_SERVER['REMOTE_ADDR']);
You will get something like iamuser1-mys.corp.company.com
你会得到类似 iamuser1-mys.corp.company.com 的东西
Filter the rest of the domain behind, and you are able to get the idsid only.
过滤后面的域的其余部分,您只能获得 idsid。
For more information visit http://lostwithin.net/how-to-get-users-ip-and-computer-name-using-php/
有关更多信息,请访问http://lostwithin.net/how-to-get-users-ip-and-computer-name-using-php/
回答by hangy
You could probably authenticate theuser in Apachewith mod_auth_kerbby requiring authenticated access to some files … I think that way, the username should also be available in PHP environment variables somewhere … probably best to check with <?php phpinfo(); ?>once you get it runnning.
您可以通过要求对某些文件进行身份验证访问来使用mod_auth_kerb对 Apache 中的用户进行身份验证……我认为这样,用户名也应该在 PHP 环境变量中的某处可用……最好在运行后进行检查。<?php phpinfo(); ?>
回答by Someone
You can say getenv('USERNAME')
你可以说 getenv('USERNAME')
回答by blomman9
Check out patched NTLM authentication module for Apache https://github.com/rsim/mod_ntlm
查看 Apache https://github.com/rsim/mod_ntlm 的修补 NTLM 身份验证模块
Based on NTLM auth module for Apache/Unix http://modntlm.sourceforge.net/
基于 Apache/Unix 的 NTLM 身份验证模块 http://modntlm.sourceforge.net/
Read more at http://blog.rayapps.com/
在http://blog.rayapps.com/阅读更多信息
Source: http://imthi.com/blog/programming/leopard-apache2-ntlm-php-integrated-windows-authentication.php
来源:http: //imthi.com/blog/programming/leopard-apache2-ntlm-php-integrated-windows-authentication.php
回答by Kornilov Ruslan
get_user_name works the same way as getenv('USERNAME');
get_user_name 的工作方式与 getenv('USERNAME'); 相同。
I had encoding(with cyrillic) problems using getenv('USERNAME')
我在使用 getenv('USERNAME') 时遇到了编码(使用西里尔文)问题

