CodeIgniter Mobile OS Detection

I was able to detect which mobile device the user was using with CodeIgniter, but I could not determine which operating system was working with the current mobile device.

Say someone uses a Samsung mobile device that runs on Android, and another uses the regular Java mobile operating system, which is still Samsung. How can I check which operating system each user is running on?

+6
source share
6 answers

Download the library from http://mobiledetect.net Place Mobile_Detect.php in the "library"

inside the 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 at http://dwij.co.in/mobile-os-detection-in-php-codeigniter

+17
source

I borrowed / stole this class loading method from phodexcel codeigniter integration.

Download the library from http://mobiledetect.net , but put Mobile_Detect.php in 'third_party', then create MobileDetect.php in 'library' 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, for example:

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

I am sure that there are other (even better) ways to integrate mobiledetect into codeigniter, I just wanted to share how I did it, I hope that it will be useful.

A few notes:

1) You do not need to use the MobileDetect.php stub file, if you put Mobile_Detect.php directly into the "libraries", you can still use it without $detect = new Mobile_Detect(); instead, call functions that like it: $this->mobile_detect->isMobile()

2) The name of the stub class can be anything if you follow the recommendations of CodeIgniter. For example, you can use "MD" as the class name and then refer to it using $this->md->isMobile()

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

+4
source

Download lib.

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

use this function to detect a mobile phone

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

Download the library from https://github.com/serbanghita/Mobile-Detect Copy the Mobile_Detect.php directory to the third_party directory Create a helper class in the encoder // This function will return the user's 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 learn more about the codeigniter helper Function https://ellislab.com/codeigniter/user-guide/general/helpers.html

+1
source

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

0
source

CodeIgniter integrated browser or agent detection support in the encoder

Inside the controller, use the following code example:

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

Source: https://habr.com/ru/post/946271/


All Articles