Home > Blockchain >  How can i get client real ip address in PHP?
How can i get client real ip address in PHP?

Time:03-22

I'm trying to get my ip address.

Here is the code, the getClientIp() method uses a $_SERVER['REMOTE_ADDR'] global variable internally, so $request->getClientIp() and $_SERVER['REMOTE_ADDR'] are the same.

<?php

use Symfony\Component\HttpFoundation\Request;

$request = Request::createFromGlobals();
Http::response('IP address:' . $request->getClientIp())->json();

I have php deployed in docker on my local machine. So I send a request to localhost

http://localhost/api/v1/ip_address

and get a response.

{
    "message": "IP address:172.ХХ.Х.Х", // I replaced my ip numbers with x.
    "data": []
}

But there is a problem, the ip address that I get is different from the ip address that applications like "get my ip" give me.

You can just open Google and type in the search "find out my ip online" or "what is my ip" and they will give the correct ip, but the ip that I get from php is not.

I think this is due to the fact that I am making a request to my own computer, and not to a remote server. Can anyone explain why this is happening and if I can get around it?

Update: From php I get the internal ip address because it starts with 172.X.X...

CodePudding user response:

Your PHP app has no clue about your public IP as it is in the private network. Public IP is assigned to you by your ISP/Router. Router NATs the private IPs so only 1 IP is allocated to your private network.

"What is my IP" website is in the public internet, so it sees your public IP and it has no clue about your 172.xxx.xxx.xxx IP. Router takes care of the translation.

So, from within the app, you will always get your private IP.

If from within your app, you need to know your public IP for what ever reason, you may call the API provided by ipify.org.

curl 'https://api.ipify.org?format=json' 

For more details on how NAT works

enter image description here

Reference:

https://www.geeksforgeeks.org/network-address-translation-nat/

CodePudding user response:

"I think this is due to the fact that I am making a request to my own computer, and not to a remote server."

You kinda answered your own question here ;)

Because you are running it locally you see your private IP address

If you want to find your public ip adress check out this post

CodePudding user response:

You can only get the IP address the request comes from.

If you make a request to the same computer then you probably use a loopback IP address like 127.0.0.1 or ::1.

If you make a request to a computer on the same LAN then you'll probably use an IP address in a private address ranger.

If you make a request to a computer on the Internet then you'll probably relay the request through a router providing NAT and, as far as the server is concerned, the request will come from the routers Internet facing IP address.

You can't tell what IP addresses might be a assigned to a computer but which weren't used to make the request.

The closest you could come would be to make requests which require the use of other IPs.

e.g. explicitly to an IPv4 address and to an IPv6 address and then you could have one of the IPv4 addresses and one of the IPv6 addresses (if there are any). Likewise have a request made to a IP address explicitly on the Internet and not on the local network to get an Internet facing address.

  • Related