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

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

            $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

$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

$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

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

Syntax

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

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

Syntax

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

Syntax

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

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

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

Syntax

Example:

User_controller.php

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

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

Example:   

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

Syntax

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

Syntax

$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

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

$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

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

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

Pin It on Pinterest

Share This