PHP - $_SERVER["REMOTE_ADDR"] inconsistent - php

$_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"]

Related

gethostbyname function returns domain name instead of IP address in PHP

I want to get machine IP address in PHP so i am using function
<?php echo gethostbyname($serverName); ?>
Its works for all my server machine but it returns domain name for particular IP address for some machine. I don't know why its returning domain name for particular machine and how to get IP address of that particular machine.
Thanks in advance.
According to docs
Returns the IPv4 address or a string containing the unmodified hostname on failure.
Failure reasons may be couple (e.g. wrong domain name, unreachable host or network problems)

REMOTE_ADDR not working

I have a wierd problem. Whatever i do, the IP is the server's IP, not the client / the visitor. What to do?
if(isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARTDED_FOR'] != '') {
$ip_address = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip_address = $_SERVER['REMOTE_ADDR'];
}
return $ip_address;
}
You may need to switch to another server software (depending on what you're using now)
From the PHP docs:
$_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.
REMOTE_ADDR is in the CGI/1.1 specification though so it's odd that whatever server you're using wouldn't be returning it.
If your server is on the same network as your server, behind a router with NAT, then you might see your private IP.
If you are behind a reverse proxy $_SERVER['REMOTE_ADDR']; will always be the IP of the proxy server.
Your webserver is not configured properly (once I've had this issue on buying alias domains at my webhost provider).
However if you're unable to fix this problem, I HIGLY recommend you to check if $_SERVER['HTTP_X_FORWARDED_FOR'] is valid IP address and then just assing it to $_SERVER['REMOTE_ADDR'] for simply usage, because It is easy to fake $_SERVER['HTTP_X_FORWARDED_FOR'] and to throw there your own values (I had this problem).

Is it possible to set getaddrinfo() to only retrieve IPV4 addresses by default?

Php curl uses getaddrinfo() to resolve hostnames to IP addresses. By setting an option it is easy to make it lookup IPV4 addresses but that will take quite some doing and will be quite difficult to undo when we get IPv6 up and running properly on our networks.
Is there any way to set getaddrinfo() to default to IPv4 lookups for our entire Ubuntu server?
Thanks
Wayne
This solution is for C, not sure about php. When calling getaddrinfo, your pass a struct addrinfo providing 'hints' as to what you want to lookup. Example below
struct addrinfo *res;
struct addrinfo hints;
memset(&hints, 0, sizeof(hints));
hints.ai_family = PF_INET;
hints.ai_flags = AI_PASSIVE;
hints.ai_socktype = SOCK_STREAM;
getaddrinfo("localhost", "80", &hints, &res);
Copied below from my comment as it seemed applicable to the answer:
Just create a wrapper function for getaddrinfo that provides your
'defaults'. Then use the wrapper function throughout your application.
When ipv6 is up and running change the function's internal 'default'.
On Linux, the behaviour of getaddrinfo() can be tweaked with /etc/gai.conf. You cannot use this file to prevent IPv6 addresses being returned, but you can use it to prioritise IPv4 addresses over IPv6 addresses.
Really though, you just want PHP/Curl to pass the AI_ADDRCONFIG flag to getaddrinfo(), which will cause it to return IPv6 addresses only if the machine currently has an IPv6 address configured.

I cannot get my ip address in php

I am trying to write a small script that prints ip of the client. I am trying to run it on my localhost, but I am not able to get the IP address. Instead I get something like ::1.
Here's the code:
<?php echo $_SERVER['REMOTE_ADDR'];?>
Why this is not working as expected?
::1
Is the loopback adress (127.0.0.1 in IPv4) in IPv6.
This is the expected behaviour. If you use Firefox, you can navigate to about:config, search for disableIPv6 and set it to true. You'll then see 127.0.0.1.
So yes, it's working.
It is working correctly. That is your IP addresses... the IPv6 version of it. If you had connected over IPv4 it would have shown as 127.0.0.1.
These are loopback addresses. They allow you to connect to your own computer without using an actual network interface. See http://en.wikipedia.org/wiki/Loopback#Virtual_network_interface

Get the client's computer name

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();
?>

Categories