天天看點

屏蔽國内或者國外ip的代碼 GeoIP

首先下載下傳兩個檔案

GeoIP-1.4.8.tar.gz                                 
GeoIP.tar.gz        

<?php

//計時開始

    function utime() {

        $time = explode( " ", microtime() );

        $usec = (double)$time[0];

        $sec = (double)$time[1];

        return $usec + $sec;

    }

    $startTimes = utime();

    include("./getIP/geoip.inc");

    // open the geoip database

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

    // 擷取國家代碼  '220.181.111.86'

    $country_code = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);

    echo "Your country code is: <strong>$country_code</strong> <br />";

    // 擷取國家名稱

    $country_name = geoip_country_name_by_addr($gi, $_SERVER['REMOTE_ADDR']);

    echo "Your country name is: <strong>$country_name</strong> <br />";

    // close the database

    geoip_close($gi);

    //運作結束時間

    $endTimes = utime();

    $runTimes = sprintf( '%0.4f', ( $endTimes - $startTimes ) );

    echo "Processed in " . $runTimes . "second.";

?>

可能你運作這段代碼之後發現什麼都沒有輸出來,原因是因為你在本地測試,位址是127.0.0.1 自然不能确定你的國家,你可以手動的加入ip,比如'220.181.111.86',運作你就知道了!