ThinkPHP,根据ip定位,切换城市

Wesley13
• 阅读 515

ThinkPHP,根据ip定位,切换城市

目标:

======

根据用户ip地址打开对应地区的分站。

实现:

<?php //入口文件 index.php
function p($msg) {
    echo "<pre>\r\n";
    print_r($msg);
    echo "</pre>\r\n";
}
function v($msg) {
    echo "<pre>\r\n";
    var_dump($msg);
    echo "</pre>\r\n";
}
if (ini_get('magic_quotes_gpc')) {
    function stripslashesRecursive(array $array) {
        foreach ($array as $k => $v) {
            if (is_string($v)) {
                $array[$k] = stripslashes($v);
            } else if (is_array($v)) {
                $array[$k] = stripslashesRecursive($v);
            }
        }
        return $array;
    }
    $_GET = stripslashesRecursive($_GET);
    $_POST = stripslashesRecursive($_POST);
}
//当前目录 非THINKPHP框架需要的路径
define('BASE_PATH', getcwd());
//多城市版本解决方案
define('MY_CITY', true);
define('MY_DOMAIN', 'www.jjab.top'); //设置根域名
static $IpLocation = null;
if (MY_CITY) {
    $city = array();
    $domains = require BASE_PATH . '/Application/Conf/domain.php';
    if ($_SERVER['HTTP_HOST'] == MY_DOMAIN || $_SERVER['HTTP_HOST'] == 'www.' . MY_DOMAIN) { //这种情况下要通过IP来判断一下当前城市!
        require BASE_PATH . '/ThinkPHP/Extend/Library/ORG/Net/IpLocation.class.php'; //包含IP
        $IpLocation = new IpLocation('UTFWry.dat'); // 实例化类 参数表示IP地址库文件
        $result = $IpLocation->getlocation($_SERVER['REMOTE_ADDR']);
        //p($result);
        // Array
        //(
        //    [ip] => 106.117.15.xxx
        //    [beginip] => 106.117.0.0
        //    [endip] => 106.117.48.255
        //    [country] => 河北省石家庄市
        //    [area] => 电信
        //)
        //p($domains);
        foreach ($domains['list'] as $val) {
            if (strstr($result['country'], $val['name'])) {
                $city = $val;
                break;
            }
        }
        //p($city);
        //$city = $domains['default']; //目前直接默认,如果需要后期再改~~
        if (empty($city)) {
            $city = $domains['default'];
        }
        //p($city);
        if (!empty($city)) {
            // print_r($_SERVER);
            // header("Location: http://".$city['domain'].$_SERVER['REQUEST_URI']);die;
            
        }
        // die('该城市站点不存在!#02');
        
    } else {
        foreach ($domains['list'] as $val) {
            if ($_SERVER['HTTP_HOST'] == $val['domain']) {
                $city = $val;
                break;
            }
        }
        if (empty($city)) $city = '';
        define('DB_FILE', $val['domain']); //如果选定了城市,那么就使用选定的城市数据库!
        
    }
}
//多城市解决方案结束
if (!file_exists(BASE_PATH . '/attachs/install.lock')) {
    header("Location: install/index.php");
    die;
}
define('PATH_HOST', md5($_SERVER['HTTP_HOST']));
//调试模式
ini_set('display_errors', 'On');
//error_reporting(0);
define('APP_DEBUG', true);
//定义项目名称
define('APP_NAME', 'Application');
ini_set('date.timezone', 'Asia/Shanghai');
define('NOW_TIME', $_SERVER['REQUEST_TIME']);
define('TODAY', date('Y-m-d', NOW_TIME));
//定义项目路径
define('APP_PATH', BASE_PATH . '/Application/');
header("Content-type: text/html; charset=utf-8");
//加载框架入文件
require './ThinkPHP/ThinkPHP.php';

CommonAction.class.php

<?php
Class CommonAction extends Action {
    protected function _initialize() {
        global $domains, $city;
        if (!cookie('area')) cookie('area', idtoname($city['enname'], 'Area', 'area_id', 'enname'));
    }
}

遇到的问题:

    1, strstr

                php strstr

    2, 权限,路径

                domain.php文件需要有读写的权限

                开始引入domain文件正确,但是后台添加城市的时候没有写入到domain文件,这时候应该查看权限和写入文件时的路径(Linux区分大小写,尤其Windows下开发,服务器是Linux要格外注意)。

点赞
收藏
评论区
推荐文章
Wesley13 Wesley13
2年前
java——20171121
!(http://a.51jsoft.com/uploads/default/original/1X/c542896b094a42a5653fb75adf6cdacd6e35d12e.png)!(https://static.oschina.net/uploads/space/2017/1121/210719_G80Z_3715033.png)
Wesley13 Wesley13
2年前
java 用sevlet实现ip定位以及天气预报的功能
最近两天想实现一个登陆网站就可以自动显示该地区的的天气情况。很是苦恼。慢慢研究然后才其所得。研究的思路大致是这样的。ip定位–通过位置获取天气。首先声明一下,以前国家气象局的接口已经被封,以前直接传一个json数据就有天气情况,现在需要手动解析下。所以,这样的实际思路为:ip获取地址(操作获取城市名称)百度下载各城市对应编号
Stella981 Stella981
2年前
PHP+echarts 简单人物关系图
开发语言:PHP需求:人物关系图插件echarts 说明:实现功能 复杂功能请自行根据业务逻辑 echarts链接地址如下:http://echarts.baidu.com/echarts2/doc/example/force.html粗略截图:!(https://static.oschina.net/uploads/spac
Wesley13 Wesley13
2年前
IP地址查询接口及调用方法
设计蜂巢IP地址查询接口:http://www.hujuntao.com/api/ip/ip.php腾讯IP地址查询接口:http://fw.qq.com/ipaddress新浪IP地址查询接口:http://int.dpool.sina.com.cn/iplookup/iplookup.php?formatjs搜狐I
Stella981 Stella981
2年前
MapReduce简单使用
1、启动hadoop工程!(http://static.oschina.net/uploads/space/2015/0510/202542_tg2o_1863482.png)(http://static.oschina.net/uploads/space/2015/0510/202542_tg2o_1863482.pn
Stella981 Stella981
2年前
Linux 文件权限详解
查看文件详细信息!(http://static.oschina.net/uploads/space/2015/0325/112532_bIms_1783617.png)参数说明!(http://static.oschina.net/uploads/space/2015/0325/112531_qT2V
Stella981 Stella981
2年前
Html5 SVG矢量图像的使用
!(http://static.oschina.net/uploads/space/2015/0814/214834_RVHb_2256215.jpg)!(http://static.oschina.net/uploads/space/2015/0814/214924_2Ios_2256215.jpg)SVG中文参考地址: moz
Wesley13 Wesley13
2年前
MongoDB分片搭建
!(http://static.oschina.net/uploads/space/2015/1021/150555_H4rD_2426299.jpg)(http://static.oschina.net/uploads/space/2015/1021/150555_H4rD_2426299.jpg)·2015年度PG大象会报名地址:http
Stella981 Stella981
2年前
PhoneGap2.9 IOS项目 BUG修复
1:IOS7状态栏覆盖页面问题:前!(http://static.oschina.net/uploads/space/2015/0616/161136_mhiL_152736.jpg)后!(http://static.oschina.net/uploads/space/2015/0616/161136_osKr_152736.jpg)
Stella981 Stella981
2年前
PHP+jQuery寥寥几行代码轻松实现百度搜索那样的无刷新PJAX的分页列表和导航链接
!(https://static.oschina.net/uploads/space/2016/1208/171419_U00R_561214.png)PHP寥寥几行代码轻松实现百度搜索那样的分页列表和导航链接,某些语言的拥趸哭晕在厕所.<?php$apparray('db_prefix''