博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
关于方维的ip定位做法
阅读量:7143 次
发布时间:2019-06-29

本文共 2882 字,大约阅读时间需要 9 分钟。

hot3.png

最近在做关于方维的项目,方维的根据ip定位思路大概是这样的,在路由分发控制器前载入了一个叫做user_init.php的文件,查看这个人第一次访问时cookie里面和url里面是否有关于城市的记录,如果没有的话,自动跳到城市选择页,

//首次访问跳转到城市选择if(!isset($_REQUEST['act']) && intval(a_fanweC("FIRST_VISIT_CITY"))==1&&!isset($_COOKIE['had_select_city'])&&!isset($_REQUEST['cityname']) && strtolower($_REQUEST['m'])=="index"){	redirect2(a_u("City/more"));}

然后封装了一个叫getCurrentCityID的函数。如果url没有传城市时,就从cookie里的历史记录里面去找。如果cookie里面没有历史记录的话,就调用一个根据ip地址定位所在城市,然后传入session里。

function getCurrentCityID(){ 	if(!empty($_REQUEST['cityname']))	{		$cityName = trim($_REQUEST['cityname']);		$currentCity = $GLOBALS['db']->getRowCached("SELECT id,py FROM ".DB_PREFIX."group_city where py = '".$cityName."' and verify=1 and status =1");		if($currentCity){			setcookie('cityID',base64_encode(serialize($currentCity['id'])));			$_SESSION['cityID'] = $currentCity['id'];			$_SESSION['cityName'] = $currentCity['py'];						return $currentCity['id'];						}	}			$cityID = intval($_SESSION["cityID"]);		if($cityID > 0){		return $cityID;	}					if ($cityID==0){		$cityID = intval(unserialize(base64_decode($_COOKIE['cityID'])));				} 	//动态定位 	if($cityID==0)	{					//$ip =  get_ip(); 		$city_list = $GLOBALS['db']->getAllCached("SELECT id,name FROM ".DB_PREFIX."group_city where status =1 and verify = 1 order by pid desc");		//加入自动定位		foreach ($city_list as $city)		{			//if(@strpos($address['area1'],$city['name']))			if(strstr($address['area1'],$city['name'])!=false||strstr($city['name'],$address['area1'])!=false)			{				$city_sub = $GLOBALS['db']->getRow("SELECT id,name,py FROM ".DB_PREFIX."group_city where status =1 and verify= 1 and pid =".$city['id']." order by sort");				if ($city_sub){					$cityID = $city_sub['id'];					}else{					$cityID = $city['id'];					}								break;			}		}	}	if($cityID > 0)		$currentCity = $GLOBALS['db']->getRowCached("SELECT id,py FROM ".DB_PREFIX."group_city where id = $cityID and verify=1 and status =1");			if(empty($currentCity))		$currentCity = $GLOBALS['db']->getRowCached("SELECT id,py FROM ".DB_PREFIX."group_city where is_defalut=1 and verify=1 and status =1");	setcookie('cityID',base64_encode(serialize($currentCity['id'])));		$_SESSION['cityID'] = $currentCity['id'];	$_SESSION['cityName'] = $currentCity['py'];			return $currentCity['id'];		}
//获取当前城市跟客户端IPif(!isset($_SESSION['CLIENT_IP']) || empty($_SESSION['C_CITY_ID']) ||(isset($_REQUEST['cityname']) && !empty($_REQUEST['cityname']))){	require ROOT_PATH.'app/source/class/IpLocation.class.php';	define("C_CITY_ID",getCurrentCityID());	$_SESSION['C_CITY_ID'] = C_CITY_ID;		$iplocation = new IpLocation();	$client_ip = $iplocation->getIP();	$_SESSION['CLIENT_IP'] = $client_ip;		if(intval(a_fanweC("FIRST_VISIT_CITY"))==1&&isset($_REQUEST['cityname']) && !isset($_COOKIE['had_select_city'])){		setcookie('had_select_city',true,time()+365*60*60*24);	}}

最后完成获取地址定位的操作。

转载于:https://my.oschina.net/u/1414906/blog/219394

你可能感兴趣的文章
EntityType 'UserInfo' has no key defined. Define the key for this EntityType.
查看>>
Merge PDF File using itextsharp library
查看>>
使用微软的TFS云服务
查看>>
高效能TCP通讯基础组件Beetle.Express
查看>>
MyEclipse内存不足配置
查看>>
四舍五入网络Java保留两位小数
查看>>
MFC 循环界面假死的解决(MFC 按钮终止循环)
查看>>
详细解说九宫图比较常用的多控件布局
查看>>
程序员的出路在哪里?挣钱的机会来了续-福利来了,仿QQ界面,放出全部源码,打造创业框架及实现思路...
查看>>
浅析Android线程模型一 --- 转
查看>>
Cocos2d-x PluginX (二)增加新的Plugin
查看>>
python-django开发学习笔记四
查看>>
cocos2d-x开发记录:二,基本概念(导演,场景,层和精灵,场景切换,效果)...
查看>>
Binutils工具集中的一些比较常用的工具
查看>>
jsp里面实现asp.net的Global文件内容。
查看>>
Oracle ROWID
查看>>
WCF服务通信测试
查看>>
dos命令dir查找文件的用法及实例
查看>>
Hadoop守护进程【简--】
查看>>
uboot中gd的定义和使用
查看>>