Get the client's computer name - php

I am getting the client's (website user's) IP address. Now I'd like to go one step further by knowing the user's computer name. So far, my research has not turned up anything to aid me in retrieving this information.
Is it possible to use the user's IP address, or some other means, to get my visitor's computer name using PHP?

PHP 5.4+
gethostbyaddr($_SERVER['REMOTE_ADDR'])

You can perform a reverse DNS lookup using gethostbyaddr().
Note that this will give you the name of the host the request came from according to reverse DNS.
It will not give you a result if reverse DNS isn't set up
It will not give you the Windows name of the computer
It will give you the name of the router if NAT is involved or proxy if a proxy is involved.

Not possible with plain php running on the server. It'd be a security/privacy issue to know details of the client such as computer name, mac address, contents of his drive.
You need some sort of application running on the client's machine in order to get this.

If you're referring to the hostname (displayed for instance by the hostname command on linux) of the computer doing the request:
That information is not included in an HTTP request. (That is, it's impossible for PHP to figure out.)
You could do a reverse DNS lookup, but that's probably not what you want anyway.

This is all that you could get using just PHP (you may try these butIi dont think this is what you actually needed):
gethostname()
gethostbyname(gethostname())
$_SERVER['HTTP_HOST']
$_SERVER['SERVER_SIGNATURE']
$_SERVER['SERVER_NAME']
$_SERVER['SERVER_ADDR']
$_SERVER['SERVER_PORT']
$_SERVER['REMOTE_ADDR']
gethostbyaddr($_SERVER['REMOTE_ADDR'])
php_uname()

The only thing you could do is try to get a DNS name for the client. "Computer Name" is a Windows made-up thing. Just call the built-in function gethostbyaddr() with the client's IP address. However, it won't always (hardly ever) work.

You can do this by
$_SERVER['REMOTE_HOST']
'REMOTE_HOST' - The Host name from which the user is viewing the current page. The reverse dns lookup is based off the REMOTE_ADDR of the user.
Note: Your web server must be configured to create this variable. For example in Apache you'll need HostnameLookups On inside httpd.conf for it to exist. As David mentioned you can also use . gethostbyaddr()
Pls go thru all the comments in the
url before actually using the
function.

Do something lik this:
<?php
//get host by name
echo gethostname();
echo "<br>";
//get OS
echo php_uname();
?>

Related

PHP - check if domain matches server's IP when domain's IP is dedicated

I have a remote login script that user hosts (runs) on his server. During registration, user needs to specify a domain he will login from. When user runs script on his domain and logins to my server for the 1st time, I log his IP using:
$ip_address = $_SERVER['REMOTE_ADDR'];
When user logins 2nd time, I check if his IP address is still the same (using the same function above). Then I check if he still uses the same domain using:
$domain = $_SERVER['HTTP_REFERER'];
Finally, besides other security checks, I also check if specified domain really points to IP address using:
$domain_ips_array = gethostbynamel($domain);
if (in_array($ip_address, $domain_ips_array)) {
echo "Wonderful, domain really points to this IP";
}
But there's a problem when domain points to a dedicated IP. For example, if server's IP (where script is actually hosted) is 1.1.1.1 (this IP is also returned by $_SERVER['REMOTE_ADDR']), but domain is configured to use a dedicated IP 1.1.1.2, gethostbynamel function will only return 1.1.1.2, and check will fail (even if domain is actually hosted on server with IP address 1.1.1.1).
How do I solve this issue? Put simply, I need to be sure that user always runs the script on the same IP/domain, and if any of these is changed, alert is displayed.
I think you have a better luck with refactoring a little bit of the script you send to the user.
For example when a first login comes along, you can see the IP from the requested server.
Do your magic and return the IP to your script. Then store it somewhere and always send it after that.
This way you will always have the first IP. And then check with the $_SERVER variable. Domains can be changed and I think it's not that reliable.
Said it more simple, you need to have it stored somewhere.
-EDIT
You can use the function gethostbyaddr.
This will return you the domain of the IP. So you can store the domain from the first request as well and then check it with every other.

PHP - $_SERVER["REMOTE_ADDR"] inconsistent

$_SERVER["REMOTE_ADDR"] sometimes returns 10.0.0.2 and sometimes a hex value like fe80::d7c:2a15:b162:81c1 during the same session. Why is this happening? I'm using XAMPP.
You seem to use the DNS name to access your server. As such, you might get either an A or an AAAA record for the DNS name. Which means, one time you access via IPv4 and the other via IPv6 (fe80::d7c:2a15:b162:81c1 is an IPv6-address).
as pointed out earlier, those hex values are ipv6 addresses..you need to change your code to adapt to both ipv4 and ipv6 addresses if it depends on $_SERVER["REMOTE_ADDR"]

PHP - IP address echoed as 127.0.0.1

I'm quite a novice at PHP.
I would like the IP address that I (myself, only. I modified my hosts file for the HotSpot shield webpage) have been given when using HotSpot shield to show up on my webpage, but unfortunately, it's echoed as 127.0.0.1. The main objective is that when I connect, the IP address that I've been set is echoed on the page. This is what code I've used:
<?php $rmt = $_SERVER["REMOTE_ADDR"]; echo "$rmt \n" ?>
The only problem is is that $rmt is 127.0.0.1. Do any of you know how to get it to display the public IP address to be displayed?
This can happen with local proxy servers; you could check out the other headers that are sent with your request by var_dump($_SERVER); and search for stuff like X-Forwarded-For or X-Real-IP
$_SERVER['REMOTE_ADDR'] is referring to the IP adress from which you connected. If you're running the server on your local machine and connecting from your local machine, it uses your local ip (127.0.0.1 is always "localhost").
In order to show a different ip you need to run it on another server (a public web hotel server preferably), or connect to your server from another machine.
I had just the same issue.
As it turns out, I was getting the proxy IP address instead of my own IP.
So I ran:
var_dump($_SERVER)
//you could also use print_r($_SERVER);
And then looked for something like this:
["HTTP_X_REAL_IP"]
Then captured it into a var like this:
$ip = getenv('HTTP_X_REAL_IP')

PHP $_SERVER['SERVER_NAME'] returns machine name in IIS6

My company uses a piece of PHP-based software that depends on $_SERVER['SERVER_NAME'] to construct a URL. It runs on PHP 5.2 under Windows Server 2003 or 2008 with IIS6 or IIS7 through FastCGI.
This works "correctly" (or, at least, how we expect it to work) on every IIS system we've ever installed it on. In other words, on the same server, if you call it with http://app.foo.com/myscript.php, $_SERVER['SERVER_NAME'] is 'app.foo.com', if you call it with http://192.168.1.22/myscript.php, $_SERVER['SERVER_NAME'] is '192.168.1.22', etc.
Today, for the first time ever, we installed it on a server (Windows Server 2003 with IIS6) that acts differently. No matter what URL we use to load the script, $_SERVER['SERVER_NAME'] is 'myserver' (the machine name of the server), which is causing problems.
Now that this issue has come up, we're working on eliminating the use of $_SERVER['SERVER_NAME'] in future releases of the software ... but is there any configuration I can perform (in IIS6, php.ini, ... ?) on this server to fix this in the meantime? If we can't change it so that $_SERVER['SERVER_NAME'] always contains the host from the requesting URL, is there at least some way to configure it so that $_SERVER['SERVER_NAME'] will contain a particular desired FQDN ('app.foo.com' instead of 'myserver')?
EDIT: Added a bounty as I am very interested in receiving an answer to this question.
but is there any configuration I can perform (in IIS6, php.ini, ... ?) on this server to fix this in the meantime?
The globals such as $_SERVER are actually writable, so as a short term solution just to get things working, you could insert some quick PHP code to specifically set the SERVER_NAME key to the value you need for the site to work.
For example, in your opening PHP file, you could just include the line:
$_SERVER['SERVER_NAME'] = 'app.foo.com';
All subsequent calls to $_SERVER['SERVER_NAME'] would have the value you wanted.
If you needed to account for IP access, you could use a combination of REQUEST_URI, parse_url(), or HTTP_HOST if available.
Longterm, getting rid of SERVER_NAME from the code base will probably help reduce your blood pressure :)
Try using $_SERVER['HTTP_HOST'] or if that doesn't work, use $_SERVER['SCRIPT_URI'] and parse_url().
well as far as i heard, $_SERVER['SERVER_NAME'] gives the value that defined in the server configuration file and doesn't tell you anything about the request. Whereas, $_SERVER['HTTP_HOST'] gives you the domain name through which the current request is being fulfilled and is more directly related to the request.
Below is an example to clear more about this two things. Assume that you have a host defined in the Server with ServerName of domain.com and an IP address of 198.16.120.100.
Below are the different between these two variables:
for http://www.domain.com
$_SERVER['SERVER_NAME'] = domain.com
$_SERVER['HTTP_HOST'] = www.domain.com
for http://198.16.120.100
$_SERVER['SERVER_NAME'] = domain.com
$_SERVER['HTTP_HOST'] = 198.16.120.100
If you want to change SERVER_NAME of your IIS server i guess this link will help you.
If you need anything more, do not hesitate.
Regards
Please try to set host headers in IIS 6.0 and test it Reference
Note: $_SERVER is an array containing information such as headers, paths, and script locations. The entries in this array are created by the web server. There is no guarantee that every web server will provide any of these; servers may omit some, or provide others not listed here. That said, a large number of these variables are accounted for in the CGI/1.1 specification, so you should be able to expect those.
The SERVER_NAME variable MUST be set to the name of the server host
to which the client request is directed. It is a case-insensitive
hostname or network address. It forms the host part of the
Script-URI.
SERVER_NAME = server-name
server-name = hostname | ipv4-address | ( "[" ipv6-address "]" )
A deployed server can have more than one possible value for this
variable, where several HTTP virtual hosts share the same IP address.
In that case, the server would use the contents of the request's Host
header field to select the correct virtual host.

My IP is showing up wrong in PHP home server

Ok simple enough
<?PHP
echo $_SERVER[REMOTE_ADDR];
?>
Ok maybe not, I my IP is currently 72.184.212.85 however the code above which I am using on an IP blocking system for a script shows my IP as my home server IP of 127.0.0.1
So when I go to my script my IP is shown as 127.0.0.1 but when I go to other websites it is shown as 72.184.212.85
How can I get the first value to show on my test server?
$_SERVER['REMOTE_ADDR'] will always show the IP address from which the request came. If you access your own script on your own computer within your own network, your external IP address never comes into play. The request would have to leave your local network and then come back in for the external address to show up, but if it's all local, that'll never happen.
You'll have to make your server publicly accessible and then access it from the public address. I'm guessing you're currently using localhost to access your server?
run your server say port 8080 and then forward the port in your router so it's public to the internet. Then visit your webpage/phpscript from http://72.184.212.85:8080 instead of http://localhost:8080.
Here is a ridiculous solution that I wouldn't recommend:
Register your home IP with a domain name, then see where the request came from via URL:
$url = $_SERVER["SERVER_NAME"];
or
$url = $_SERVER["HTTP_HOST"];
and then do a dns lookup of that result, which should return the IP it's registered to, ie your external IP.
$ext_ip = gethostbyaddr($url);
The only reason this wouldn't work (so sorry if I'm wrong), is if SERVER_NAME uses the same method as "REMOTE_HOST", which is a reverse DNS lookup, which won't resolve, as your internal IP won't be registered to that domain name. An easy way to check is to do either:
phpinfo();
and see what the environmental variables are.

Categories