如何在cpanel中顯示國家、城市名稱?

如何在cpanel中顯示國家、城市名稱?

我正在使用 Cpanel,我想在登入頁面中顯示國家/地區名稱和城市名稱,如下所示: 例如:

 Your IP address is :  xxx.xxx.xxx.xxx
 Your Country name is: USA 
 Your City name is:  Seattle

對於以上信息,我可以透過以下 php 代碼顯示 IP 位址:

<?php echo $_SERVER['REMOTE_ADDR']; ?>

如何顯示其他使用者訊息,例如國家名稱和城市名稱?

請逐步指導我如何做。

答案1

您可能想要使用地理位置API

ipinfo.io提供簡單的純文字回傳接口,但與許多地理定位 API 一樣,如果超出某些限制,則需要付費版本的服務(即,對於 ipinfo.io,您需要每天超過 1000 個請求或 SSL)。

以 ipinfo.io 的非 JSON 版本為例,您的 PHP 程式碼可能如下所示:

<?php 

    // This turns on error display without messing with php.ini
    // Delete the following two lines in production

    ini_set('display_errors',1); 
    error_reporting(E_ALL);

    // We avoid using $_SERVER['REMOTE_ADDR'] directly with a custom variable
    $ip = $_SERVER['REMOTE_ADDR'];

    // Otherwise, using the $_SERVER['REMOTE_ADDR'] directly
    //$city = file_get_contents('http://ipinfo.io/'. $_SERVER['REMOTE_ADDR']. '/city');
 
    // Using our custom $ip variable

    $city = file_get_contents('http://ipinfo.io/'. $ip. '/city');
    $country = file_get_contents('http://ipinfo.io/'. $ip. '/country');

    //$region = file_get_contents('http://ipinfo.io/'. $ip. '/region');
    

    // An alternate formatting of City State, Country
    //echo $city.' '.$region.', ' .$country;

    // Print our variables. <br> is a standard HTML line break.
    echo 'Your IP address is:   '.$ip;
    echo '<br>';
    echo 'Your Country name is: '.$country;
    echo '<br>'; 
    echo 'Your City name is:    '.$city;
?>

您可以使用 // 省略所有行,因為這些只是註解/範例。同樣,錯誤顯示行 (ini_set/error_reporting) 僅用於偵錯。變數前後的句點對於串聯是必需的。 URL 連接到

 Ex. http://ipinfo.io/123.123.123.123/city

並以這種形式傳回純文字。查看ipinfo.io 開發者頁面有關可以退貨的更多想法。上面的程式碼回傳:

Ex.
    Your IP address is :  xxx.xxx.xxx.xxx
    Your Country name is: US
    Your City name is:  Las Vegas

或者,如果您想要“United States”與“US”,您也可以嘗試類似的內容Geobytes 城市詳細信息舊版 API。返回“美國”:

<?php

    function getIP() {
      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 (explode(',', $_SERVER[$key]) as $ip) {
             if (filter_var($ip, FILTER_VALIDATE_IP) !== false) {
                   return $ip;
                }
             } 
          }
       }
    }

    $tags=json_decode(file_get_contents('http://getcitydetails.geobytes.com/GetCityDetails?fqcn='. getIP()), true);

    // Prints all available members of the $tags array, in case we forget our options
    //print_r($tags);

    // $tags[geobytesipaddress]) creates a non-fatal error so we use '' quotes around the array elements.
    print_r('Your IP address is: ' .$tags['geobytesipaddress']);
    echo '<br>';
    print_r('Your Country name is: ' .$tags['geobytescountry']);
    echo '<br>'; 
    print_r('Your City name is:    ' .$tags['geobytescity']);

?>

這只是 Geobytes 頁面中的範例程式碼稍作修改。如所寫,它複製了第一個程式碼範例的輸出,但具有完整的國家/地區名稱:

Ex.
    Your IP address is :  xxx.xxx.xxx.xxx
    Your Country name is: United States
    Your City name is:  Las Vegas

順便說一句,Geobytes API 似乎比 ipinfo.io 支援更多選項,並允許很多更高的非付費請求率(如果重要的話)。

相關內容