javascript - PHP怎样识别手机与电脑的不同访问
我想应该是用脚本语言实现的吧。
回复内容:
像很多网站一样,访问时会识别为手机版或桌面版。
我想应该是用脚本语言实现的吧。
User-Agent最常用
或者JavaScript判断屏幕宽度、分辨率,然后重定向到相应的mobile版
一般是根据 useragent来判断的。
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)) { $is_mobile = true; break; } } return $is_mobile; }
也可参考 https://github.com/easychen/LazyPHP/b...
一般从浏览器的 UA 判断浏览器和系统
还有像上面说的用的响应式设计,个人暂时倾向于判断UA
我也来个大礼包,直接看代码就好。
/** * 手机访问重定向 * * @return void */ public static function mobile_location() { $_SERVER['HTTP_USER_AGENT'] = empty($_SERVER['HTTP_USER_AGENT']) ? '' : $_SERVER['HTTP_USER_AGENT']; $_SERVER['HTTP_ACCEPT'] = empty($_SERVER['HTTP_ACCEPT']) ? '' : $_SERVER['HTTP_ACCEPT']; $_SERVER['HTTP_VIA'] = empty($_SERVER['HTTP_VIA']) ? '' : $_SERVER['HTTP_VIA']; $browser = ''; switch (true) { // ipad case (preg_match('/ipad/i', $_SERVER['HTTP_USER_AGENT'])): $browser = 'ipad'; break; // Apple/iPhone browser renders as mobile case (preg_match('/Android/i', $_SERVER['HTTP_USER_AGENT'])): $browser = "android"; break; // Apple/iPhone browser renders as mobile case (preg_match('/(apple|iphone|ipod)/i', $_SERVER['HTTP_USER_AGENT']) && preg_match('/mobile/i', $_SERVER['HTTP_USER_AGENT'])): $browser = "iphone"; break; // Apple/iPhone browser renders as mobile case (preg_match('/IEMobile\/9\.0/i', $_SERVER['HTTP_USER_AGENT'])): $browser = "wp7"; break; // Other mobile browsers render as mobile case (preg_match('/(blackberry|configuration\/cldc|hp |hp-|htc |htc_|htc-|iemobile|kindle|midp|mmp|motorola|mobile|nokia|opera mini|opera mobi|palm|palmos|pocket|portalmmm|ppc; |smartphone|sonyericsson|sqh|spv|symbian| treo|up.browser|up.link|vodafone|windows ce|xda |xda_)/i', $_SERVER['HTTP_USER_AGENT'])): $browser = "mobile"; break; // Wap browser case (((strpos(strtolower($_SERVER['HTTP_ACCEPT']), 'text/vnd.wap.wml') > 0) || (strpos(strtolower($_SERVER['HTTP_ACCEPT']), 'application/vnd.wap.xhtml+xml') > 0)) || ((isset($_SERVER['HTTP_X_WAP_PROFILE']) || isset($_SERVER['HTTP_PROFILE'])))): $browser = "mobile"; break; // Shortend user agents case (in_array(strtolower(substr($_SERVER['HTTP_USER_AGENT'], 0, 3)), array('lg ' => 'lg ', 'lg-' => 'lg-', 'lg_' => 'lg_', 'lge' => 'lge'))); $browser = "mobile"; break; // More shortend user agents case (in_array(strtolower(substr($_SERVER['HTTP_USER_AGENT'], 0, 4)), array('acs-' => 'acs-', 'amoi' => 'amoi', 'doco' => 'doco', 'eric' => 'eric', 'huaw' => 'huaw', 'lct_' => 'lct_', 'leno' => 'leno' , 'mobi' => 'mobi', 'mot-' => 'mot-', 'moto' => 'moto', 'nec-' => 'nec- ', 'phil' => 'phil', 'sams' => 'sams', 'sch-' => 'sch-', 'shar' => 'shar', 'sie-' => 'sie-', 'wap_' => 'wap_', 'zte- ' => 'zte-'))); $browser = "mobile"; break; // Render mobile site for mobile search engines case (preg_match('/Googlebot-Mobile/i', $_SERVER['HTTP_USER_AGENT']) || preg_match('/YahooSeeker\/M1A1-R2D2/i', $_SERVER['HTTP_USER_AGENT'])): $browser = "mobile"; break; case (preg_match('/wap/i', $_SERVER['HTTP_VIA'])): $browser = "mobile"; break; } return $browser; }
useragent 判断,这是语言无关的
user-agent 用来判断操作系统和浏览器类型、版本
一般手机都支持css3,可以用media query对不同屏幕宽度的手机返回不同样式的网页
还能用响应式前端布局,非php
UA+屏幕尺寸,分辨率等结合起来用,因为有些手机自带的浏览器不发ua的~
以下的javascript代码以及php代码可以用来判断iPhone或者iPod – 360weboy – 专注php web开发 | web前端开发。
Javascript
if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) { window.location = "http://www.360weboy.com/iphone"; }
本人更倾向于使用服务器端语言,比如php来做判断:
Php
if(strstr($_SERVER['HTTP_USER_AGENT'],'iPhone') || strstr($_SERVER['HTTP_USER_AGENT'],'iPod')) { header('Location: http://www.360weboy.com/iphone'); exit(); }
360weboy – 专注php web开发 | web前端开发。感兴趣的话可以加入360weboy的qq群-197642724,或者关注360weboy的新浪或者腾讯微博,大家在一起分享讨论开发中的技术以及思想。
给两个通道 应该能解决问题
可以用js判断 比如
http://tangram.baidu.com/api#baidu.pl...
或者是media query
http://www.qianduan.net/responsive-we...