php判断终端是手机还是电脑访问网站的思路及代码

代码一:


代码如下:

<?php
function check_wap() {
if (isset($_SERVER['HTTP_VIA'])) return true;
if (isset($_SERVER['HTTP_X_NOKIA_CONNECTION_MODE'])) return true;
if (isset($_SERVER['HTTP_X_UP_CALLING_LINE_ID'])) return true;
if (strpos(strtoupper($_SERVER['HTTP_ACCEPT']),"VND.WAP.WML") > 0) {
// Check whether the browser/gateway says it accepts WML.
$br = "WML";
} else {
$browser = isset($_SERVER['HTTP_USER_AGENT']) ? trim($_SERVER['HTTP_USER_AGENT']) : '';
if(empty($browser)) return true;
$mobile_os_list=array('Google Wireless Transcoder','Windows CE','WindowsCE','Symbian','Android','armv6l','armv5','Mobile','CentOS','mowser','AvantGo','Opera Mobi','J2ME/MIDP','Smartphone','Go.Web','Palm','iPAQ');
$mobile_token_list=array('Profile/MIDP','Configuration/CLDC-','160×160','176×220','240×240','240×320','320×240','UP.Browser','UP.Link','SymbianOS','PalmOS','PocketPC','SonyEricsson','Nokia','BlackBerry','Vodafone','BenQ','Novarra-Vision','Iris','NetFront','HTC_','Xda_','SAMSUNG-SGH','Wapaka','DoCoMo','iPhone','iPod');
$found_mobile=checkSubstrs($mobile_os_list,$browser) ||
checkSubstrs($mobile_token_list,$browser);
if($found_mobile)
$br ="WML";
else $br = "WWW";
}
if($br == "WML") {
return true;
} else {
return false;
}
}
function checkSubstrs($list,$str){
$flag = false;
for($i=0;$i<count($list);$i++){
if(strpos($str,$list[$i]) > 0){
$flag = true;
break;
}
}
return $flag;
}
if(check_wap()){
echo "wap";
}else{
echo "web";
}
?>

代码二:

代码如下:

<?php
header("Content-type:text/html;charset=utf-8");
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;
}
if(is_mobile()){
echo "手机";
}else{
echo "电脑";
}

(0)

相关推荐

  • PHP使用CURL实现对带有验证码的网站进行模拟登录的方法

    网上的很多模拟登录程序,大都是通过服务程序apache之类的运行,获取到验证码之后显示在网页上,然后填上再POST出去,这样虽然看起来很友好,但是既然模拟登录,登录后所干的事情就不一定是短时间完成的,所以这就要受到php最大执行时间的限制,而且有些操作还有可能权限不足. 本文提供了一个程序实例,思路就是获取到验证码之后把验证码存储为一个图片,然后程序休眠20秒,在20秒之后由用户手动查看图片,并把验证码填写到code.txt文件中,20秒休眠完成后,程序会读code.txt的验证码,这样再带着验

  • php网站判断用户是否是手机访问的方法

    随着移动设备的普及,网站也会迎来越来越多移动设备的访问.用适应PC的页面,很多时候对手机用户不友好,那么有些时候,我们需要判断用户是否用手机访问,如果是手机的话,就跳转到指定的手机友好页面.这里就介绍一下,如何判断用户是否用手机访问. 自定义的函数如下: $agent = check_wap(); if( $agent ) { header('Location: http://www.jb51.net'); exit; } // check if wap function check_wap()

  • PHP实现批量检测网站是否能够正常打开的方法

    本文实例讲述了PHP实现批量检测网站是否能够正常打开的方法.分享给大家供大家参考,具体如下: curl_setopt函数是php中一个重要的函数,它可以模仿用户的一些行为,如模仿用户登录,注册等等一些用户可操作的行为. <?php //设置最大执行时间是 120秒 ini_set('max_execution_time',120); function httpcode($url){ $ch = curl_init(); $timeout = 3; curl_setopt($ch,CURLOPT_

  • 禁止IP访问网站的多种方法分享(linux,php,nginx,apache)

    PHP禁止某个IP或IP段访问 废话不多说,客官请看: <? //禁止某个IP $banned_ip = array ( "127.0.0.1", //"119.6.20.66", "192.168.1.4" ); if ( in_array( getenv("REMOTE_ADDR"), $banned_ip ) ) { die ("您的IP禁止访问!"); } //禁止某个IP段 $ban_ran

  • PHP检查网站是否宕机的方法示例

    本文实例讲述了PHP检查网站是否宕机的方法.分享给大家供大家参考,具体如下: <?php function Networkcheck($url){ $agent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:42.0) Gecko/20100101 Firefox/42.0"; //curl_init-初始化一个curl会话 $ch=curl_init(); //curl_setopt - 为一个curl设置会话参数 curl_setopt(

  • php检测用户是否用手机(Mobile)访问网站的类

    下载地址http://code.google.com/p/php-mobile-detect/ 程序就是一个文件,下载之后直接引用就可以,基于MIT License协议开源的PHP程序 使用方法: 复制代码 代码如下: <?phpinclude("Mobile_Detect.php");$detect = new Mobile_Detect(); if ($detect->isMobile()) {    // any mobile platform    echo 'is

  • php提取字符串中网站url地址的方法

    本文实例讲述了php提取字符串中网站url地址的方法.分享给大家供大家参考.具体分析如下: 今天写一个问答系统上线之后发现有很多人发链接了,由于业务部门要我们过滤掉网站地址了,下面我给大家分享一个提取字符串url地址函数,代码如下: 复制代码 代码如下: $postInfo['answer2'] ='可以的,商业贷款可摊还36%,公积金贷款可摊还16%|||可以先把账户里的余额提取出来用作首付,然后每个月贷款商业贷款可摊还36%,公积金贷款可摊还16%|||可以的,现在甲类公积金是摊还比例htt

  • phpsir 开发 一个检测百度关键字网站排名的python 程序

    源码如下 :保存成utf-8 bd.py 文件 复制代码 代码如下: #!/usr/bin/env python # -*- coding: utf-8 -*- import sys import urllib ,urllib2 import re def baidu(w): url= "http://www.baidu.com/s?" values = { "w":w.encode('gbk','ignore') } data = urllib.urlencode

  • PHP如何解决网站大流量与高并发的问题

    首先,确认服务器硬件是否足够支持当前的流量. 普通的P4服务器一般最多能支持每天10万独立IP,如果访问量比这个还要大, 那么必须首先配置一台更高性能的专用服务器才能解决问题 ,否则怎么优化都不可能彻底解决性能问题. 其次,优化数据库访问. 前台实现完全的静态化当然最好,可以完全不用访问数据库,不过对于频繁更新的网站, 静态化往往不能满足某些功能. 缓存技术就是另一个解决方案,就是将动态数据存储到缓存文件中,动态网页直接调用 这些文件,而不必再访问数据库,WordPress和Z-Blog都大量使

  • PHP获取网站域名和地址的代码

    复制代码 代码如下: <? function PMA_getenv($var_name) { if (isset($_SERVER[$var_name])) { return $_SERVER[$var_name]; } elseif (isset($_ENV[$var_name])) { return $_ENV[$var_name]; } elseif (getenv($var_name)) { return getenv($var_name); } elseif (function_exi

随机推荐