24

This program only returns the client machine name in localhost only

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.

d-_-b
  • 21,536
  • 40
  • 150
  • 256
Deepak
  • 872
  • 2
  • 7
  • 13

6 Answers6

13

What's this "other information"? An IP address?

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.

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • @Marc B: This worked fine for me in localhost. But when it is put online, i didn't get the same. – Deepak Feb 28 '11 at 12:41
  • @Deepak: You get whatever is in DNS for the remote host. You cannot forcibly reach into a remote user's computer and find their machine name - they don't send that information to you. – Borealid Feb 28 '11 at 12:42
  • What are you getting? If you're connecting to a completely seperate server, you will get something different. There's no way around that. – Marc B Feb 28 '11 at 12:43
  • @Marc B: Static-51.14.34.36.ip-dsl.ekm.eth.net. This is what i get when i test this online. – Deepak Feb 28 '11 at 12:45
  • 1
    Yes, and that's to be expected. If you run your browser and webserver on the same machine, it'll use the local loopback address (127.0.0.1/localhost). When you access the site on a server elsewhere, the script will see the browser's "external" address - could be your NAT router, could be your ISP's proxy server, etc... You won't see localhost, because it's NOT A LOCAL CONNECTION anymore. – Marc B Feb 28 '11 at 12:46
  • Here why I need this is because, need to know the client system configuration like name, ram etc for making sure that my application runs perfectly on that system. If the system does have some predefined configuration, i cant just throw a message that it wont work there. – Deepak Feb 28 '11 at 12:50
  • You can't access that data with javascript or extract it from HTTP headers - that information is NOT accessible to anything you could do, unless you want to use something like ActiveX or a signed Java applet. – Marc B Feb 28 '11 at 13:00
  • This worked for me in my LOCAL NETWORK : echo gethostbyaddr($_SERVER["REMOTE_ADDR"]); – cetipabo Aug 30 '18 at 12:30
7

Not in PHP.
phpinfo(32) contains everything PHP able to know about particular client, and there is no [windows] computer name

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
3

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".

By requesting "NTLM" authorization via HTTP header...

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.

$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("\0", '', 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/

netblognet
  • 1,951
  • 2
  • 20
  • 46
0

PHP Manual says:

gethostname (PHP >= 5.3.0) gethostname — Gets the host name

Look:

<?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
?>

http://php.net/manual/en/function.gethostname.php

Enjoy

whoiz
  • 217
  • 2
  • 3
-4

gethostname() using the IP from $_SERVER['REMOTE_ADDR'] while accessing the script remotely will return the IP of your internet connection, not your computer.

Andy Jones
  • 6,205
  • 4
  • 31
  • 47
-11

Try

echo getenv('COMPUTERNAME');

This will return your computername.

Danny Beckett
  • 20,529
  • 24
  • 107
  • 134
SwR
  • 612
  • 1
  • 8
  • 21