
目标:
======
根据用户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
2, 权限,路径
domain.php文件需要有读写的权限
开始引入domain文件正确,但是后台添加城市的时候没有写入到domain文件,这时候应该查看权限和写入文件时的路径(Linux区分大小写,尤其Windows下开发,服务器是Linux要格外注意)。