detect user ip address using PHP - php

i have a contact form on my website, and i'm trying to know what is the IP address of the users, i've tried this PHP code on my local testing XAMPP:
function getRealIpAddr()
{
if (!empty($_SERVER['HTTP_CLIENT_IP']))
//check ip from share internet
{
$ip=$_SERVER['HTTP_CLIENT_IP'];
}
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
//to check ip is pass from proxy
{
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
$ip=$_SERVER['REMOTE_ADDR'];
}
return $ip;
}
$ipaddress= getRealIpAddr();
echo "my ip address is" . $ipaddress;
but the results is always like
"my ip address is::1",
I don't know what's wrong with the code.

That's because you're on your local machine - ::1 is one of the IPv6 addresses of localhost. It's similar to 127.0.0.1. That's because you're accessing your script locally. If you upload this script to a server, and access it via an internet connection, you will be able to see your external IP.

::1 is the same as 127.0.0.1, but in IPv6 notation, so maybe your code is right! Can you check it on a server and compare your results with something like "whatismyip.com" output?

Related

Why the super global array element $_SERVER['SERVER_ADDR'] is not returning proper value? [duplicate]

I am trying to fetch the ip address of my machine through php. For that I am writing the code like:
<?php echo "<br />".$_SERVER['REMOTE_ADDR'];?>
But this piece of code is not working. It is returning "::1".
Please help me how to get the actual IP Address.
::1 is the actual IP. It is an ipv6 loopback address (i.e. localhost). If you were using ipv4 it would be 127.0.0.1.
If you want to get a different IP address, then you'll need to connect to the server through a different network interface.
If you are trying to run localhost, this answer will fix your problem. Just few changes on
apache2/httpd.conf
search all "listen" words
ex:
Listen 80
Make like this.
Listen 127.0.0.1:80
than restart your apache
$_SERVER[REMOTE_ADDR]
will show Listen 127.0.0.1
you can see answer in this detailed answer link
If you mean getting the user's IP address, you can do something like :
<?php
if(!empty($_SERVER['HTTP_CLIENT_IP'])){
$ip=$_SERVER['HTTP_CLIENT_IP'];
}
elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}
else{
$ip=$_SERVER['REMOTE_ADDR'];
}
?>
<?php echo "<br />".$ip;?>
It will get the user's actual IP address, regardless of proxies etc.
$_SERVER['REMOTE_ADDR'] is the IP address of the client.
$_SERVER['SERVER_ADDR'] is the IP address of the server.
Reference: http://php.net/manual/en/reserved.variables.server.php
Simple answer: You are using it on local server.
Try running
function getUserIpAddr(){
if(!empty($_SERVER['HTTP_CLIENT_IP'])){
//ip from share internet
$ip = $_SERVER['HTTP_CLIENT_IP'];
}elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
//ip pass from proxy
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}else{
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}
echo 'User Real IP - '.getUserIpAddr();
in real server. Or you can also user online php executor.
Look at the output of phpinfo(). If the address is not on that page, then the address is not available directly through PHP.

PHP : Static function not returning right value [duplicate]

I am trying to fetch the ip address of my machine through php. For that I am writing the code like:
<?php echo "<br />".$_SERVER['REMOTE_ADDR'];?>
But this piece of code is not working. It is returning "::1".
Please help me how to get the actual IP Address.
::1 is the actual IP. It is an ipv6 loopback address (i.e. localhost). If you were using ipv4 it would be 127.0.0.1.
If you want to get a different IP address, then you'll need to connect to the server through a different network interface.
If you are trying to run localhost, this answer will fix your problem. Just few changes on
apache2/httpd.conf
search all "listen" words
ex:
Listen 80
Make like this.
Listen 127.0.0.1:80
than restart your apache
$_SERVER[REMOTE_ADDR]
will show Listen 127.0.0.1
you can see answer in this detailed answer link
If you mean getting the user's IP address, you can do something like :
<?php
if(!empty($_SERVER['HTTP_CLIENT_IP'])){
$ip=$_SERVER['HTTP_CLIENT_IP'];
}
elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}
else{
$ip=$_SERVER['REMOTE_ADDR'];
}
?>
<?php echo "<br />".$ip;?>
It will get the user's actual IP address, regardless of proxies etc.
$_SERVER['REMOTE_ADDR'] is the IP address of the client.
$_SERVER['SERVER_ADDR'] is the IP address of the server.
Reference: http://php.net/manual/en/reserved.variables.server.php
Simple answer: You are using it on local server.
Try running
function getUserIpAddr(){
if(!empty($_SERVER['HTTP_CLIENT_IP'])){
//ip from share internet
$ip = $_SERVER['HTTP_CLIENT_IP'];
}elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
//ip pass from proxy
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}else{
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}
echo 'User Real IP - '.getUserIpAddr();
in real server. Or you can also user online php executor.
Look at the output of phpinfo(). If the address is not on that page, then the address is not available directly through PHP.

Get IP address of ISP with PHP

If I write in php $_SERVER['REMOTE_ADDR'] I get my ip : 176.x.x.x this address it's equal with the address who I get in cmd written ipconfig.
Now if I write in google find my ip I get another ip : 87.x.x.x
The question is: it's possible to get this ip 87.x.x.x with php?
Yes, it is possible by using $_SERVER['REMOTE_ADDR']. Only thing to do is: place your script in internet on some free/nonfree webhosting.
You get your ip: 176.x.x.x because this ip is your ip in your local network and you run webserver on your computer (or other computer) which is in local network.
Your local network is connected to internet by router. Router connects 2 networks, so routerwise you have 2 ip addresses: local network ip (176.x.x.x) and internet ip (87.x.x.x).
Depending on the webserver location you will get one of these addresses.
When you google your ip, you are looking for it in internet. Webservers placed in internet will see your internet ip address. Btw. Your local network ip is masqueraded by NAT.
The geoip_isp_by_name() function will return the name of the Internet Service Provider (ISP) that an IP is assigned to
<?php
$isp = geoip_isp_by_name('www.example.com');
if ($isp) {
echo 'This host IP is from ISP: ' . $isp;
}
?>
User the following function to get the client IP. Hope it will work
function getClientIP() {
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
} else if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}

Get the users Local IP of the visitor

We have one internet line shared with multiple users and when everyone access the internet the request are sent through same remote IP. I need to find out the request is coming from which local IP.
can anybody please help me to find out the local IP.
I have already written below code but it not giving the excepted result.
Thanks.
$ip = getenv('HTTP_CLIENT_IP');
var_dump($ip);
var_Dump($_SERVER["X-FORWARDED-FOR"]);
if (getenv('HTTP_X_FORWARDED_FOR')) {
$pipaddress = getenv('HTTP_X_FORWARDED_FOR');
$ipaddress = getenv('REMOTE_ADDR');
echo "Your Proxy IP address is : ".$pipaddress. "(via $ipaddress)" ;
} else {
$ipaddress = getenv('REMOTE_ADDR');
echo "Your IP address is : $ipaddress";
}
Since you all share the same public IP within the local network, your router uses a technique called NAT so you can share all the same ip(s), because it can be one, or several. The relation between requests and local ip is saved in a local table in the router acting as gateway.
You CAN NOT view the local ip address behind a NAT performing router. It is not a NAT problem, it is Internet addressing and packet header limitation, nothing to do with configurations you could deal with.
If this is a corporate trouble, you could do it placing the server in your local network and in that case, the $_SERVER['REMOTE_ADDR'] value will be the local IP address you are looking for.

how to get client public ip address using php?

I used
if(!empty($_SERVER['HTTP_CLIENT_IP']))
{
//check ip from share internet
$ip = $_SERVER['HTTP_CLIENT_IP'];
}
else if(!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
{
//to check ip is pass from proxy
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
else{
$ip = $_SERVER['REMOTE_ADDR'];
}
echo $ip;
to get client ip address
It is working when I test it on my local server but when I upload my files into remote server where i am hosting my web it is fetching my server ip address not client ip
Your server might be behind an internal proxy or a load balancer. Try using my PHP Utilities Functions library which has code for this.

Categories