blob: a1d969abf99a7c84c057e6d9bb9177422314e19c [file] [log] [blame]
Derek Jones8ede1a22011-10-05 13:34:52 -05001################
2User Agent Class
3################
4
5The User Agent Class provides functions that help identify information
6about the browser, mobile device, or robot visiting your site. In
7addition you can get referrer information as well as language and
8supported character-set information.
9
Andrey Andreev20d93e42013-09-23 16:22:23 +030010.. contents::
Andrey Andreevcc042092014-01-03 17:08:27 +020011 :local:
Andrey Andreev20d93e42013-09-23 16:22:23 +030012
13.. raw:: html
14
Andrey Andreevcc042092014-01-03 17:08:27 +020015 <div class="custom-index container"></div>
Andrey Andreev20d93e42013-09-23 16:22:23 +030016
17**************************
18Using the User Agent Class
19**************************
20
Derek Jones8ede1a22011-10-05 13:34:52 -050021Initializing the Class
22======================
23
24Like most other classes in CodeIgniter, the User Agent class is
25initialized in your controller using the $this->load->library function::
26
27 $this->load->library('user_agent');
28
Andrey Andreev20d93e42013-09-23 16:22:23 +030029Once loaded, the object will be available using: ``$this->agent``
Derek Jones8ede1a22011-10-05 13:34:52 -050030
31User Agent Definitions
32======================
33
34The user agent name definitions are located in a config file located at:
35application/config/user_agents.php. You may add items to the various
36user agent arrays if needed.
37
38Example
39=======
40
41When the User Agent class is initialized it will attempt to determine
42whether the user agent browsing your site is a web browser, a mobile
43device, or a robot. It will also gather the platform information if it
44is available.
45
46::
47
Derek Jones06cd1622011-10-05 15:30:47 -050048 $this->load->library('user_agent');
49
50 if ($this->agent->is_browser())
51 {
Andrey Andreev20d93e42013-09-23 16:22:23 +030052 $agent = $this->agent->browser().' '.$this->agent->version();
Derek Jones06cd1622011-10-05 15:30:47 -050053 }
54 elseif ($this->agent->is_robot())
55 {
Andrey Andreev20d93e42013-09-23 16:22:23 +030056 $agent = $this->agent->robot();
Derek Jones06cd1622011-10-05 15:30:47 -050057 }
58 elseif ($this->agent->is_mobile())
59 {
Andrey Andreev20d93e42013-09-23 16:22:23 +030060 $agent = $this->agent->mobile();
Derek Jones06cd1622011-10-05 15:30:47 -050061 }
62 else
63 {
Andrey Andreev20d93e42013-09-23 16:22:23 +030064 $agent = 'Unidentified User Agent';
Derek Jones06cd1622011-10-05 15:30:47 -050065 }
66
67 echo $agent;
68
69 echo $this->agent->platform(); // Platform info (Windows, Linux, Mac, etc.)
Derek Jones8ede1a22011-10-05 13:34:52 -050070
Andrey Andreev20d93e42013-09-23 16:22:23 +030071***************
72Class Reference
73***************
Derek Jones8ede1a22011-10-05 13:34:52 -050074
Andrey Andreevcd3d9db2015-02-02 13:41:01 +020075.. php:class:: CI_User_agent
Derek Jones8ede1a22011-10-05 13:34:52 -050076
Andrey Andreevcd3d9db2015-02-02 13:41:01 +020077 .. php:method:: is_browser([$key = NULL])
Derek Jones8ede1a22011-10-05 13:34:52 -050078
Andrey Andreev28c2c972014-02-08 04:27:48 +020079 :param string $key: Optional browser name
80 :returns: TRUE if the user agent is a (specified) browser, FALSE if not
81 :rtype: bool
Derek Jones8ede1a22011-10-05 13:34:52 -050082
Andrey Andreev20d93e42013-09-23 16:22:23 +030083 Returns TRUE/FALSE (boolean) if the user agent is a known web browser.
84 ::
Derek Jones8ede1a22011-10-05 13:34:52 -050085
Andrey Andreev20d93e42013-09-23 16:22:23 +030086 if ($this->agent->is_browser('Safari'))
87 {
88 echo 'You are using Safari.';
89 }
90 elseif ($this->agent->is_browser())
91 {
92 echo 'You are using a browser.';
93 }
Derek Jones8ede1a22011-10-05 13:34:52 -050094
Andrey Andreev20d93e42013-09-23 16:22:23 +030095 .. note:: The string "Safari" in this example is an array key in the list of browser definitions.
96 You can find this list in **application/config/user_agents.php** if you want to add new
97 browsers or change the stings.
Derek Jones8ede1a22011-10-05 13:34:52 -050098
Andrey Andreevcd3d9db2015-02-02 13:41:01 +020099 .. php:method:: is_mobile([$key = NULL])
Derek Jones8ede1a22011-10-05 13:34:52 -0500100
Andrey Andreev28c2c972014-02-08 04:27:48 +0200101 :param string $key: Optional mobile device name
102 :returns: TRUE if the user agent is a (specified) mobile device, FALSE if not
103 :rtype: bool
Derek Jones8ede1a22011-10-05 13:34:52 -0500104
Andrey Andreev20d93e42013-09-23 16:22:23 +0300105 Returns TRUE/FALSE (boolean) if the user agent is a known mobile device.
106 ::
Derek Jones8ede1a22011-10-05 13:34:52 -0500107
Andrey Andreev20d93e42013-09-23 16:22:23 +0300108 if ($this->agent->is_mobile('iphone'))
109 {
110 $this->load->view('iphone/home');
111 }
112 elseif ($this->agent->is_mobile())
113 {
114 $this->load->view('mobile/home');
115 }
116 else
117 {
118 $this->load->view('web/home');
119 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500120
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200121 .. php:method:: is_robot([$key = NULL])
Derek Jones8ede1a22011-10-05 13:34:52 -0500122
Andrey Andreev28c2c972014-02-08 04:27:48 +0200123 :param string $key: Optional robot name
124 :returns: TRUE if the user agent is a (specified) robot, FALSE if not
125 :rtype: bool
Derek Jones8ede1a22011-10-05 13:34:52 -0500126
Andrey Andreev20d93e42013-09-23 16:22:23 +0300127 Returns TRUE/FALSE (boolean) if the user agent is a known robot.
Derek Jones8ede1a22011-10-05 13:34:52 -0500128
Andrey Andreev20d93e42013-09-23 16:22:23 +0300129 .. note:: The user agent library only contains the most common robot definitions. It is not a complete list of bots.
130 There are hundreds of them so searching for each one would not be very efficient. If you find that some bots
131 that commonly visit your site are missing from the list you can add them to your
132 **application/config/user_agents.php** file.
Derek Jones8ede1a22011-10-05 13:34:52 -0500133
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200134 .. php:method:: is_referral()
Derek Jones8ede1a22011-10-05 13:34:52 -0500135
Andrey Andreev28c2c972014-02-08 04:27:48 +0200136 :returns: TRUE if the user agent is a referral, FALSE if not
137 :rtype: bool
Derek Jones8ede1a22011-10-05 13:34:52 -0500138
Andrey Andreev20d93e42013-09-23 16:22:23 +0300139 Returns TRUE/FALSE (boolean) if the user agent was referred from another site.
Derek Jones8ede1a22011-10-05 13:34:52 -0500140
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200141 .. php:method:: browser()
Derek Jones8ede1a22011-10-05 13:34:52 -0500142
Andrey Andreev28c2c972014-02-08 04:27:48 +0200143 :returns: Detected browser or an empty string
144 :rtype: string
Derek Jones8ede1a22011-10-05 13:34:52 -0500145
Andrey Andreev20d93e42013-09-23 16:22:23 +0300146 Returns a string containing the name of the web browser viewing your site.
Derek Jones8ede1a22011-10-05 13:34:52 -0500147
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200148 .. php:method:: version()
Derek Jones8ede1a22011-10-05 13:34:52 -0500149
Andrey Andreev28c2c972014-02-08 04:27:48 +0200150 :returns: Detected browser version or an empty string
151 :rtype: string
Derek Jones8ede1a22011-10-05 13:34:52 -0500152
Andrey Andreev20d93e42013-09-23 16:22:23 +0300153 Returns a string containing the version number of the web browser viewing your site.
Derek Jones8ede1a22011-10-05 13:34:52 -0500154
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200155 .. php:method:: mobile()
Derek Jones8ede1a22011-10-05 13:34:52 -0500156
Andrey Andreev28c2c972014-02-08 04:27:48 +0200157 :returns: Detected mobile device brand or an empty string
158 :rtype: string
Derek Jones8ede1a22011-10-05 13:34:52 -0500159
Andrey Andreev20d93e42013-09-23 16:22:23 +0300160 Returns a string containing the name of the mobile device viewing your site.
Derek Jones8ede1a22011-10-05 13:34:52 -0500161
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200162 .. php:method:: robot()
Derek Jones8ede1a22011-10-05 13:34:52 -0500163
Andrey Andreev28c2c972014-02-08 04:27:48 +0200164 :returns: Detected robot name or an empty string
165 :rtype: string
Derek Jones8ede1a22011-10-05 13:34:52 -0500166
Andrey Andreev20d93e42013-09-23 16:22:23 +0300167 Returns a string containing the name of the robot viewing your site.
Derek Jones8ede1a22011-10-05 13:34:52 -0500168
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200169 .. php:method:: platform()
Derek Jones8ede1a22011-10-05 13:34:52 -0500170
Andrey Andreev28c2c972014-02-08 04:27:48 +0200171 :returns: Detected operating system or an empty string
172 :rtype: string
Derek Jones8ede1a22011-10-05 13:34:52 -0500173
Andrey Andreev20d93e42013-09-23 16:22:23 +0300174 Returns a string containing the platform viewing your site (Linux, Windows, OS X, etc.).
Derek Jones8ede1a22011-10-05 13:34:52 -0500175
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200176 .. php:method:: referrer()
Derek Jones8ede1a22011-10-05 13:34:52 -0500177
Andrey Andreev28c2c972014-02-08 04:27:48 +0200178 :returns: Detected referrer or an empty string
179 :rtype: string
Derek Jones8ede1a22011-10-05 13:34:52 -0500180
Andrey Andreev20d93e42013-09-23 16:22:23 +0300181 The referrer, if the user agent was referred from another site. Typically you'll test for this as follows::
Derek Jones8ede1a22011-10-05 13:34:52 -0500182
Andrey Andreev20d93e42013-09-23 16:22:23 +0300183 if ($this->agent->is_referral())
184 {
185 echo $this->agent->referrer();
186 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500187
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200188 .. php:method:: agent_string()
Derek Jones8ede1a22011-10-05 13:34:52 -0500189
Andrey Andreev28c2c972014-02-08 04:27:48 +0200190 :returns: Full user agent string or an empty string
191 :rtype: string
Andrey Andreev20d93e42013-09-23 16:22:23 +0300192
193 Returns a string containing the full user agent string. Typically it will be something like this::
194
195 Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.0.4) Gecko/20060613 Camino/1.0.2
196
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200197 .. php:method:: accept_lang([$lang = 'en'])
Andrey Andreev20d93e42013-09-23 16:22:23 +0300198
Andrey Andreev28c2c972014-02-08 04:27:48 +0200199 :param string $lang: Language key
200 :returns: TRUE if provided language is accepted, FALSE if not
201 :rtype: bool
Andrey Andreev20d93e42013-09-23 16:22:23 +0300202
203 Lets you determine if the user agent accepts a particular language. Example::
204
205 if ($this->agent->accept_lang('en'))
206 {
207 echo 'You accept English!';
208 }
209
210 .. note:: This method is not typically very reliable since some browsers do not provide language info,
211 and even among those that do, it is not always accurate.
212
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200213 .. php:method:: languages()
Andrey Andreev20d93e42013-09-23 16:22:23 +0300214
Andrey Andreev28c2c972014-02-08 04:27:48 +0200215 :returns: An array list of accepted languages
216 :rtype: array
Andrey Andreev20d93e42013-09-23 16:22:23 +0300217
218 Returns an array of languages supported by the user agent.
219
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200220 .. php:method:: accept_charset([$charset = 'utf-8'])
Andrey Andreev20d93e42013-09-23 16:22:23 +0300221
Andrey Andreev28c2c972014-02-08 04:27:48 +0200222 :param string $charset: Character set
223 :returns: TRUE if the character set is accepted, FALSE if not
224 :rtype: bool
Andrey Andreev20d93e42013-09-23 16:22:23 +0300225
226 Lets you determine if the user agent accepts a particular character set. Example::
227
228 if ($this->agent->accept_charset('utf-8'))
229 {
230 echo 'You browser supports UTF-8!';
231 }
232
233 .. note:: This method is not typically very reliable since some browsers do not provide character-set info,
234 and even among those that do, it is not always accurate.
235
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200236 .. php:method:: charsets()
Andrey Andreev20d93e42013-09-23 16:22:23 +0300237
Andrey Andreev28c2c972014-02-08 04:27:48 +0200238 :returns: An array list of accepted character sets
239 :rtype: array
Andrey Andreev20d93e42013-09-23 16:22:23 +0300240
Andrey Andreev1f811c32014-01-09 01:02:38 +0200241 Returns an array of character sets accepted by the user agent.
242
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200243 .. php:method:: parse($string)
Andrey Andreev1f811c32014-01-09 01:02:38 +0200244
Andrey Andreev28c2c972014-02-08 04:27:48 +0200245 :param string $string: A custom user-agent string
246 :rtype: void
Andrey Andreev1f811c32014-01-09 01:02:38 +0200247
248 Parses a custom user-agent string, different from the one reported by the current visitor.