个人博客 > 学无止境 > PHP > Thinkphp通过入口文件判断移动端和PC端

Thinkphp通过入口文件判断移动端和PC端

代码如下:

<?php
// 检测PHP环境
if(version_compare(PHP_VERSION,'5.3.0','<')) die('require PHP > 5.3.0 !');
// 开启调试模式 建议开发阶段开启 部署阶段注释或者设为falsedefine('APP_DEBUG',True);
define('MULTI_MODULE', false);
// 获取当前文件所在的目录
define('DKSITE_TSDIR', dirname(__FILE__));
// 定义应用目录
define('APP_PATH',DKSITE_TSDIR.'/Application/');
function is_mobile(){   
	$user_agent = $_SERVER['HTTP_USER_AGENT'];
	$mobile_agents = Array("240x320","acer","acoon","acs-","abacho","ahong","airness","alcatel","amoi","android","anywhereyougo.com","applewebkit/525","applewebkit/532","asus","audio","au-mic","avantogo","becker","benq","bilbo","bird","blackberry","blazer","bleu","cdm-","compal","coolpad","danger","dbtel","dopod","elaine","eric","etouch","fly ","fly_","fly-","go.web","goodaccess","gradiente","grundig","haier","hedy","hitachi","htc","huawei","hutchison","inno","ipad","ipaq","ipod","jbrowser","kddi","kgt","kwc","lenovo","lg ","lg2","lg3","lg4","lg5","lg7","lg8","lg9","lg-","lge-","lge9","longcos","maemo","mercator","meridian","micromax","midp","mini","mitsu","mmm","mmp","mobi","mot-","moto","nec-","netfront","newgen","nexian","nf-browser","nintendo","nitro","nokia","nook","novarra","obigo","palm","panasonic","pantech","philips","phone","pg-","playstation","pocket","pt-","qc-","qtek","rover","sagem","sama","samu","sanyo","samsung","sch-","scooter","sec-","sendo","sgh-","sharp","siemens","sie-","softbank","sony","spice","sprint","spv","symbian","tablet","talkabout","tcl-","teleca","telit","tianyu","tim-","toshiba","tsm","up.browser","utec","utstar","verykool","virgin","vk-","voda","voxtel","vx","wap","wellco","wig browser","wii","windows ce","wireless","xda","xde","zte");
	$is_mobile = false;   
	foreach ($mobile_agents as $device){
	  //这里把值遍历一遍,用于查找是否有上述字符串出现过     
		if (stristr($user_agent, $device)){ 
		  //stristr 查找访客端信息是否在上述数组中,不存在即为PC端。      
		  $is_mobile = true;      
		  break;     
		}   
	}   
	return $is_mobile; 
}//判断
if(is_mobile()){
	echo '手机端';
	// 手机端模块 
	define('BIND_MODULE','Mobile');
	}else{
	echo 'pc端';
	// pc端前台模块 
	define('BIND_MODULE','Home');      
} 
// 引入ThinkPHP入口文件
require dirname(__FILE__).'/ThinkPHP/ThinkPHP.php';
?>

到这里就可以通过入口文件判断是手机端还是PC端了,同理其他的CMS系统都是可以的,如果网站静态化则可以用JS来判断做跳转。

本文出自:琅枫个人博客。如需转载请注明出处!

本文出处:"https://www.phpfeng.cn/learn/php/122.html"

如果您觉得文章对你有帮助,可以进行打赏。
打赏多少,您高兴就行,谢谢您对琅枫博客的支持! ~(@^_^@)~

微信打赏

琅枫博客微信号

支付宝打赏

琅枫博客个人支付宝
本文关键词: thinkphp 入口 文件



你想在庞大的互联网上留下一丝足迹?

我不想成为一个庸俗的人。十年百年后,当我们死去,质疑我们的人同样死去,后人看到的是裹足不前、原地打转的你,还是一直奔跑、走到远方的我?

点我了解如何搭建个人博客?