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])
  • Parameters :
  • $key (string) : Optional browser name.
  • Returns : TRUE if the user agent is a (specified) browser, FALSE if not.
  • Returns type : Bool

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:-
<html>
<head>      
<title></title> 
</head>	
<body>
<?php
if ($this->agent->is_browser())
{
$agent = $this->agent->browser().' '.$this->agent->version();
}
echo $agent;
echo $this->agent->platform();
?>
</body>
</html>

Output will be like this:-

Codeigniter User Agent Class Library

2. is mobile.

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

is_mobile([$key = NULL])
  • Parameters :
  • $key (string) : Optional mobile device name.
  • Returns : TRUE if the user agent is a (specified) mobile device, FALSE if not.
  • Returns type : Bool

Example.

View code:-
<html>
<head>      
<title></title> 
</head>	
<body>
<?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";
}
?>
</body>
</html>

Output will be like this:-

Codeigniter User Agent Class Library

3. is robot.

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

is_robot([$key = NULL])
  • Parameters :
  • $key (string) : Optional robot name.
  • Returns : TRUE if the user agent is a (specified) robot, FALSE if not.
  • Returns type : Bool

Example.

View code:-
<html>
<head>      
<title></title> 
</head>	
<body>
<?php
if ($this->agent->is_robot())
{
$roboot = $this->agent->robot();
}
echo $roboot;
?>
</body>
</html>

Output will be like this:-

Codeigniter User Agent Class Library

4. is referral.

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

is_referral()
  • Parameters :
  • Returns : TRUE if the user agent is a referral, FALSE if not.
  • Returns type : Bool

5. browser.

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

browser()
  • Parameters :
  • Returns : Detected browser or an empty string.
  • Returns type : String

6. Version.

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

version()
  • Parameters :
  • Returns : Detected browser version or an empty string.
  • Returns type : String

7. Mobile.

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

mobile()
  • Parameters :
  • Returns : Detected mobile device brand or an empty string.
  • Returns type : String

8. Robot.

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

robot()
  • Parameters :
  • Returns : Detected robot name or an empty string.
  • Returns type : String

9. Platform.

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

platform()
  • Parameters :
  • Returns : Detected operating system or an empty string.
  • Returns type : String

10. Referrer.

This reference is used to refer on the another site.

referrer()
  • Parameters :
  • Returns : Detected referrer or an empty string.
  • Returns type : String

11. Agent string.

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

agent_string()
  • Parameters :
  • Returns : Full user agent string or an empty string.
  • Returns type : String

12. Accept lang.

This reference is used to accepts a particular language.

accept_lang([$lang = 'en'])
  • Parameters :
  • $lang (string) : Language key
  • Returns : TRUE if provided language is accepted, FALSE if not.
  • Returns type : String

Example.

View code:-
<html>
<head>      
<title></title> 
</head>	
<body>
<?php
if ($this->agent->accept_lang('en'))
{
echo 'You accept English!';
}
?>
</body>
</html>

Output will be like this:-

Codeigniter User Agent Class Library

13. Accept charset.

This reference is used to accepts a particular character set.

accept_charset([$charset = 'utf-8'])
  • Parameters :
  • $charset (string) : Character set
  • Returns : TRUE if the character set is accepted, FALSE if not.
  • Returns type : Bool

14. Parse.

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

parse($string)
  • Parameters :
  • $string (string) : A custom user-agent string
  • Returns type : void

Advertisements

Add Comment

📖 Read More