Home » Php » php – CodeIgniter mobile OS detection

php – CodeIgniter mobile OS detection

Posted by: admin July 12, 2020 Leave a comment

Questions:

I’ve been able to detect what mobile device a user is using with CodeIgniter, but I haven’t been able to detect what operating system the current mobile device is running.

Let’s say someone is using a Samsung mobile device that runs on Android, and another is using a normal Java mobile operating system that’s still Samsung. How can I check to see the which operating system each user is on?

How to&Answers:

Download library from http://mobiledetect.net
Put Mobile_Detect.php in to ‘libraries’

inside main controller

public function index() {
    $this -> load -> library('Mobile_Detect');
    $detect = new Mobile_Detect();
    if ($detect->isMobile() || $detect->isTablet() || $detect->isAndroidOS()) {
        header("Location: ".$this->config->item('base_url')."/mobile"); exit;
    }
}

Find documentation on https://dwij.net/mobile-os-detection-in-php-codeigniter/

Answer:

I borrowed/stole this method of loading the class from the phpexcel codeigniter integration.

Download library from http://mobiledetect.net, but put Mobile_Detect.php in ‘third_party’ then create MobileDetect.php in ‘libraries’ and put the following code in it:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once APPPATH."third_party/Mobile_Detect.php";

class MobileDetect extends Mobile_Detect {
  public function __construct() {
    parent::__construct();
  }
}

Now you can use it in your controllers like this:

$this->load->library('MobileDetect');
if ($this->mobiledetect->isMobile()) {
    //do something cool;
}

I’m sure there are other (even better) ways to integrate mobiledetect into codeigniter, I just wanted to share the way I did it, I hope it’s helpful.

A couple of notes:

1) You don’t have to use the stub file MobileDetect.php, if you put Mobile_Detect.php directly in ‘libraries’ you can still use it without $detect = new Mobile_Detect(); instead call the functions liked this: $this->mobile_detect->isMobile()

2) The name of the stub file class can be anything you want as long as you follow CodeIgniter guidelines. So for example you could use ‘MD’ as the class name and then reference it with $this->md->isMobile()

3) I recommend adding if ( ! defined('BASEPATH')) exit('No direct script access allowed'); after the opening <?php of Mobile_Detect.php to prevent direct access to the class.

Answer:

Load lib.

$this->load->library('user_agent');

use this function to detect is mobile

$mobile=$this->agent->is_mobile();
if($mobile){
  //your code
}

Answer:

Download Library from https://github.com/serbanghita/Mobile-Detect
Copy the Mobile_Detect.php into third_party directory
Create a helper class in codeigniter
//This function will return the user agent phone or tablet or computer

if(!function_exists('is_MTC')):
    function is_MTC()
    {
        require(APPPATH .'third_party/Mobile_Detect.php');
        $detect = new Mobile_Detect;
        return ($detect->isMobile() ? ($detect->isTablet() ? 'tablet' : 'phone') : 'computer');
    }
endif;

In the view You can directly call the is_MTC function and check the user agent
//This will print the user agent

<?php echo is_MTC(); ?>

To know more about codeigniter helper Function https://ellislab.com/codeigniter/user-guide/general/helpers.html

Answer:

if you use the session class there is a variable built right in. user_agent

Answer:

CodeIgniter have built in support for browser or agent detection in codeigniter

Inside controller use the following sample code:

$this->load->library('user_agent');
if ($this->agent->is_mobile()) {
    // Is a mobile browser
} else {
     // Is a Desktop/Bot User Agent
}