php - 仅在具有 IP 的选定区域中显示内容
问题描述
是否可以仅向美国显示特殊网页内容?我可以用访问者的 IP 做到这一点吗?
解决方案
当然可以。请尝试如下:
<?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
}
}
?>
推荐阅读
- java - 如果我们在 Spring Batch 中执行特定方法,如何在 tasklet 中获取作业参数
- django - 如果用户在 Django 中有 3 次或更少的失败登录尝试,则隐藏 reCatpcha 错误消息
- javascript - 如何从 AuthenticationResult 创建客户端认知会话?
- sql - 如何在保留结果值的同时合并列?
- android - If-else 在 android studio 中意外工作。为什么?
- apexcharts - APEX JET Chart 组合图
- javascript - 使用反应更改动态表头的字体大小
- c# - 使用 specflow/vstest 运行特定目标
- root - 如何在 shell(bash)命令行中确定运行脚本的虚拟主机是否在没有 root 权限的情况下共享
- linux - 如何从每一行中获取特定索引处的字符?