在 PHP 中获取客户端机器名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5142051/
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
Get Client Machine Name in PHP
提问by Deepak
This program only returns the client machine name in localhost only
该程序仅返回本地主机中的客户端机器名称
echo gethostbyaddr($_SERVER['REMOTE_ADDR']);
echo gethostbyaddr($_SERVER['REMOTE_ADDR']);
If it is run from an online server, then computer name is not shown and some other information is being shown. So is there anyway to get the computer name in php when the program runs from online server.
如果它是从在线服务器运行的,则不会显示计算机名称,而会显示一些其他信息。那么,当程序从在线服务器运行时,无论如何都可以在 php 中获取计算机名称。
回答by Marc B
What's this "other information"? An IP address?
这个“其他信息”是什么?一个IP地址?
In PHP, you use $_SERVER['REMOTE_ADDR']
to get the IP address of the remote client, then you can use gethostbyaddr()
to try and conver that IP into a hostname - but not all IPs have a reverse mapping configured.
在 PHP 中,您用于$_SERVER['REMOTE_ADDR']
获取远程客户端的 IP 地址,然后您可以gethostbyaddr()
尝试将该 IP 转换为主机名 - 但并非所有 IP 都配置了反向映射。
回答by Your Common Sense
Not in PHP.phpinfo(32)
contains everything PHP able to know about particular client, and there is no [windows] computer name
不是在 PHP 中。phpinfo(32)
包含 PHP 能够知道的关于特定客户端的所有信息,并且没有 [windows] 计算机名称
回答by netblognet
In opposite to the most comments, I think it is possible to get the client's hostname (machine name) in plain PHP, but it's a little bit "dirty".
与大多数评论相反,我认为可以在纯 PHP 中获取客户端的主机名(机器名),但这有点“脏”。
By requesting "NTLM" authorization via HTTP header...
通过 HTTP 标头请求“NTLM”授权...
if (!isset($headers['AUTHORIZATION']) || substr($headers['AUTHORIZATION'],0,4) !== 'NTLM'){
header('HTTP/1.1 401 Unauthorized');
header('WWW-Authenticate: NTLM');
exit;
}
You can force the client to send authorization credentials via NTLM format. The NTLM hash sent by the client to server contains, besides the login credtials, the clients machine name. This works cross-browser and PHP only.
您可以强制客户端通过 NTLM 格式发送授权凭据。客户端发送到服务器的 NTLM 哈希除了包含登录凭据外,还包含客户端计算机名称。这仅适用于跨浏览器和 PHP。
$auth = $headers['AUTHORIZATION'];
if (substr($auth,0,5) == 'NTLM ') {
$msg = base64_decode(substr($auth, 5));
if (substr($msg, 0, 8) != "NTLMSSPx00")
die('error header not recognised');
if ($msg[8] == "x01") {
$msg2 = "NTLMSSPx00x02"."x00x00x00x00".
"x00x00x00x00".
"x01x02x81x01".
"x00x00x00x00x00x00x00x00".
"x00x00x00x00x00x00x00x00".
"x00x00x00x00x30x00x00x00";
header('HTTP/1.1 401 Unauthorized');
header('WWW-Authenticate: NTLM '.trim(base64_encode($msg2)));
exit;
}
else if ($msg[8] == "x03") {
function get_msg_str($msg, $start, $unicode = true) {
$len = (ord($msg[$start+1]) * 256) + ord($msg[$start]);
$off = (ord($msg[$start+5]) * 256) + ord($msg[$start+4]);
if ($unicode)
return str_replace("<?php
echo gethostname(); // may output e.g,: sandie
// Or, an option that also works before PHP 5.3
echo php_uname('n'); // may output e.g,: sandie
?>
", '', substr($msg, $off, $len));
else
return substr($msg, $off, $len);
}
$user = get_msg_str($msg, 36);
$domain = get_msg_str($msg, 28);
$workstation = get_msg_str($msg, 44);
print "You are $user from $workstation.$domain";
}
}
And yes, it's not a plain and easy "read the machine name function", because the user is prompted with an dialog, but it's an example, that it is indeed possible (against the other statements here).
是的,这不是一个简单易行的“读取机器名称功能”,因为用户会收到一个对话框提示,但这是一个例子,它确实是可能的(与此处的其他陈述相反)。
Full code can be found here: https://en.code-bude.net/2017/05/07/how-to-read-client-hostname-in-php/
完整代码可以在这里找到:https: //en.code-bude.net/2017/05/07/how-to-read-client-hostname-in-php/
回答by whoiz
PHP Manual says:
PHP手册说:
gethostname (PHP >= 5.3.0) gethostname — Gets the host name
gethostname (PHP >= 5.3.0) gethostname — 获取主机名
Look:
看:
echo getenv('COMPUTERNAME');
http://php.net/manual/en/function.gethostname.php
http://php.net/manual/en/function.gethostname.php
Enjoy
享受
回答by Senya Peter Cullen
gethostname()
using the IP from $_SERVER['REMOTE_ADDR']
while accessing the script remotely will return the IP of your internet connection, not your computer.
gethostname()
在$_SERVER['REMOTE_ADDR']
远程访问脚本时使用 IP将返回您的互联网连接的 IP,而不是您的计算机。
回答by SwR
Try
尝试
##代码##This will return your computername.
这将返回您的计算机名。