获取当前IP地址,跳转到对应城市网站
1,通过获取当前IP地址
2,调用新浪AIP,获取到当前城市
3,将中文转换为拼音后跳转
<?php include './pinyin.php'; //获取当前ip function getIp(){ $onlineip=''; if(getenv('HTTP_CLIENT_IP')&&strcasecmp(getenv('HTTP_CLIENT_IP'),'unknown')){ $onlineip=getenv('HTTP_CLIENT_IP'); } elseif(getenv('HTTP_X_FORWARDED_FOR')&&strcasecmp(getenv('HTTP_X_FORWARDED_FOR'),'unknown')){ $onlineip=getenv('HTTP_X_FORWARDED_FOR'); } elseif(getenv('REMOTE_ADDR')&&strcasecmp(getenv('REMOTE_ADDR'),'unknown')){ $onlineip=getenv('REMOTE_ADDR'); } elseif(isset($_SERVER['REMOTE_ADDR'])&&$_SERVER['REMOTE_ADDR']&&strcasecmp($_SERVER['REMOTE_ADDR'],'unknown')){ $onlineip=$_SERVER['REMOTE_ADDR']; } return $onlineip; } //获取城市信息api function getLocation($ip){ $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, "http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=".$ip); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30); $str = curl_exec($curl); curl_close($curl); return $str; } //当前ip地址 $currentIP = getIp(); //通过当前ip获取信息 $getLocation = getLocation($currentIP); $currentInfo = json_decode($getLocation, true); //判断ip是否为有效 if($currentInfo['ret'] == '-1') { $currentInfo['city'] = 'unknown'; } //当前城市中文名 $currentCityName = $currentInfo['city']; $currentCityEName = $pin->Pinyin("$currentCityName",'UTF8'); //城市拼音多音字 switch($currentCityEName) { case 'zhongqing': $currentCityEName = 'chongqing'; break; case 'shenfang': $currentCityEName = 'shifang'; break; case 'chengdou': $currentCityEName = 'chengdu'; break; case 'yueshan': $currentCityEName = 'leshan'; break; case 'junxian': $currentCityEName = 'xunxian'; break; case 'shamen': $currentCityEName = 'xiamen'; break; case 'zhangsha': $currentCityEName = 'changsha'; break; case 'weili': $currentCityEName = 'yuli'; break; case 'zhaoyang': $currentCityEName = 'chaoyang'; break; case 'danxian': $currentCityEName = 'shanxian'; break; default: $currentCityEName = $pin->Pinyin("$currentCityName",'UTF8'); break; } //重定向浏览器 header("Location: http://www.huanhang.com/?city=$currentCityEName"); exit;
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)