blob: af76cfa6e3a8bdd35690c4e70827842f8a996e59 [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 Andreev20d93e42013-09-23 16:22:23 +030075.. class: CI_User_agent
Derek Jones8ede1a22011-10-05 13:34:52 -050076
Andrey Andreev20d93e42013-09-23 16:22:23 +030077 .. method:: is_browser([$key = NULL])
Derek Jones8ede1a22011-10-05 13:34:52 -050078
Andrey Andreev20d93e42013-09-23 16:22:23 +030079 :param string $key: optional browser name
80 :returns: bool
Derek Jones8ede1a22011-10-05 13:34:52 -050081
Andrey Andreev20d93e42013-09-23 16:22:23 +030082 Returns TRUE/FALSE (boolean) if the user agent is a known web browser.
83 ::
Derek Jones8ede1a22011-10-05 13:34:52 -050084
Andrey Andreev20d93e42013-09-23 16:22:23 +030085 if ($this->agent->is_browser('Safari'))
86 {
87 echo 'You are using Safari.';
88 }
89 elseif ($this->agent->is_browser())
90 {
91 echo 'You are using a browser.';
92 }
Derek Jones8ede1a22011-10-05 13:34:52 -050093
Andrey Andreev20d93e42013-09-23 16:22:23 +030094 .. note:: The string "Safari" in this example is an array key in the list of browser definitions.
95 You can find this list in **application/config/user_agents.php** if you want to add new
96 browsers or change the stings.
Derek Jones8ede1a22011-10-05 13:34:52 -050097
Andrey Andreev20d93e42013-09-23 16:22:23 +030098 .. method:: is_mobile([$key = NULL])
Derek Jones8ede1a22011-10-05 13:34:52 -050099
Andrey Andreev20d93e42013-09-23 16:22:23 +0300100 :param string $key: optional mobile device name
101 :returns: bool
Derek Jones8ede1a22011-10-05 13:34:52 -0500102
Andrey Andreev20d93e42013-09-23 16:22:23 +0300103 Returns TRUE/FALSE (boolean) if the user agent is a known mobile device.
104 ::
Derek Jones8ede1a22011-10-05 13:34:52 -0500105
Andrey Andreev20d93e42013-09-23 16:22:23 +0300106 if ($this->agent->is_mobile('iphone'))
107 {
108 $this->load->view('iphone/home');
109 }
110 elseif ($this->agent->is_mobile())
111 {
112 $this->load->view('mobile/home');
113 }
114 else
115 {
116 $this->load->view('web/home');
117 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500118
Andrey Andreev20d93e42013-09-23 16:22:23 +0300119 .. method:: is_robot([$key = NULL])
Derek Jones8ede1a22011-10-05 13:34:52 -0500120
Andrey Andreev20d93e42013-09-23 16:22:23 +0300121 :param string $key: optional robot name
122 :returns: bool
Derek Jones8ede1a22011-10-05 13:34:52 -0500123
Andrey Andreev20d93e42013-09-23 16:22:23 +0300124 Returns TRUE/FALSE (boolean) if the user agent is a known robot.
Derek Jones8ede1a22011-10-05 13:34:52 -0500125
Andrey Andreev20d93e42013-09-23 16:22:23 +0300126 .. note:: The user agent library only contains the most common robot definitions. It is not a complete list of bots.
127 There are hundreds of them so searching for each one would not be very efficient. If you find that some bots
128 that commonly visit your site are missing from the list you can add them to your
129 **application/config/user_agents.php** file.
Derek Jones8ede1a22011-10-05 13:34:52 -0500130
Andrey Andreev20d93e42013-09-23 16:22:23 +0300131 .. method:: is_referral()
Derek Jones8ede1a22011-10-05 13:34:52 -0500132
Andrey Andreev20d93e42013-09-23 16:22:23 +0300133 :returns: bool
Derek Jones8ede1a22011-10-05 13:34:52 -0500134
Andrey Andreev20d93e42013-09-23 16:22:23 +0300135 Returns TRUE/FALSE (boolean) if the user agent was referred from another site.
Derek Jones8ede1a22011-10-05 13:34:52 -0500136
Andrey Andreev20d93e42013-09-23 16:22:23 +0300137 .. method:: browser()
Derek Jones8ede1a22011-10-05 13:34:52 -0500138
Andrey Andreev20d93e42013-09-23 16:22:23 +0300139 :returns: string
Derek Jones8ede1a22011-10-05 13:34:52 -0500140
Andrey Andreev20d93e42013-09-23 16:22:23 +0300141 Returns a string containing the name of the web browser viewing your site.
Derek Jones8ede1a22011-10-05 13:34:52 -0500142
Andrey Andreev20d93e42013-09-23 16:22:23 +0300143 .. method:: version()
Derek Jones8ede1a22011-10-05 13:34:52 -0500144
Andrey Andreev20d93e42013-09-23 16:22:23 +0300145 :returns: string
Derek Jones8ede1a22011-10-05 13:34:52 -0500146
Andrey Andreev20d93e42013-09-23 16:22:23 +0300147 Returns a string containing the version number of the web browser viewing your site.
Derek Jones8ede1a22011-10-05 13:34:52 -0500148
Andrey Andreev20d93e42013-09-23 16:22:23 +0300149 .. method:: mobile()
Derek Jones8ede1a22011-10-05 13:34:52 -0500150
Andrey Andreev20d93e42013-09-23 16:22:23 +0300151 :returns: string
Derek Jones8ede1a22011-10-05 13:34:52 -0500152
Andrey Andreev20d93e42013-09-23 16:22:23 +0300153 Returns a string containing the name of the mobile device viewing your site.
Derek Jones8ede1a22011-10-05 13:34:52 -0500154
Andrey Andreev20d93e42013-09-23 16:22:23 +0300155 .. method:: robot()
Derek Jones8ede1a22011-10-05 13:34:52 -0500156
Andrey Andreev20d93e42013-09-23 16:22:23 +0300157 :returns: string
Derek Jones8ede1a22011-10-05 13:34:52 -0500158
Andrey Andreev20d93e42013-09-23 16:22:23 +0300159 Returns a string containing the name of the robot viewing your site.
Derek Jones8ede1a22011-10-05 13:34:52 -0500160
Andrey Andreev20d93e42013-09-23 16:22:23 +0300161 .. method:: platform()
Derek Jones8ede1a22011-10-05 13:34:52 -0500162
Andrey Andreev20d93e42013-09-23 16:22:23 +0300163 :returns: string
Derek Jones8ede1a22011-10-05 13:34:52 -0500164
Andrey Andreev20d93e42013-09-23 16:22:23 +0300165 Returns a string containing the platform viewing your site (Linux, Windows, OS X, etc.).
Derek Jones8ede1a22011-10-05 13:34:52 -0500166
Andrey Andreev20d93e42013-09-23 16:22:23 +0300167 .. method:: referrer()
Derek Jones8ede1a22011-10-05 13:34:52 -0500168
Andrey Andreev20d93e42013-09-23 16:22:23 +0300169 :returns: string
Derek Jones8ede1a22011-10-05 13:34:52 -0500170
Andrey Andreev20d93e42013-09-23 16:22:23 +0300171 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 -0500172
Andrey Andreev20d93e42013-09-23 16:22:23 +0300173 if ($this->agent->is_referral())
174 {
175 echo $this->agent->referrer();
176 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500177
Andrey Andreev20d93e42013-09-23 16:22:23 +0300178 .. method:: agent_string()
Derek Jones8ede1a22011-10-05 13:34:52 -0500179
Andrey Andreev20d93e42013-09-23 16:22:23 +0300180 :returns: string
181
182 Returns a string containing the full user agent string. Typically it will be something like this::
183
184 Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.0.4) Gecko/20060613 Camino/1.0.2
185
186 .. method:: accept_lang([$lang = 'en'])
187
188 :param string $lang: language key
189 :returns: bool
190
191 Lets you determine if the user agent accepts a particular language. Example::
192
193 if ($this->agent->accept_lang('en'))
194 {
195 echo 'You accept English!';
196 }
197
198 .. note:: This method is not typically very reliable since some browsers do not provide language info,
199 and even among those that do, it is not always accurate.
200
201 .. method:: languages()
202
203 :returns: array
204
205 Returns an array of languages supported by the user agent.
206
207 .. method:: accept_charset([$charset = 'utf-8'])
208
209 :param string $charset: character set
210 :returns: bool
211
212 Lets you determine if the user agent accepts a particular character set. Example::
213
214 if ($this->agent->accept_charset('utf-8'))
215 {
216 echo 'You browser supports UTF-8!';
217 }
218
219 .. note:: This method is not typically very reliable since some browsers do not provide character-set info,
220 and even among those that do, it is not always accurate.
221
222 .. method:: charsets()
223
224 :returns: array
225
Andrey Andreev1f811c32014-01-09 01:02:38 +0200226 Returns an array of character sets accepted by the user agent.
227
228 .. method:: parse($string)
229
230 :param string $string: A custom user-agent string
231 :returns: void
232
233 Parses a custom user-agent string, different from the one reported by the current visitor.