Tutorialsplane

Codeigniter User Agent Class Library


Codeigniter User Agent Class Library – This library used for identify the browser, mobile device and robot visiting your website. We can also use this library to detect support for the language, character set, and referrer. We can load user agent class library like this $this->load->library(‘user_agent’);. Here in this tutorial, we are going to explain how to use user agent class library.


Codeigniter user agent class library | Example.

Let us understand how user agent class library works in codeigniter with examples.

Load user agent class library

First load user agent class library to use its functions, this library can be loaded simply as below-

How to load user agent class library:

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

Class reference:-

There are following references available in user agent class library. Now we will explain.

1. is browser.

This reference is used to Returns TRUE/FALSE if the user agent is a known web browser.

is_browser([$key = NULL])

Example.

Controller code:-
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class user_agent_controller extends CI_Controller 
{
public function checkuseragent()
{
$this-?>load->library('user_agent');
$this->load->view('library/user_agent_view');	
}
}
?>
View code:-

      
<title></title> 
	

<?php if ($this-?>agent->is_browser())
{
$agent = $this->agent->browser().' '.$this->agent->version();
}
echo $agent;
echo $this->agent->platform();
?>


Output will be like this:-

2. is mobile.

This reference is used to Returns TRUE/FALSE if the user agent is a known mobile device.

is_mobile([$key = NULL])

Example.

View code:-

      
<title></title> 
	

<?php if ($this-?>agent->is_mobile('iphone'))
{
echo "User is using iphone";
}
elseif ($this->agent->is_mobile())
{
echo "User is using mobile";
}
else
{
echo "User is using Web";
}
?>


Output will be like this:-

3. is robot.

This reference is used to Returns TRUE/FALSE if the user agent is a known mobile device.

is_robot([$key = NULL])

Example.

View code:-

      
<title></title> 
	

<?php if ($this-?>agent->is_robot())
{
$roboot = $this->agent->robot();
}
echo $roboot;
?>


Output will be like this:-

4. is referral.

This reference is used to Returns TRUE/FALSE if the user agent was referred from another site.

is_referral()

5. browser.

This reference is used to Returns name of your web browser and visiting website.

browser()

6. Version.

This reference is used to Returns version number of your web browser.

version()

7. Mobile.

This reference is used to Returns name of your mobile device which is currently use in visiting website.

mobile()

8. Robot.

This reference is used to Returns name of your robot visiting site.

robot()

9. Platform.

This reference is used to Returns platform viewing your site (Linux, Windows, OS X, etc).

platform()

10. Referrer.

This reference is used to refer on the another site.

referrer()

11. Agent string.

This reference is used to containing the full user agent string.

agent_string()

12. Accept lang.

This reference is used to accepts a particular language.

accept_lang([$lang = 'en'])

Example.

View code:-

      
<title></title> 
	

<?php if ($this-?>agent->accept_lang('en'))
{
echo 'You accept English!';
}
?>


Output will be like this:-

13. Accept charset.

This reference is used to accepts a particular character set.

accept_charset([$charset = 'utf-8'])

14. Parse.

This reference is used to parse a custom user agent string.

parse($string)

Models

Connect Database

Helpers

Libraries

Helper Reference

Library Reference

Database Reference