How do I determine whether it's a mobile devic

2019-01-10 17:45发布

I am writing a website with PHP. Since it will need to be accessed by anyone on the network to access the internet I have to create a mobile version. How do I best check if it's a mobile device? I don't want to have a switch statement with 50 devices at the end since I don't only want to support the iPhone.

Is there a PHP class I could use?

标签: php mobile
11条回答
SAY GOODBYE
2楼-- · 2019-01-10 18:16

For detection based on User-Agent, use WURFL database. At least it's comprehensive and continually updated.

If you target only high-end(ish) phones, then you may not need to detect them at all, just embed appropriate mobile stylesheets.

查看更多
▲ chillily
3楼-- · 2019-01-10 18:17

Traditionally mobile devices have been detected by comparing the HTTP User-Agent header against a list of well known mobile UA strings. A novel approach instead tries to detect the presence of a desktop OS - anything which is found to not be a desktop OS must then be mobile.

This results in far less false positives.

I've written a post with sample code in Python here: http://notnotmobile.appspot.com

Here is a snippet:

import re

# Some mobile browsers which look like desktop browsers.
RE_MOBILE = {
    "iphone" : re.compile("ip(hone|od)", re.I),
    "winmo" : re.compile("windows\s+ce", re.I)}

RE_DESKTOP = {
    "linux" : re.compile(r"linux", re.I),
    "windows" : re.compile(r"windows", re.I),
    "mac" : re.compile(r"os\s+(X|9)", re.I),
    "solaris" : re.compile(r"solaris", re.I),
    "bsd" : re.compile(r"bsd", re.I)}

# Bots that don't contain desktop OSs.
RE_BOT = re.compile(r"(spider|crawl|slurp|bot)")


def is_desktop(user_agent):
  # Anything that looks like a phone isn't a desktop.
  for regex in RE_PHONE.values():
    if regex.search(user_agent) is not None:
      return False

  # Anything that looks like a desktop probably is.
  for regex in RE_DESKTOP.values():
    if regex.search(user_agent) is not None:
      return True

  # Bots get the desktop view.
  if RE_BOT.search(user_agent) is not None:
    return True

  # Anything else is probably a phone!
  return False

def get_user_agent(request):
  # Some browsers put the User-Agent in a HTTP-X header
  if 'HTTP_X_OPERAMINI_PHONE_UA' in request.headers:
    return request.headers['HTTP_X_OPERAMINI_PHONE_UA']
  elif:
    # Skyfire / Bolt / other mobile browsers
    ...
  else:
    return request.headers.get('HTTP_USER_AGENT', '')

def view(request):
  user_agent = get_user_agent(request)
  if is_desktop(user_agent):
    return desktop_response()
  else:
    return mobile_response()
查看更多
ら.Afraid
4楼-- · 2019-01-10 18:19

You should look at Tera-WURFL, it is a PHP & MySQL-based software package that detects mobile devices and their capabilities. Here is the Tera-WURFL code that you would use to detect if the visiting device is mobile:

<?php
require_once("TeraWurfl.php");
$wurflObj = new TeraWurfl();
$wurflObj->GetDeviceCapabilitiesFromAgent();
if($wurflObj->capabilities['product_info']['is_wireless_device']){
    // this is a mobile device
}else{
    // this is a desktop device
}
?>    
查看更多
够拽才男人
5楼-- · 2019-01-10 18:21

What is a mobile device? Weaker CPU? Lower bandwidth? In reality, it has a screen the resolution of which is below 320x240 and color depth is below 24.

You have to use Javascript also. This link will give you an idea: http://www.w3schools.com/js/tryit.asp?filename=tryjs_browsermonitor

And, this link will teach you what is what: http://www.w3schools.com/htmldom/dom_obj_screen.asp

查看更多
beautiful°
6楼-- · 2019-01-10 18:24

For the redirection part, I used

$arr = explode('.', $_SERVER['SERVER_NAME'], 2);
$sub=$arr[0];
$need_redirect=false;
if (!isset($_SERVER['HTTP_REFERER'])){
    $need_redirect=true;
}else{
    $domain = parse_url($_SERVER['HTTP_REFERER']);   
    $host = $domain['host'];
    if (!preg_match('/romajidesu\.com/', $host)){
        $need_redirect=true;        
    }    
}
if ($need_redirect && ($sub!='m') && is_mobile() ){
    $old_url=$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; 
    $new_url='http://'.str_replace('www.', 'm.', $old_url);
    header("Location:".$new_url);die();
}

For more detail of my implmentation, please read my blog at http://haibuihoang.blogspot.com/2012/11/how-to-redirect-mobile-users-to-your.html

查看更多
兄弟一词,经得起流年.
7楼-- · 2019-01-10 18:25

If you want adapt the content to any particular device e.g. to resize images to be the width of the device, then you can also use DeviceAtlas. Based on the useragent of the requesting device, it will tell you the size of the screen, along with supported image formats, supported markup types, maximum page size and so on.

查看更多
登录 后发表回答