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

目标:

======

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

实现:

1<?php //入口文件 index.php 2function p($msg) { 3    echo "<pre>\r\n"; 4    print_r($msg); 5    echo "</pre>\r\n"; 6} 7function v($msg) { 8    echo "<pre>\r\n"; 9    var_dump($msg); 10    echo "</pre>\r\n"; 11} 12if (ini_get('magic_quotes_gpc')) { 13    function stripslashesRecursive(array $array) { 14        foreach ($array as $k => $v) { 15            if (is_string($v)) { 16                $array[$k] = stripslashes($v); 17            } else if (is_array($v)) { 18                $array[$k] = stripslashesRecursive($v); 19            } 20        } 21        return $array; 22    } 23    $_GET = stripslashesRecursive($_GET); 24    $_POST = stripslashesRecursive($_POST); 25} 26//当前目录 非THINKPHP框架需要的路径 27define('BASE_PATH', getcwd()); 28//多城市版本解决方案 29define('MY_CITY', true); 30define('MY_DOMAIN', 'www.jjab.top'); //设置根域名 31static $IpLocation = null; 32if (MY_CITY) { 33    $city = array(); 34    $domains = require BASE_PATH . '/Application/Conf/domain.php'; 35    if ($_SERVER['HTTP_HOST'] == MY_DOMAIN || $_SERVER['HTTP_HOST'] == 'www.' . MY_DOMAIN) { //这种情况下要通过IP来判断一下当前城市! 36        require BASE_PATH . '/ThinkPHP/Extend/Library/ORG/Net/IpLocation.class.php'; //包含IP 37        $IpLocation = new IpLocation('UTFWry.dat'); // 实例化类 参数表示IP地址库文件 38        $result = $IpLocation->getlocation($_SERVER['REMOTE_ADDR']); 39        //p($result); 40        // Array 41        //( 42        //    [ip] => 106.117.15.xxx 43        //    [beginip] => 106.117.0.0 44        //    [endip] => 106.117.48.255 45        //    [country] => 河北省石家庄市 46        //    [area] => 电信 47        //) 48        //p($domains); 49        foreach ($domains['list'] as $val) { 50            if (strstr($result['country'], $val['name'])) { 51                $city = $val; 52                break; 53            } 54        } 55        //p($city); 56        //$city = $domains['default']; //目前直接默认,如果需要后期再改~~ 57        if (empty($city)) { 58            $city = $domains['default']; 59        } 60        //p($city); 61        if (!empty($city)) { 62            // print_r($_SERVER); 63            // header("Location: http://".$city['domain'].$_SERVER['REQUEST_URI']);die; 64             65        } 66        // die('该城市站点不存在!#02'); 67         68    } else { 69        foreach ($domains['list'] as $val) { 70            if ($_SERVER['HTTP_HOST'] == $val['domain']) { 71                $city = $val; 72                break; 73            } 74        } 75        if (empty($city)) $city = ''; 76        define('DB_FILE', $val['domain']); //如果选定了城市,那么就使用选定的城市数据库! 77         78    } 79} 80//多城市解决方案结束 81if (!file_exists(BASE_PATH . '/attachs/install.lock')) { 82    header("Location: install/index.php"); 83    die; 84} 85define('PATH_HOST', md5($_SERVER['HTTP_HOST'])); 86//调试模式 87ini_set('display_errors', 'On'); 88//error_reporting(0); 89define('APP_DEBUG', true); 90//定义项目名称 91define('APP_NAME', 'Application'); 92ini_set('date.timezone', 'Asia/Shanghai'); 93define('NOW_TIME', $_SERVER['REQUEST_TIME']); 94define('TODAY', date('Y-m-d', NOW_TIME)); 95//定义项目路径 96define('APP_PATH', BASE_PATH . '/Application/'); 97header("Content-type: text/html; charset=utf-8"); 98//加载框架入文件 99require './ThinkPHP/ThinkPHP.php';

CommonAction.class.php

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

遇到的问题:

    1, strstr

                php strstr

    2, 权限,路径

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

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

点赞
收藏

评论区

加载中...

相关推荐

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

java——20171121

!(http://a.51jsoft.com/uploads/default/original/1X/c542896b094a42a5653fb75adf6cdacd6e35d12e.png)!(https://static.oschina.net/uploads/space/2017/1121/210719_G80Z_3715033.png)

PHP+echarts 简单人物关系图

开发语言:PHP需求:人物关系图插件echarts 说明:实现功能 复杂功能请自行根据业务逻辑 echarts链接地址如下:http://echarts.baidu.com/echarts2/doc/example/force.html粗略截图:!(https://static.oschina.net/uploads/spac

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

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

Linux 文件权限详解

查看文件详细信息!(http://static.oschina.net/uploads/space/2015/0325/112532_bIms_1783617.png)参数说明!(http://static.oschina.net/uploads/space/2015/0325/112531_qT2V