谁一开始就是大神?

yii如何判断是手机访问还是电脑访问[2.0]

大神好 求yii如何判断是手机访问还是电脑访问

帅哥,不用纠结了,Yii官方是没有提供方法去判断来源是移动设备还是PC的,包括其核心代码以及扩展代码。
在服务器和客户端的 HTTP 交互中,客户端会通过请求头中的 User-Agent 告知服务器客户端到底是啥玩意。你可以直接获取 这个 User-Agent,不过基于 CGI 协议,PHP可以直接用 $_SERVER['HTTP_USER_AGENT']获取这个 User-Agent。但不幸的是,它是一大堆字符串,人们虽然可能可以通过其直接判断是移动设备还是PC,但程序却很麻烦,毕竟难免有所疏漏之处。所以有专门的人士去做自然最好。
https://github.com/serbanghita/Mobile-Detect,这个程序可以的。star 六千,还专门为 Yii 写个了扩展,应该不会辱没帅哥你的程序的。。
https://www.baidu.com/s?wd=如何判断是手机访问还是电脑访问&ie=UTF-8
composer下一个
可以使用 $_SERVER[‘HTTP_USER_AGENT’] 来进行判断~,然后可以继承 baseModel来封装成一个类中的判断方法~希望可以帮到你
yii框架没有的。 可以用这个 http://www.yiichina.com/tutorial/346
https://github.com/serbanghita/Mobile-Detect/wiki/Code-examples
<?php
// These lines are mandatory.
require_once ‘Mobile_Detect.php’;
$detect = new Mobile_Detect;
<?php
// Basic detection.
$detect->isMobile();
$detect->isTablet();
// Magic methods.
$detect->isIphone();
$detect->isSamsung();
// […]
// Alternative to magic methods.
$detect->is(‘iphone’);
// Find the version of component.
$detect->version(‘Android’);
// Additional match method.
$detect->match(‘regex.*here’);
// Browser grade method.
$detect->mobileGrade();
// Batch methods.
$detect->setUserAgent($userAgent);
$detect->setHttpHeaders($httpHeaders);
<?php
// Check for mobile environment.
if ($detect->isMobile()) {
}
<?php
// Check for tablet device.
if($detect->isTablet()){
}
<?php
// Check for any mobile device, excluding tablets.
if ($detect->isMobile() && !$detect->isTablet()) {
}
<?php
// Keep the value in $_SESSION for later use
// and for optimizing the speed of the code.
if(!$_SESSION[‘isMobile’]){
}
<?php
// Redirect the user to your mobile version of the site.
if($detect->isMobile()){
}
<?php
// Include and instantiate the class.
require_once ‘Mobile_Detect.php’;
$detect = new Mobile_Detect;
// Any mobile device (phones or tablets).
if ( $detect->isMobile() ) {
}
// Any tablet device.
if( $detect->isTablet() ){
}
// Exclude tablets.
if( $detect->isMobile() && !$detect->isTablet() ){
}
// Check for a specific platform with the help of the magic methods:
if( $detect->isiOS() ){
}
if( $detect->isAndroidOS() ){
}
// Alternative method is() for checking specific properties.
// WARNING: this method is in BETA, some keyword properties will change in the future.
$detect->is(‘Chrome’)
$detect->is(‘iOS’)
$detect->is(‘UCBrowser’)
$detect->is(‘Opera’)
// […]
// Batch mode using setUserAgent():
$userAgents = array(
‘Mozilla/5.0 (Linux; Android 4.0.4; Desire HD Build/IMM76D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19’,
‘BlackBerry7100i/4.1.0 Profile/MIDP-2.0 Configuration/CLDC-1.1 VendorID/103’,
// […]
);
foreach($userAgents as $userAgent){
$detect->setUserAgent($userAgent);
$isMobile = $detect->isMobile();
$isTablet = $detect->isTablet();
// Use the force however you want.
}
// Get the version() of components.
// WARNING: this method is in BETA, some keyword properties will change in the future.
$detect->version(‘iPad’); // 4.3 (float)
$detect->version(‘iPhone’) // 3.1 (float)
$detect->version(‘Android’); // 2.1 (float)
$detect->version(‘Opera Mini’); // 5.0 (float)
// […]
这个tp框架封装了的,直接复制出来用
自己写了一个方法,方法有点笨,使用了多种判断的方法,不过用起来还算是有用些,可以试一试!

赞(0) 打赏
未经允许不得转载:菜鸟之家 » yii如何判断是手机访问还是电脑访问[2.0]

评论 抢沙发

登录

找回密码

注册