首页 > 解决方案 > 仅在具有 IP 的选定区域中显示内容

问题描述

是否可以仅向美国显示特殊网页内容?我可以用访问者的 IP 做到这一点吗?

标签: phpip

解决方案


当然可以。请尝试如下:

<?php

$ip = $_SERVER['REMOTE_ADDR'];

$result = file_get_contents( "http://echoip.ir/" . $ip );

if ( $result ) {

    $result_decode = json_decode( $result, true );

    if ( $result_decode['ip2location']['country_short'] == "US" ) {

      // Show your page

    }

}

?>

推荐阅读