Simplest way to detect a mobile device in PHP

后端 未结 15 2330
别跟我提以往
别跟我提以往 2020-11-22 05:12

What is the simplest way to tell if a user is using a mobile device to browse my site using PHP?

I have come across many classes that you can use but I was hoping fo

相关标签:
15条回答
  • 2020-11-22 05:24

    In case you care about screen size you can store screen width and Height as cookie values if they do not exists yet and then make self page redirection.

    Now you have cookies on client and server side and can use it to determine mobile phones, tablets and other devices

    Here a quick example how you can do it with JavaScript. Warning! [this code contain pseudo code].

    if (!getcookie("screen_size")) {
        var screen_width = screen.width;
        var screen_height = screen.height;
        setcookie("screen_size", screen_width+", " +screen_height);
        go2(geturl());
    }
    
    0 讨论(0)
  • 2020-11-22 05:25

    You only need to include user_agent.php file which can be found from Mobile device detection in PHP page and use the following code.

    <?php
    //include file
    include_once 'user_agent.php';
    
    //create an instance of UserAgent class
    $ua = new UserAgent();
    
    //if site is accessed from mobile, then redirect to the mobile site.
    if($ua->is_mobile()){
       header("Location:http://m.codexworld.com");
       exit;
    }
    ?>
    
    0 讨论(0)
  • 2020-11-22 05:33

    I found mobile detect to be really simple and you can just use the isMobile() function :)

    0 讨论(0)
  • 2020-11-22 05:33
    function isMobileDev(){
        if(isset($_SERVER['HTTP_USER_AGENT']) and !empty($_SERVER['HTTP_USER_AGENT'])){
           $user_ag = $_SERVER['HTTP_USER_AGENT'];
           if(preg_match('/(Mobile|Android|Tablet|GoBrowser|[0-9]x[0-9]*|uZardWeb\/|Mini|Doris\/|Skyfire\/|iPhone|Fennec\/|Maemo|Iris\/|CLDC\-|Mobi\/)/uis',$user_ag)){
              return true;
           };
        };
        return false;
    }
    
    0 讨论(0)
  • 2020-11-22 05:33
    <?php 
    
    //-- Very simple way
    $useragent = $_SERVER['HTTP_USER_AGENT']; 
    $iPod = stripos($useragent, "iPod"); 
    $iPad = stripos($useragent, "iPad"); 
    $iPhone = stripos($useragent, "iPhone");
    $Android = stripos($useragent, "Android"); 
    $iOS = stripos($useragent, "iOS");
    //-- You can add billion devices 
    
    $DEVICE = ($iPod||$iPad||$iPhone||$Android||$iOS);
    
    if (!$DEVICE) { ?>
    
    <!-- What you want for all non-mobile devices. Anything with all HTML, PHP, CSS, even full page codes-->
    
    <?php }else{ ?> 
    
    <!-- What you want for all mobile devices. Anything with all HTML, PHP, CSS, even full page codes --> 
    
    <?php } ?>
    
    0 讨论(0)
  • 2020-11-22 05:33

    PHP device detection from 51Degrees.com does exactly what you want - detects mobile devices and various properties associated with detected devices. It is simple to use and requires no maintenance. Set up is done in 4 easy steps:

    1. Download the Zip file from http://sourceforge.net/projects/fiftyone/.
    2. Unzip the file into a directory in your PHP server.
    3. Then add the following code to your PHP page:
    4. require_once 'path/to/core/51Degrees.php';
      require_once 'path/to/core/51Degrees_usage.php';
      
    5. All available device information will be contained in $_51d array:
    6. if ($_51d['IsMobile'])
      {
          //Start coding for a mobile device here.
      }
      

    51Degrees device detector does not use regular expressions for detections. Only important parts of HTTP headers are used to match devices. Which makes this solution the fastest (5 000 000 detections per second on mediocre hardware) and most accurate (99.97% accuracy) as hundreds of new devices are added to the database weekly (Supported device types include consoles, smart TVs, e-readers, tablets and more).

    Software is open source distributed under Mozilla Public License 2 and compatible with commercial and open source projects. As a bonus 51Degrees solution also contains a complementary PHP image optimiser that can automatically resize images for mobile devices.

    By default 51Degrees PHP device detector uses Lite data file which is free and contains over 30000 devices and 50 properties for each device. Lite file is updated once every 3 month. If you want to have a higher level of details about requesting mobile devices, then Premium and Enterprise data files are available. Premium contains well over 70000 devices and 100 properties for each device with weekly updates. Enterprise is updated daily and contains over 150000 devices with 150 properties for each.

    Full list of device properties.
    Compare data files.

    0 讨论(0)
提交回复
热议问题