4

For some reason this code only works when I change the $ip to my current IP address. Is there something that I need to do to the PHP.ini file for this code to work dynamically?

Even when I download the code from the internet it does not display, and it has nothing to do with the API I am using, I even tried PHP $_SERVER['REMOTE_ADDR'] empty that code wont work.i think there is a setting i need to do. i am using MAMP, and a MAC. this is what its displaying "::1"

<?php
// Get User IP
if(! empty($_SERVER['REMOTE_ADDR']) ){
    $ip = $_SERVER['REMOTE_ADDR'];
}
else{
    $ip = empty($_SERVER['HTTP_X_FORWARDED_FOR']) ? '' : $_SERVER['HTTP_X_FORWARDED_FOR'];
}
//Send the API request with user Ip
$ipinfoAPI = "http://ipinfo.io/{$ip}/json";
//get the APi requeted data
$load = file_get_contents($ipinfoAPI);
//Convert it to the readable format
$return = json_decode($load);
echo $return->ip;
?>
  • 1
    Turn on error reporting and/or check your error logs – WillardSolutions Jan 04 '19 at 13:42
  • After you create `$ipinfoAPI = "http://ipinfo.io/{$ip}/json";`, you should `echo` this value out on the page, copy and paste it in your browser and see if it's working. That may be pretty telling, especially if it's working with a value you input manually. – GrumpyCrouton Jan 04 '19 at 13:43
  • What I'm assuming is happening is `empty($_SERVER['REMOTE_ADDR'])` is true for some reason, and `empty($_SERVER['HTTP_X_FORWARDED_FOR'])` is also true, making `$ip = '';` – GrumpyCrouton Jan 04 '19 at 13:44
  • Why don't you use `CURL` instead of `file_get_contents()` – BadPiggie Jan 04 '19 at 13:44
  • 2
    `$_SERVER['REMOTE_ADDR']` is probably returning `::1` (IPv6) since you're using a local development solution. – frobinsonj Jan 04 '19 at 13:45
  • @BanujanBalendrakumar What's wrong with `file_get_contents()`? In my experience, it's much easier to use than CURL – GrumpyCrouton Jan 04 '19 at 13:45
  • @GrumpyCrouton As a developer we need to follow some standards. Refer this https://stackoverflow.com/questions/3488425/php-ini-file-get-contents-external-url – BadPiggie Jan 04 '19 at 13:46
  • Possible duplicate of [Should a MAMP return ::1 as IP on localhost?](https://stackoverflow.com/questions/3699454/should-a-mamp-return-1-as-ip-on-localhost) – frobinsonj Jan 04 '19 at 13:47
  • In your case you will only get loopback IP. You can't locate with that. – BadPiggie Jan 04 '19 at 13:48
  • @BanujanBalendrakumar Thanks for the link. – GrumpyCrouton Jan 04 '19 at 13:51
  • @freddyrobinson i tried using Listen 127.0.0.1:8080, it shows 127.0.0.1 not my IP address – sonqoba dubulaa Jan 04 '19 at 15:43
  • 1
    @BanujanBalendrakumar PHP's built-in curl functions are, frankly, awful; for a simple request like this I would probably jump straight from `file_get_contents` to installing [Guzzle](http://docs.guzzlephp.org/en/stable/), rather than bothering to write the boilerplate needed for curl to give me something meaningful. – IMSoP Jan 04 '19 at 15:52
  • Try dumping the contents of `$_SERVER` - what are the values of REMOTE_ADDR and HTTP_X_FORWARDED_FOR in that array? – IMSoP Jan 04 '19 at 15:53

0 Answers0