blob: d6641c883a9810701b906862f345502ed3ae55d3 [file] [log] [blame]
Derek Allard2067d1a2008-11-13 22:59:24 +00001<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
3<head>
4
5<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
6<title>User Agent Class : CodeIgniter User Guide</title>
7
8<style type='text/css' media='all'>@import url('../userguide.css');</style>
9<link rel='stylesheet' type='text/css' media='all' href='../userguide.css' />
10
11<script type="text/javascript" src="../nav/nav.js"></script>
12<script type="text/javascript" src="../nav/prototype.lite.js"></script>
13<script type="text/javascript" src="../nav/moo.fx.js"></script>
14<script type="text/javascript" src="../nav/user_guide_menu.js"></script>
15
16<meta http-equiv='expires' content='-1' />
17<meta http-equiv= 'pragma' content='no-cache' />
18<meta name='robots' content='all' />
19<meta name='author' content='ExpressionEngine Dev Team' />
20<meta name='description' content='CodeIgniter User Guide' />
21
22</head>
23<body>
24
25<!-- START NAVIGATION -->
26<div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div>
27<div id="nav2"><a name="top"></a><a href="javascript:void(0);" onclick="myHeight.toggle();"><img src="../images/nav_toggle_darker.jpg" width="154" height="43" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div>
28<div id="masthead">
29<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
30<tr>
Derek Jonesb8c038a2011-08-20 08:57:14 -050031<td><h1>CodeIgniter User Guide Version 2.0.3</h1></td>
Derek Allard2067d1a2008-11-13 22:59:24 +000032<td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
33</tr>
34</table>
35</div>
36<!-- END NAVIGATION -->
37
38
39<!-- START BREADCRUMB -->
40<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
41<tr>
42<td id="breadcrumb">
43<a href="http://codeigniter.com/">CodeIgniter Home</a> &nbsp;&#8250;&nbsp;
44<a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
45User Agent Class
46</td>
47<td id="searchbox"><form method="get" action="http://www.google.com/search"><input type="hidden" name="as_sitesearch" id="as_sitesearch" value="codeigniter.com/user_guide/" />Search User Guide&nbsp; <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" />&nbsp;<input type="submit" class="submit" name="sa" value="Go" /></form></td>
48</tr>
49</table>
50<!-- END BREADCRUMB -->
51
52<br clear="all" />
53
54
55<!-- START CONTENT -->
56<div id="content">
57
58
59<h1>User Agent Class</h1>
60
61<p>The User Agent Class provides functions that help identify information about the browser, mobile device, or robot visiting your site.
62In addition you can get referrer information as well as language and supported character-set information.</p>
63
64<h2>Initializing the Class</h2>
65
66<p>Like most other classes in CodeIgniter, the User Agent class is initialized in your controller using the <dfn>$this->load->library</dfn> function:</p>
67
68<code>$this->load->library('user_agent');</code>
69<p>Once loaded, the object will be available using: <dfn>$this->agent</dfn></p>
70
71<h2>User Agent Definitions</h2>
72
Derek Jones4b9c6292011-07-01 17:40:48 -050073<p>The user agent name definitions are located in a config file located at: <dfn>application/config/user_agents.php</dfn>. You may add items to the
Derek Allard2067d1a2008-11-13 22:59:24 +000074various user agent arrays if needed.</p>
75
76<h2>Example</h2>
77
78<p>When the User Agent class is initialized it will attempt to determine whether the user agent browsing your site is
Derek Jones4b9c6292011-07-01 17:40:48 -050079a web browser, a mobile device, or a robot. It will also gather the platform information if it is available.</p>
Derek Allard2067d1a2008-11-13 22:59:24 +000080
81
82<code>
83$this->load->library('user_agent');<br />
84<br />
85if ($this->agent->is_browser())<br />
86{<br />
Derek Jones4b9c6292011-07-01 17:40:48 -050087&nbsp;&nbsp;&nbsp;&nbsp;$agent = $this->agent->browser().' '.$this->agent->version();<br />
Derek Allard2067d1a2008-11-13 22:59:24 +000088}<br />
89elseif ($this->agent->is_robot())<br />
90{<br />
91&nbsp;&nbsp;&nbsp;&nbsp;$agent = $this->agent->robot();<br />
92}<br />
93elseif ($this->agent->is_mobile())<br />
94{<br />
95&nbsp;&nbsp;&nbsp;&nbsp;$agent = $this->agent->mobile();<br />
96}<br />
97else<br />
98{<br />
99&nbsp;&nbsp;&nbsp;&nbsp;$agent = 'Unidentified User Agent';<br />
100}<br />
101<br />
102echo $agent;<br />
103<br />
104echo $this->agent->platform(); // Platform info (Windows, Linux, Mac, etc.)
105</code>
106
107
108<h1>Function Reference</h1>
109
110
111<h2>$this->agent->is_browser()</h2>
112<p>Returns TRUE/FALSE (boolean) if the user agent is a known web browser.</p>
113
Phil Sturgeondac1b462011-01-06 17:40:10 +0000114<code> if ($this->agent->is_browser('Safari'))<br />
115{<br />
116&nbsp;&nbsp;&nbsp;&nbsp;echo 'You are using Safari.';<br />
117}<br />
118else if ($this->agent->is_browser())<br />
119{<br />
120&nbsp;&nbsp;&nbsp;&nbsp;echo 'You are using a browser.';<br />
121}</code>
122
123<p class="important"><strong>Note:</strong>&nbsp; The string "Safari" in this example is an array key in the list of browser definitions.
124You can find this list in <dfn>application/config/user_agents.php</dfn> if you want to add new browsers or change the stings.</p>
125
Derek Allard2067d1a2008-11-13 22:59:24 +0000126<h2>$this->agent->is_mobile()</h2>
127<p>Returns TRUE/FALSE (boolean) if the user agent is a known mobile device.</p>
128
Phil Sturgeondac1b462011-01-06 17:40:10 +0000129<code> if ($this->agent->is_mobile('iphone'))<br />
130{<br />
131&nbsp;&nbsp;&nbsp;&nbsp;$this->load->view('iphone/home');<br />
132}<br />
133else if ($this->agent->is_mobile())<br />
134{<br />
135&nbsp;&nbsp;&nbsp;&nbsp;$this->load->view('mobile/home');<br />
Bo-Yi Wu61bb5012011-09-11 14:35:52 +0800136}<br />
Phil Sturgeondac1b462011-01-06 17:40:10 +0000137else<br />
138{<br />
139&nbsp;&nbsp;&nbsp;&nbsp;$this->load->view('web/home');<br />
140}</code>
141
Derek Allard2067d1a2008-11-13 22:59:24 +0000142<h2>$this->agent->is_robot()</h2>
143<p>Returns TRUE/FALSE (boolean) if the user agent is a known robot.</p>
144
145<p class="important"><strong>Note:</strong>&nbsp; The user agent library only contains the most common robot
Derek Jones4b9c6292011-07-01 17:40:48 -0500146definitions. It is not a complete list of bots. There are hundreds of them so searching for each one would not be
Derek Allard2067d1a2008-11-13 22:59:24 +0000147very efficient. If you find that some bots that commonly visit your site are missing from the list you can add them to your
148<dfn>application/config/user_agents.php</dfn> file.</p>
149
150<h2>$this->agent->is_referral()</h2>
151<p>Returns TRUE/FALSE (boolean) if the user agent was referred from another site.</p>
152
153
154<h2>$this->agent->browser()</h2>
155<p>Returns a string containing the name of the web browser viewing your site.</p>
156
157<h2>$this->agent->version()</h2>
158<p>Returns a string containing the version number of the web browser viewing your site.</p>
159
160<h2>$this->agent->mobile()</h2>
161<p>Returns a string containing the name of the mobile device viewing your site.</p>
162
163<h2>$this->agent->robot()</h2>
164<p>Returns a string containing the name of the robot viewing your site.</p>
165
166<h2>$this->agent->platform()</h2>
167<p>Returns a string containing the platform viewing your site (Linux, Windows, OS X, etc.).</p>
168
169<h2>$this->agent->referrer()</h2>
170<p>The referrer, if the user agent was referred from another site. Typically you'll test for this as follows:</p>
171
172<code> if ($this->agent->is_referral())<br />
173{<br />
174&nbsp;&nbsp;&nbsp;&nbsp;echo $this->agent->referrer();<br />
175}</code>
176
177
178<h2>$this->agent->agent_string()</h2>
Derek Jones4b9c6292011-07-01 17:40:48 -0500179<p>Returns a string containing the full user agent string. Typically it will be something like this:</p>
Derek Allard2067d1a2008-11-13 22:59:24 +0000180
181<code>Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.0.4) Gecko/20060613 Camino/1.0.2</code>
182
183
184<h2>$this->agent->accept_lang()</h2>
185<p>Lets you determine if the user agent accepts a particular language. Example:</p>
186
187<code>if ($this->agent->accept_lang('en'))<br />
188{<br />
189&nbsp;&nbsp;&nbsp;&nbsp;echo 'You accept English!';<br />
190}</code>
191
192<p class="important"><strong>Note:</strong> This function is not typically very reliable
193since some browsers do not provide language info, and even among those that do, it is not always accurate. </p>
194
195
196
197<h2>$this->agent->accept_charset()</h2>
198<p>Lets you determine if the user agent accepts a particular character set. Example:</p>
199
200<code>if ($this->agent->accept_charset('utf-8'))<br />
201{<br />
202&nbsp;&nbsp;&nbsp;&nbsp;echo 'You browser supports UTF-8!';<br />
203}</code>
204
205<p class="important"><strong>Note:</strong> This function is not typically very reliable
206since some browsers do not provide character-set info, and even among those that do, it is not always accurate. </p>
207
208
209
210</div>
211<!-- END CONTENT -->
212
213
214<div id="footer">
215<p>
216Previous Topic:&nbsp;&nbsp;<a href="uri.html">URI Class</a>
217&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
218<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
219<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
220Next Topic:&nbsp;&nbsp;<a href="xmlrpc.html">XML-RPC Class</a>
221</p>
Derek Jones700205a2011-01-28 07:44:28 -0600222<p><a href="http://codeigniter.com">CodeIgniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2006 - 2011 &nbsp;&middot;&nbsp; <a href="http://ellislab.com/">EllisLab, Inc.</a></p>
Derek Allard2067d1a2008-11-13 22:59:24 +0000223</div>
224
225</body>
Bo-Yi Wu61bb5012011-09-11 14:35:52 +0800226</html>