首页 > 其他分享 >获取当前IP地址,跳转到对应城市网站

获取当前IP地址,跳转到对应城市网站

时间:2023-02-27 22:45:22浏览次数:30  
标签:case currentCityEName 跳转 应城市 break 获取 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;

 

标签:case,currentCityEName,跳转,应城市,break,获取,IP地址
From: https://www.cnblogs.com/xiaobingch/p/17162258.html

相关文章

  • ip地址、子网掩码、网关
    IP地址IPv4IP地址=网络地址+主机地址(又称:主机号和网络号组成)IP地址192.168.1.168,子网掩码255.255.255.0作为示例:192.168.100.168(IP地址)=192.168.1.0(网络......
  • LeetCode算法训练 93.复原IP地址 78.子集 90.子集II
    欢迎关注个人公众号:爱喝可可牛奶LeetCode算法训练93.复原IP地址78.子集90.子集IILeetCode93.复原IP地址分析字符串全部由数字组成,ipv4每一段数字不能有前导0,且......
  • Vue2路由跳转传参,获取路由参数,Vue监听路由
    1this.$router.push({2//name:路由组件名3name:routeName,4query:{5mapId:this.mapId6}7})89this.$router.push({1......
  • uniapp app跳转至支付宝然后返回app
    有需求:用支付宝支付时(第一次支付需授权)跳转至支付宝授权,然后再返回app跳转支付宝:<button@click="toZFB">支付宝</button>toZFB(){leturls='https......
  • linux配置ip地址
    1.配置ip地址:1cd/etc/sysconfig/network-scripts/2vimifcfg-ens332.刷新网络服务:1servicenetworkrestart-->centos72ifupens33-->centos83.关闭f......
  • Activity创建和跳转
    首先,在layout目录下创建XML文件方法一:方法二:快速创建Activity能够在清单文件上的activity自动配置Android:MainActivity3  创建与文件对应的Java代码创建并在acti......
  • uni-app:从webview跳转到应用内其他页面(hbuilderx 3.7.3)
    一,代码:1,页面代码:<template><view><web-view:fullscreen="true":style="{height:height+'px'}":webview-styles="webviewStyles":src='websrc'allow......
  • 书签项目可以跳转ChatGPT,浏览器新必应展示。
    等了一个星期,终于可以访问新必应了,话不多说看截图:然后这篇博客的项目也是接入了ChatGPT,国内可以正常访问博文地址喜欢的可以去下载看看,当然只是跳转ChatGPT,git开源的......
  • vue3.0 router路由跳转传参(router.push)
    方法一:<scriptlang="ts"setup>   import{useRouter}from"vue-router";   constrouter=useRouter(); //打开新界面constbb=(tuanId:any,i......
  • uni-app学习笔记之----页面跳转
    1、声明式跳转<navigatorurl="/pages/detail/detail"><button>跳转至详情页</button></navigator><navigatorurl="/pages/index/index"open-type="switchTab"......