Geoip with While

Asked

Viewed 45 times

0

I have this code below, and a TXT file with all IP’s. In the code below you have to set the IP directly to get the location of it, however I need it to be automated, I have the Ip’s in a file called ip.txt, I needed to create a while, for etc so that the IP’s are read and with echo bring the result. I’ve tried some unsuccessful ways, could help me?

    <?php

    require 'vendor/autoload.php';


   $gi = 


 geoip_open("/xxx/xxx/xxx/xxx/GeoLiteCity.dat",GEOIP_STANDARD);





 echo geoip_country_code_by_addr($gi, "80.24.24.24") . "\t" .
      geoip_country_name_by_addr($gi, "80.24.24.24") . "\n";
 echo geoip_country_code_by_addr($gi, "201.210.254.45") . "\t" .
      geoip_country_name_by_addr($gi, "201.210.254.45") . "\n";
 echo geoip_country_code_by_addr($gi, "124.253.51.88") . "\t" .
     geoip_country_name_by_addr($gi, "124.253.51.88") . "\n";

     geoip_close($gi);


      ?>
  • How are the data in .txt?

  • They got the Ips under each other, no other information, just the Ip’s.

  • Sorry, I edited my text, in TXT there are no countries and acronyms, but only Ips.

1 answer

1

Use the function file and eternal:

$ips = file('ips.txt');

$gi = geoip_open("/xxx/xxx/xxx/xxx/GeoLiteCity.dat",GEOIP_STANDARD);

foreach($ips as $ip) {
    echo geoip_country_code_by_addr($gi, $ip) . "\t";
}

geoip_close($gi);
  • Good Afternoon @Marcelo, I used your example now, however, it just reads the " t"and the return is an echo skipping a line, without returning any information. If I put it in place of the $ip variable, any IP, e.g.: (24.24.24.24) it returns several US, which is the location of this IP. You know the reason why?

  • Give a var_dump($ips) and shows what the result.

  • Sorry, I’m a little layman still in PHP, give this var_dump out of the right foreach? if it is the result is: syntax error, Unexpected T_STRING in

  • The var_dump($ips); goes right after the $ips = file('ips.txt');

  • [657]=> string(14) "191.190.58.56 " [658]=> string(16) "204.191.149.146 " [659]=> string(13) "179.233.5.94 " [660]=> string(14) "203.86.153.18 " [661]=&& string(15) "177.156.150.97&Xa;"#####662]=> string(15) "177.156.150.97 " [663]=> string(13) "187.37.83.76 " [664]=> string(16) "220.143.174.218 " [665]=> string(15) "74.101.206.183 " [666]=> string(14) "179.218.2.171 " [667]=> string(14) "12.249.95.238

  • The exit is correct, inside the foreach, how is the exit of var_dump($ip);?

  • with var_dump($ip) is : PHP Notice: Undefined variable: ip in /home/mmatos/projects/projectospammer/ipinfo.php on line 8 NULL

  • I put the var_dump inside the foreach and the result is : string(16) "220.143.174.218 " string(15) "74.101.206.183 " string(14) "179.218.2.171 " string(14) "12.249.95.238

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.