I cannot get my ip address in php - 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

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)

Not able to connect android app running on phone with localhost

I am making an Android Application using GCM. I am trying to register through my app running on phone, the inputs are not able to store on localhost XAMPP using php. My phone and laptop are running on the same network. There is some problem with the ip address of localhost. I am using this url for saving on local host, where 192.168.43.247 is the ip address when pc is running with the help of phone's 3G network.
192.168.43.247/gcm_server_php/register.php
Please help!
Open Xampp>apache>conf>http-conf file in notepad.
Find Listen 80 and comment out using #(like #Listen 80) and write below it: Listen (static ip address of server):80
eg:
Listen 192.168.1.34:80
If you want to use your application on emulator with localhost database(Xampp) then you have to use the IP address 10.0.2.2 instead of other local ip address .. if you want to use your aap on mobile then you you can use same ip .. press up arrow on left side if my answer is usefull for you
try to check by disabling firewall or use ipconfig /all command then get your ip from there and use this ip as server ip address.
If still facing error then it might be error of your local server the request may be block or something else that you need to find out.to check it try to open url in your android mobile browser.

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')

Getting strange IP address from server call (PHP, Drupal)

I'm calling $_SERVER["REMOTE_ADDR"] and it returns '::1'
Any ideas why am I getting this strange output? How to get a proper ip?
::1 is the IPv6 equivalent of 127.0.0.1 which is the IP address of your local (loopback) interface.
More information here: http://en.wikipedia.org/wiki/Localhost
How to get a proper ip?
You can disable IPv6 in your webserver to only serve IPv4 hosts. This way, you will only get propers IP adresses in $_SERVER["REMOTE_ADDR"]

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