×

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 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 Security Helper

A Security helper file contains some predefined functions that are used to protect application from unauthorized access. Loading the Helper The following syntax is used to load the security helper in the CodeIgniter application. Syntax $this-> load->...

4 minutes read.

CodeIgniter Pagination Class

Pagination Class The CodeIgniter framework provides a pagination class that is used in the data list to create pagination links in a web application. Generally, it is used to retrieve data records from...

10 minutes read.

CodeIgniter Email Class

CodeIgniter provides an e-mail library that helps to send a message from one application to another. You can create and send text messages easily in CodeIgniter application even you can set email...

9 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.

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.

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.

Download Helper Codeigniter

Download Helper Codeigniter: The download helper function is used to download data from the server to your computer. The data can be any format like text, jpg, mp3, mp4, etc. Loading...

2 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 Shopping Cart Class

It contains the various function that enables the user to add or update, display, and delete data item from the shopping carts while browsing the site. The items added in the cart...

10 minutes read.

Update a Record CodeIgniter

Update a record In this topic, we will learn how we can update existing records in database tables. To update a record, CodeIgniter provides an update() function used with the set()...

10 minutes read.

CodeIgniter Array Helper

The Array Helper contains some predefined functions that are used to perform the various operation with array. Load Array helper It is used to load the helper class. We can pass the helper function in the...

6 minutes read.

CodeIgniter File Helper

The file helper function is used to perform various tasks with files such as accessing files, writing data to files, deleting files, and more. Loading the File Helper Before using the file helper, you must...

7 minutes read.

Calendaring Class CodeIgniter

Calendaring Class The calendar library contains various functions that are used to create a dynamic calendar. It also enables you to pass the data to calendar cells and 100% control over the calendar...

6 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.

Insert data to the database CodeIgniter

After successfully creating a database connection with the CodeIgniter application, we will now understand how we can insert records into the database. To insert a record into a database table, Codeigniter provides...

8 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 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.

Retrieve data from Database

Retrieve data from the Database Now we will discuss how we can retrieve data from the database that we have inserted to the employees table of the database in our previous topic “inserted...

2 minutes read.

Codeigniter Session Library

Session Library The session is an essential part of any application. It contains various functions to manage the users' status and track their activity while browsing on the site. For example,...

13 minutes read.