wurfl php,javascript - 浏览器详细信息检测的最佳实践-我应该在客户端(使用JavaScript)还是在服务器端(使用PHP和WURFL)进行检测 - 堆栈内存溢出...

这篇博客介绍了一种在服务器端检测用户代理以根据浏览器类型调整HTML内容的方法。作者使用PHP函数来识别不同类型的浏览器(如桌面、移动设备或爬虫),并据此决定加载不同的HTML。此技术有助于优化不同设备上的用户体验。
摘要由CSDN通过智能技术生成

我通常会在服务器端检测浏览器。

我检测浏览器以更改加载应用程序之前呈现的html的原因。 这就是为什么服务器端检测最适合我的用例的原因。

我使用了很久以前拾起的这个php函数

/* USER-AGENTS This is my custom function to add contact logic...

================================================== */

function check_user_agent ( $type = NULL ) {

$user_agent = strtolower ( $_SERVER['HTTP_USER_AGENT'] );

if ( $type == 'bot' ) {

// matches popular bots

if ( preg_match ( "/googlebot|adsbot|yahooseeker|yahoobot|msnbot|watchmouse|pingdom\.com|feedfetcher-google/", $user_agent ) ) {

return true;

// watchmouse|pingdom\.com are "uptime services"

}

} else if ( $type == 'browser' ) {

// matches core browser types

if ( preg_match ( "/mozilla\/|opera\//", $user_agent ) ) {

return true;

}

} else if ( $type == 'mobile' ) {

// matches popular mobile devices that have small screens and/or touch inputs

// mobile devices have regional trends; some of these will have varying popularity in Europe, Asia, and America

// detailed demographics are unknown, and South America, the Pacific Islands, and Africa trends might not be represented, here

if ( preg_match ( "/phone|iphone|itouch|ipod|symbian|android|htc_|htc-|palmos|blackberry|opera mini|iemobile|windows ce|nokia|fennec|hiptop|kindle|mot |mot-|webos\/|samsung|sonyericsson|^sie-|nintendo/", $user_agent ) ) {

// these are the most common

return true;

} else if ( preg_match ( "/mobile|pda;|avantgo|eudoraweb|minimo|netfront|brew|teleca|lg;|lge |wap;| wap /", $user_agent ) ) {

// these are less common, and might not be worth checking

return true;

}

}

return false;

} ?>

然后,我创建一个对此函数的调用

<?php $mobilehtml="";

$ismobile = check_user_agent('mobile');

if($ismobile) {

echo "$mobilehtml";

}

else {

echo "$browserhtml";

}

?>

我希望这有帮助。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值