-3

I would like to get the visitor IP by using any online service like https://www.ipstack.com or https://www.ipify.org/ but the problem when I use PHP code it returns the server IP. I understand that because the code runs in the server and its return the server IP, But how could I get the visitor IP? I dont want to use REMOTE_ADDR or so.

I tried getting the visitor IP by using getJSON but the problem then how to pass the data to PHP code and use it there.

======UPDATE======

Other pages not solving the issue so please dont add links..

The issue I want to use this api: http://api.ipstack.com/check?access_key=xxxxx&output=json

Here we will get the data as JSON.. But how to pass the data to PHP?

==========UPDATE========== The code I am using right now to get the visitor IP..

foreach (array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR') as $key) {
    if (array_key_exists($key, $_SERVER) === true) {
        foreach (array_map('trim', explode(',', $_SERVER[$key])) as $ip) {
            if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) !== false) {
                return $ip;
            }
        }
    }
}

Thanks

Mohammed
  • 390
  • 3
  • 7
  • Also [How to get client's IP address using JavaScript?](https://stackoverflow.com/questions/391979/how-to-get-clients-ip-address-using-javascript) – Herohtar Dec 19 '18 at 20:32
  • 6
    "I dont want to use REMOTE_ADDR or so." But that's what `REMOTE_ADDR` is **for**... – ceejayoz Dec 19 '18 at 20:33
  • The api's you mention take an IP address to return other details, you still have to provide an IP to them, they cant know the IP of a visitor your site –  Dec 19 '18 at 20:41
  • You can get the IP but using check for example http://api.ipstack.com/checkaccess_key=YOUR_ACCESS_KEY – Mohammed Dec 19 '18 at 20:47
  • If you want to use that API, per https://ipstack.com/documentation#standard, you'd just do `https://api.ipstack.com/{$_SERVER['REMOTE_ADDR']}?access_key=YOUR_ACCESS_KEY` – ceejayoz Dec 19 '18 at 20:49
  • I want to use check,, I want to get the IP Remote_addr not working fine always I think..... http://api.ipstack.com/check ? access_key=YOUR_ACCESS_KEY – Mohammed Dec 19 '18 at 20:51
  • Read the docs. `check` checks **your** IP - you can't pass it your visitor's IP. Pass them `$_SERVER['REMOTE_ADDR']` as I instructed and it'll work just fine. – ceejayoz Dec 19 '18 at 20:52
  • Side note: You've just shared your private API key. You should not do that, and you should generate a new API key so others can't use your 10,000 free requests. – ceejayoz Dec 19 '18 at 21:01
  • Yes I know that's thanks for the note. I just want to solve the issue :) – Mohammed Dec 19 '18 at 21:02

1 Answers1

0

The issue I want to use this api: http://api.ipstack.com/check?access_key=xxxxx&output=json

That /check API is a shortcut to looking up the IP address making that API call. If you make it from your server via PHP, it will always return your server's IP address.

Use the standard lookup and pass it the user's REMOTE_ADDR, as documented at https://ipstack.com/documentation#standard:

$url = "https://api.ipstack.com/{$_SERVER['REMOTE_ADDR']}?access_key=YOUR_ACCESS_KEY";
$json = file_get_contents($url);
ceejayoz
  • 176,543
  • 40
  • 303
  • 368
  • But then the REMOTE_ADDR is the same the Visitor IP? right? so why should use ipstack? coz I just want to get the IP for the visitor. – Mohammed Dec 19 '18 at 21:01
  • If you just want the visitor IP and not all the additional geolocation info that API provides, just use `$_SERVER['REMOTE_ADDR']` in your PHP. That's what it's there for. – ceejayoz Dec 19 '18 at 21:01
  • Thanks Ceejayoz, Well A bit worried as client say sometimes it dose not work well. I added the code above. I am using it now, Is that should work fine? – Mohammed Dec 19 '18 at 21:05
  • @Mohammed You should ask your client what their *specific* concerns are with `REMOTE_ADDR`. If you're behind something like a CDN like CloudFlare or a load balancer, you may have to configure your server correctly to handle `X-Forwarded-For`. – ceejayoz Dec 19 '18 at 21:08
  • OK Thanks a lot, Its a bit complicated :(, any documentation about how to deal with IPs and so on.? – Mohammed Dec 19 '18 at 21:15
  • 1
    Just try with `$_SERVER['REMOTE_ADDR']` and see if you get the right results. Don't overcomplicate things until you have to. – ceejayoz Dec 19 '18 at 21:17