Jump to content
php.lv forumi

Ka noteikt IP adreses valsti, precizak vai ir LV vai nav


Devils666

Recommended Posts

Bez datubāzes/failu glabātuves būs ļoti lēni. Katru reizi nāksies vilkt aktuālākās adreses no nic.lv un meklēt pa tajām.

Tavs paša links uz cuuu rakstīto kodu ir labs. Piekešo pie sevis no nic.lv Latvijai izdalītās IP adreses un tajās meklē.

Link to comment
Share on other sites

skaidrs, un ok, tad lai paliek tas kods :), tikai īsti nezinu kā ar to glabāšanu failos tapec,

ka es varetu kaut kāda subfolderi saglabāt to failu?

 

  function check_lv($ip) {

   function update_ranges() {
     $f = file_get_contents('http://www.nic.lv/local.net');
     $f = explode("\n",$f);
     foreach ($f as $range) {
       // ja tie nav komentaari
       if (!(strpos($range,'#') === 0)) {
         $slash = strpos($range,'/');
         if ($slash) {
           $mask = (int)substr($range,$slash+1);
           $start = ip2long(substr($range,0,$slash));
           $end = $start|((1 << $mask)-1);
           $ranges[$start] = $end;
         }
       }
     }
     ksort($ranges,SORT_NUMERIC);
     // noglabaajam
     $f = fopen('localnet','w');
     fputs($f, serialize($ranges));
     fclose($f);
     return $ranges;
   }

   // nav jaatjauninaas? (reizi nedeeljaa)
   if ((!file_exists('localnet')) or (time() - filemtime('localnet') > 604800)) {
     $ranges = update_ranges();
   } else 
     $ranges = unserialize(file_get_contents('localnet'));

   $ipl = ip2long($ip);

   $best = 0;
   while ((list($s,$e) = each($ranges)) and ($s < $ipl)) {
     $best = $s;
   }

   if ($best) {
     if (($ipl >= $best) and ($ipl <= $ranges[$best])) 
       return true;      
   }

   return false;
 }

 if (check_lv('213.175.115.12')) {
   print('no LV');
 } else {
   print('nav no LV');
 }

 

nederetu jau tā, ka visas vietas 'localnet' vienkārsi nomainitu uz 'sistema/localnet'?

Link to comment
Share on other sites

interesanti, cik ilgi nic.lv pacietīs, ka kkāds serveris no viņiem nonstopā n-tās reizes dienā velk vienu un to pašu failu :D:D:D

sry, pasteidzos un neizlasīju nākamo funkciju. viss pareizi ;)

Edited by 2easy
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...