blob: 97abd2244905bd7594e86434c4e8955abafe6ac9 [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
10Initializing the Class
11======================
12
13Like most other classes in CodeIgniter, the User Agent class is
14initialized in your controller using the $this->load->library function::
15
16 $this->load->library('user_agent');
17
18Once loaded, the object will be available using: $this->agent
19
20User Agent Definitions
21======================
22
23The user agent name definitions are located in a config file located at:
24application/config/user_agents.php. You may add items to the various
25user agent arrays if needed.
26
27Example
28=======
29
30When the User Agent class is initialized it will attempt to determine
31whether the user agent browsing your site is a web browser, a mobile
32device, or a robot. It will also gather the platform information if it
33is available.
34
35::
36
Derek Jones06cd1622011-10-05 15:30:47 -050037 $this->load->library('user_agent');
38
39 if ($this->agent->is_browser())
40 {
41 $agent = $this->agent->browser().' '.$this->agent->version();
42 }
43 elseif ($this->agent->is_robot())
44 {
45 $agent = $this->agent->robot();
46 }
47 elseif ($this->agent->is_mobile())
48 {
49 $agent = $this->agent->mobile();
50 }
51 else
52 {
53 $agent = 'Unidentified User Agent';
54 }
55
56 echo $agent;
57
58 echo $this->agent->platform(); // Platform info (Windows, Linux, Mac, etc.)
Derek Jones8ede1a22011-10-05 13:34:52 -050059
60******************
61Function Reference
62******************
63
64$this->agent->is_browser()
65===========================
66
67Returns TRUE/FALSE (boolean) if the user agent is a known web browser.
68
69::
70
Derek Jones06cd1622011-10-05 15:30:47 -050071 if ($this->agent->is_browser('Safari'))
72 {
73 echo 'You are using Safari.';
74 }
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030075 elseif ($this->agent->is_browser())
Derek Jones06cd1622011-10-05 15:30:47 -050076 {
77 echo 'You are using a browser.';
78 }
79
Derek Jones8ede1a22011-10-05 13:34:52 -050080
81.. note:: The string "Safari" in this example is an array key in the
82 list of browser definitions. You can find this list in
83 application/config/user_agents.php if you want to add new browsers or
84 change the stings.
85
86$this->agent->is_mobile()
87==========================
88
89Returns TRUE/FALSE (boolean) if the user agent is a known mobile device.
90
91::
92
Derek Jones06cd1622011-10-05 15:30:47 -050093 if ($this->agent->is_mobile('iphone'))
94 {
95 $this->load->view('iphone/home');
96 }
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030097 elseif ($this->agent->is_mobile())
Derek Jones06cd1622011-10-05 15:30:47 -050098 {
99 $this->load->view('mobile/home');
100 }
101 else
102 {
103 $this->load->view('web/home');
104 }
105
Derek Jones8ede1a22011-10-05 13:34:52 -0500106
107$this->agent->is_robot()
108=========================
109
110Returns TRUE/FALSE (boolean) if the user agent is a known robot.
111
112.. note:: The user agent library only contains the most common robot
113 definitions. It is not a complete list of bots. There are hundreds of
114 them so searching for each one would not be very efficient. If you find
115 that some bots that commonly visit your site are missing from the list
116 you can add them to your application/config/user_agents.php file.
117
118$this->agent->is_referral()
119============================
120
121Returns TRUE/FALSE (boolean) if the user agent was referred from another
122site.
123
124$this->agent->browser()
125=======================
126
127Returns a string containing the name of the web browser viewing your
128site.
129
130$this->agent->version()
131=======================
132
133Returns a string containing the version number of the web browser
134viewing your site.
135
136$this->agent->mobile()
137======================
138
139Returns a string containing the name of the mobile device viewing your
140site.
141
142$this->agent->robot()
143=====================
144
145Returns a string containing the name of the robot viewing your site.
146
147$this->agent->platform()
148========================
149
150Returns a string containing the platform viewing your site (Linux,
151Windows, OS X, etc.).
152
153$this->agent->referrer()
154========================
155
156The referrer, if the user agent was referred from another site.
157Typically you'll test for this as follows::
158
Derek Jones06cd1622011-10-05 15:30:47 -0500159 if ($this->agent->is_referral())
160 {
161 echo $this->agent->referrer();
162 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500163
164$this->agent->agent_string()
165=============================
166
167Returns a string containing the full user agent string. Typically it
168will be something like this::
169
170 Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.0.4) Gecko/20060613 Camino/1.0.2
171
172$this->agent->accept_lang()
173============================
174
175Lets you determine if the user agent accepts a particular language.
176Example::
177
Derek Jones06cd1622011-10-05 15:30:47 -0500178 if ($this->agent->accept_lang('en'))
179 {
180 echo 'You accept English!';
181 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500182
183.. note:: This function is not typically very reliable since some
184 browsers do not provide language info, and even among those that do, it
185 is not always accurate.
186
187$this->agent->accept_charset()
188===============================
189
190Lets you determine if the user agent accepts a particular character set.
191Example::
192
Derek Jones06cd1622011-10-05 15:30:47 -0500193 if ($this->agent->accept_charset('utf-8'))
194 {
195 echo 'You browser supports UTF-8!';
196 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500197
198.. note:: This function is not typically very reliable since some
199 browsers do not provide character-set info, and even among those that
200 do, it is not always accurate.