×

CodeIgniter User Agent Class

User Agent Class

The CodeIgniter provides a user agent class that helps to identify and collect information about the browser, mobile devices, or robots visiting your site. Besides this, it is also used to obtain referrer information, language support, and character sets from another web site that are linking to your web site.

Load the user agent class

Before using the agent library in the CodeIgniter application, you must load this library in the controller file, as shown below.

Syntax

$this->load->library( ‘user_agent’);

When the library is loaded, you can simply use ‘$this->agent’ as an object to execute the user agent function.

Class Reference

The following class references are available in the user agent class:

  1. is_browser(): The is_browser() function is used to determine whether you are using a browser or not. And, If the user agent is a specified browser, it returns TRUE; otherwise, it shows FALSE.

Syntax

is_browser( [$key = NULL] )

            $key: It defines an optional browser name, such as Google, Firefox, etc.

2. is_mobile(): The is_mobile() function is also used to determine whether the user agent is a specified mobile device or not. And if the user agent is a known device, it returns TRUE; otherwise, FALSE.

Syntax

is_mobile( [$key = NULL] ) 

$key: It defines an optional mobile device name.

3. is_robot(): The is_robot() function is used to determine whether the user agent is a specified robot or not, and if it user agent is a specified robot, it returns TRUE; otherwise, it shows FALSE.

Syntax

is_robot( [$key = NULL] )

$key: It defines an optional robot name.

4. is_referral(): The is_referral() function is used to determine whether the user agent is referred from another site. And, if the user agent is a referral, it returns TRUE; otherwise, it shows FALSE.

Syntax

is_referral()

5. browser(): A browser() function is used to return the name of a web browser through which your site is viewing.

Syntax

$this->agent->browser();

6. version(): As the name suggests, a version() function is used to detect the version number of the web browser through which your site is viewing.

Syntax

$this->agent->version();

7. mobile(): A mobile() function is used to detect and return the name of the mobile device through which your site is viewing.

Syntax

$this->agent->mobile();

8. robot(): A robot() function is used to return the detected robot name that is viewing your site.

Syntax

$this->agent->robot();

9. platform(): A platform() function is used to return the detected operating system through which your site is viewing. For example, Linux, Macintosh, Windows, etc.

Syntax

$this->agent->platform();

10. referrer(): A referrer() function is used to return the name of detected referrer string from another site that’s linking to your site.

Syntax

$this->agent->referrer();

11. agent_string(): An agent_string() function is used to return the full user agent string of your site.

Syntax

$this->agent->agent_string();

Example:

User_controller.php

<?php
 defined( 'BASEPATH') OR exit( 'No direct script access allowed');
 class User_controller extends CI_Controller 
 {
 public function temp()
     {  
         echo "<title> Tutorial and Example </title>";
         $this->load->library('user_agent'); 
         echo $this->agent->agent_string();
     }
 }
 ?> 

Execute the above program in localhost by invoking the URL localhost/CodeIgniter-3.1.11/index.php/User_controller/temp, it shows the output, as shown below.

User Agent Class

12. accept_lang(): As the name suggests, an accept_lang() function is used to accept only a defined language in your site.

Syntax

accept_lang( [$lang = ‘en’] )

$lang: You can define a language key that is accepted by the user agent.

Example:   

If ( $this->agent ->accept_lang(‘en’))
             {
                       echo ‘You have provided the English Language’;
                 } 

13. languages(): The languages() function is used to return all the languages that are supported by the user agent.

Syntax

$this->agent->languages();  // it returns an array of accepted languages

14. accept_charset(): An accept_charset() function is used to accept only the defined charset in your site.

Syntax

accept_charset( [$charset = ‘utf-8’] )

$charset: You can define a character set that is accepted by the user agent.

15. charsets(): The charset() function is used to return the character set that is supported by the user agent class.  

Syntax

$this->agent->charsets(); // It returns an array of character sets

16. parse(): A parse() function is used to parses a custom user agent that is different from the one generated by the current visitor.

Syntax

parse ($string)

$string: It defines a custom user-agent string.

Example of User-Agent Class:

Create a User_controller.php controller file and save it to the following path application/controller/User_controller.php folder. After that, write the following program in the controller file.

User_controller.php

<?php
 defined( 'BASEPATH') OR exit( 'No direct script access allowed');
 class User_controller extends CI_Controller 
 {
     public function show_browser()
     { 
   $this->load->library('user_agent'); 
         echo "<title> Tutorial and Example </title>";
         if ($this->agent->is_browser())
         {
                echo "Browser name:  " $this->agent->browser(). "<br/> Browser version is  ". $this->agent->version(); 
                 $info = $this->agent->platform();
                 echo "<br/>". "Operating System  ". $info;
         }
     }
 }
 ?> 

To run the program in the localhost by invoking the URL localhost/CodeIgniter-3.1.11/index.php/User_controller/show_browser, it shows the output, as shown below.

User Agent Class

Example 2.   User_controller.php

<?php
 defined( 'BASEPATH') OR exit( 'No direct script access allowed');
 class User_controller extends CI_Controller 
 {
  public function detect_browser()
            {   
                echo "<title> Tutorial and Example </title>";    
                $this->load->library('user_agent');
         if ($this->agent->is_browser()) 
         {
         $user = $this->agent->browser();
         }
         elseif ($this->agent->is_mobile())
         {
             $user = $this->agent->mobile(); 
         }
         elseif ($this->agent->is_robot())
         {
             $user = $this->agent->robot(); 
         }
         echo "You are using ". $user. " browser";
     }
 }
 ?> 

To run the program in the localhost by invoking the URL localhost/CodeIgniter-3.1.11/index.php/User_controller/detect_browser, it shows the output, as shown below.

User Agent Class

Related Topics

CodeIgniter Date helper

A date helper file contains some predefined functions that work with date functions. Before using the date function in CodeIgniter, you have to load the date helper in the controller. For example: $this->load->helper(‘date’); Functions of...

12 minutes read.

Versions of CodeIgniter

CodeIgniter v3.1.11 (This is the current version which we will used in this tutorial)CodeIgniter v3.1.10CodeIgniter v3.1.9CodeIgniter v3.1.8CodeIgniter v3.1.7CodeIgniter v3.1.6CodeIgniter v3.1.5CodeIgniter v3.1.4CodeIgniter v3.1.3CodeIgniter v3.1.2CodeIgniter v3.1.1CodeIgniter v3.1.0CodeIgniter v3.0.6CodeIgniter...

1 minute read.

How to remove an index.php file from the Codeigniter URLs?

As we know, a URL in CodeIgniter is designed in such a way that it would be search engine and human friendly, too, rather than using a standard approach to create a...

2 minutes read.

CodeIgniter URL Helper

The helper file contains some predefined functions that are used to work with URL, such as generate the current file path, transfer control, redirect, create a link, etc. Load the URL Helper Before using...

7 minutes read.

Database Configuration CodeIgniter

In every application, there is an important part of a database through which developers can connect an application to store users’ data and perform various functions in the database application such as inserting,...

10 minutes read.

HTML Table Class CodeIgniter

HTML Table Class The CodeIgniter framework provides an HTML Table Class that is used to develop an auto-generated table using a defined array or result sets of the database table. Loading an HTML Table Class...

8 minutes read.

CodeIgniter Language Class

Language Class The CodeIgniter’s language class contains the various functionality that is used to access the language-specific files and text line for the purposes of internationalization. This class provides you suitability to add or...

4 minutes read.

CodeIgniter Text Helper

The text helper contains the various function that is used to operate with text such as define a word limit and characters limit, conversion of word from ASCII to entities, etc. Loading the text...

4 minutes read.

CodeIgniter Output class

Output class The CodeIgniter framework contains a core class that is an Output class. It  includes the various functions that are used to send the final web page to the requesting browser. In...

8 minutes read.

CodeIgniter String Helper

The string helper file contains functions that allows separate operations with strings in CodeIgniter. Loading the String Helper Before using the string helper in the CodeIgniter application, you must load it in the controller...

7 minutes read.

CodeIgniter Cookie Helper

A cookie is a small set of files sent from the web server to the end-user system. In Helpers, the cookie helper file has some predefined functions that are used to the...

4 minutes read.

CodeIgniter Number helper

The number helper file contains some predefined function that deals with numeric data to display numbers in bytes format. Load a Number Helper You must load a number helper file in the controller to perform...

3 minutes read.

CodeIgniter ZIP Encoding Class

ZIP Encoding Class The CodeIgniter provides a Zip encoding class that are used to compress a large file into a Zip archives. And this archives files can be downloaded either to your desktop or...

5 minutes read.

CodeIgniter URI Class

URI Class The CodeIgniter contains a URI class that is used to retrieve information from URI strings. It also provides suitability to get information from the re-routed segments. The system automatically initializes this...

6 minutes read.

CodeIgniter Helpers

The Helpers (functions) are stored in the Helper file. It helps CodeIgniter to perform different tasks. The helper file is the collection of many functions that is used to perform specific task. It...

3 minutes read.

CodeIgniter Models

Models are the classes that deals with backend operations. It is used to fetch the data from the database and send it to the Controller. All the database related information provided inside...

5 minutes read.

Installation of CodeIgniter

Before installing CodeIgniter, you need to make sure that the wamp or xampp server in the system is preinstalled. The installation procedure for CodeIgniter is very easy. You can download CodeIgniter by following these...

2 minutes read.

CodeIgniter Form Helper

A form helper file contains different functions that works with forms. It is used to create various functionality in a form such as dropdown, radio button, password, upload, hidden data, etc. These functionalities...

12 minutes read.

Directory Helper Codeigniter

Directory Helper CodeIgniter: provides a directory helper function that works with the directory. It shows the structure of the file, hidden file in the folder, and the folder in...

3 minutes read.

CodeIgniter User Agent Class

User Agent Class The CodeIgniter provides a user agent class that helps to identify and collect information about the browser, mobile devices, or robots visiting your site. Besides this, it is also used...

4 minutes read.