当前位置:首页 » 代码教程 » PHP和JAVASCRIPT判断访客终端是电脑还是手机

PHP和JAVASCRIPT判断访客终端是电脑还是手机

当用户使用手机等移动终端访问网站时,我们可以通过程序检测用户终端类型,如果是手机用户,则引导用户访问适配手机屏幕的移动站点。本文将介绍分别使用PHP和JAVASCRIPT代码判断用户终端类型。

当用户使用手机等移动终端访问网站时,我们可以通过程序检测用户终端类型,如果是手机用户,则引导用户访问适配手机屏幕的移动站点。本文将介绍分别使用PHP和JAVASCRIPT代码判断用户终端类型。


PHP版

我们使用PHP的$_SERVER['HTTP_USER_AGENT']来获取手机用户浏览器的用户代理,然后匹配已有的各种手机浏览器代理库,如果含有匹配的关键字,则判断为手机(移动终端)用户。

emlog程序为例:第一步,找到模板文件module.php,在其中加入如下代码:
<?php //判断手机参数
function sywap(){ 
$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","iphone","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"); 
$sywap = false;foreach($mobile_agents as $device){if(stristr($user_agent, $device)) {$sywap = true;break;}}return $sywap;}?>
在需要放置的地方用如下代码:
<?php if(sywap()){?>是手机端显示参数
<?php }else{?>不是手机端显示参数
<?php }?>

以下代码为舍力自用跳转代码,请不必理会!!!
<?php if(sywap()){header('Location:http://www.shuyong.net/wap/');?>
<?php }else{header('Location:http://www.shuyong.net/');//不是手机访问跳转到电脑版?>
<?php }?>
12
End
温馨提示:如果您对本文有疑问,请在本页留言即可!!!
觉得很赞 (0)付款方式
X
付款方式:
  • 微信
  • 支付宝
  • QQ红包

使用微信扫一扫
blogger
    发布日期:2015-08-08   所属类别:代码教程   浏览:19506次   评论:7次
    本文地址:http://www.shuyong.net/chengxu/794.html [ 百度已收录 ]
    声明:本页信息由网友自行发布或来源于网络,真实性、合法性由发布人负责,请仔细甄别!本站只为传递信息,我们不做任何双方证明,也不承担任何法律责任。文章内容若侵犯你的权益,请联系本站删除!

留言/评论:◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

填写好QQ号码,任点空白处自动获取

昵称

邮箱

网址

5楼、我我 [回复该留言]
2017-09-02 04:09
你这是跳转不是显示,不对?
舍力 [回复该留言]
2017-09-02 04:46
@我我:仔细看教程,还不会就自己慢慢摸索吧。
4楼、友吧网 [回复该留言]
2017-09-01 14:12
没有效果,我想向你网站这效果,手机菜单跟电脑菜单不一样,直接说方法。可以吗?或者发邮箱,谢谢!
舍力 [回复该留言]
2017-09-02 00:42
@友吧网:方法教程不是给你了么?还要什么?要我帮我做好么?这样就要收费了哦
3楼、无言 [回复该留言]
2016-08-03 11:04
看看看
2楼、白洋淀旅游 [回复该留言]
2016-06-26 01:42
我来找代码了。
嘿嘿。
1楼、网站目录 [回复该留言]
2015-10-24 03:27
拿过来用用看,最近正在找这个代码,谢谢了

个人资料

舍力

博主:舍力

Emlog交流群

    简单的道理,复杂的人生!

博客统计

  • 文章数量:1133篇
  • 文章评论:3520条
  • 总访问量:10299633次
  • 微语数量:81条
  • 在线人数:1人
  • 本站运行:11年2月28天
  • 估计博主很忙,已经很久没更新了!
Q2 虚位以待
Copyright © 2024 舍力博客 版权所有   粤ICP备11021420号
基于宝塔面板,数据存储于阿里云ECS
Powered by emlog / &Author 舍力博客.