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要格外注意)。

你可能感兴趣的:(thinkphp,根据ip定位,切换城市)