blob: 5bb87a76b413e8bea797a854eeb58ec90b9f970d [file] [log] [blame]
adminada5fa32006-10-09 21:40:36 +00001<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2<html>
3<head>
4
Derek Allardd2df9bc2007-04-15 17:41:17 +00005<title>CodeIgniter User Guide</title>
adminada5fa32006-10-09 21:40:36 +00006
7<style type='text/css' media='all'>@import url('../userguide.css');</style>
8<link rel='stylesheet' type='text/css' media='all' href='../userguide.css' />
9
10<script type="text/javascript" src="../nav/nav.js"></script>
11<script type="text/javascript" src="../nav/prototype.lite.js"></script>
12<script type="text/javascript" src="../nav/moo.fx.js"></script>
13<script type="text/javascript">
14window.onload = function() {
admine334c472006-10-21 19:44:22 +000015 myHeight = new fx.Height('nav', {duration: 400});
adminada5fa32006-10-09 21:40:36 +000016 myHeight.hide();
17}
18</script>
19
20<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
21<meta http-equiv='expires' content='-1' />
22<meta http-equiv= 'pragma' content='no-cache' />
23<meta name='robots' content='all' />
24<meta name='author' content='Rick Ellis' />
Derek Allardd2df9bc2007-04-15 17:41:17 +000025<meta name='description' content='CodeIgniter User Guide' />
adminada5fa32006-10-09 21:40:36 +000026
27</head>
28<body>
29
30<!-- START NAVIGATION -->
31<div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div>
32<div id="nav2"><a name="top"></a><a href="javascript:void(0);" onclick="myHeight.toggle();"><img src="../images/nav_toggle.jpg" width="153" height="44" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div>
33<div id="masthead">
34<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
35<tr>
Derek Allardd2df9bc2007-04-15 17:41:17 +000036<td><h1>CodeIgniter User Guide Version 1.5.3</h1></td>
adminc0d5d522006-10-30 19:40:35 +000037<td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
adminada5fa32006-10-09 21:40:36 +000038</tr>
39</table>
40</div>
41<!-- END NAVIGATION -->
42
43
44<!-- START BREADCRUMB -->
45<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
46<tr>
47<td id="breadcrumb">
Derek Allardd2df9bc2007-04-15 17:41:17 +000048<a href="http://www.codeigniter.com/">CodeIgniter Home</a> &nbsp;&#8250;&nbsp;
adminada5fa32006-10-09 21:40:36 +000049<a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
50User Agent Class
51</td>
Derek Allardbc030912007-06-24 18:25:29 +000052<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>
adminada5fa32006-10-09 21:40:36 +000053</tr>
54</table>
55<!-- END BREADCRUMB -->
56
57<br clear="all" />
58
59
60<!-- START CONTENT -->
61<div id="content">
62
63
64<h1>User Agent Class</h1>
65
66<p>The User Agent Class provides functions that help identify information about the browser, mobile device, or robot visiting your site.
67In addition you can get referrer information as well as language and supported character-set information.</p>
68
69<h2>Initializing the Class</h2>
70
Derek Allardd2df9bc2007-04-15 17:41:17 +000071<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>
adminada5fa32006-10-09 21:40:36 +000072
73<code>$this->load->library('user_agent');</code>
74<p>Once loaded, the object will be available using: <dfn>$this->agent</dfn></p>
adminada5fa32006-10-09 21:40:36 +000075
76<h2>User Agent Definitions</h2>
77
78<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
79various user agent arrays if needed.</p>
80
81<h2>Example</h2>
82
83<p>When the User Agent class is initialized it will attempt to determine whether the user agent browsing your site is
Derek Allardc6441282007-07-04 23:54:32 +000084a web browser, a mobile device, or a robot. It will also gather the platform information if it is available.</p>
adminada5fa32006-10-09 21:40:36 +000085
86
87<code>
88$this->load->library('user_agent');<br />
89<br />
90if ($this->agent->is_browser())<br />
91{<br />
92&nbsp;&nbsp;&nbsp;&nbsp;$agent = $this->agent->browser().' '.$this->agent->version();<br />
93}<br />
94elseif ($this->agent->is_robot())<br />
95{<br />
96&nbsp;&nbsp;&nbsp;&nbsp;$agent = $this->agent->robot();<br />
97}<br />
98elseif ($this->agent->is_mobile())<br />
99{<br />
100&nbsp;&nbsp;&nbsp;&nbsp;$agent = $this->agent->mobile();<br />
101}<br />
102else<br />
103{<br />
104&nbsp;&nbsp;&nbsp;&nbsp;$agent = 'Unidentified User Agent';<br />
105}<br />
106<br />
107echo $agent;<br />
108<br />
109echo $this->agent->platform(); // Platform info (Windows, Linux, Mac, etc.)
110</code>
111
112
adminada5fa32006-10-09 21:40:36 +0000113<h1>Function Reference</h1>
114
115
116<h2>$this->agent->is_browser()</h2>
117<p>Returns TRUE/FALSE (boolean) if the user agent is a known web browser.</p>
118
119<h2>$this->agent->is_mobile()</h2>
120<p>Returns TRUE/FALSE (boolean) if the user agent is a known mobile device.</p>
121
122<h2>$this->agent->is_robot()</h2>
123<p>Returns TRUE/FALSE (boolean) if the user agent is a known robot.</p>
124
125<p class="important"><strong>Note:</strong>&nbsp; The user agent library only contains the most common robot
admine334c472006-10-21 19:44:22 +0000126definitions. It is not a complete list of bots. There are hundreds of them so searching for each one would not be
adminada5fa32006-10-09 21:40:36 +0000127very efficient. If you find that some bots that commonly visit your site are missing from the list you can add them to your
128<dfn>application/config/user_agents.php</dfn> file.</p>
129
130<h2>$this->agent->is_referral()</h2>
131<p>Returns TRUE/FALSE (boolean) if the user agent was referred from another site.</p>
132
133
134<h2>$this->agent->browser()</h2>
135<p>Returns a string containing the name of the web browser viewing your site.</p>
136
137<h2>$this->agent->version()</h2>
138<p>Returns a string containing the version number of the web browser viewing your site.</p>
139
140<h2>$this->agent->mobile()</h2>
141<p>Returns a string containing the name of the mobile device viewing your site.</p>
142
143<h2>$this->agent->robot()</h2>
144<p>Returns a string containing the name of the robot viewing your site.</p>
145
146<h2>$this->agent->platform()</h2>
147<p>Returns a string containing the platform viewing your site (Linux, Windows, OS X, etc.).</p>
148
149<h2>$this->agent->referrer()</h2>
150<p>The referrer, if the user agent was referred from another site. Typically you'll test for this as follows:</p>
151
152<code> if ($this->agent->is_referral())<br />
153{<br />
154&nbsp;&nbsp;&nbsp;&nbsp;echo $this->agent->referrer();<br />
155}</code>
156
157
158<h2>$this->agent->agent_string()</h2>
159<p>Returns a string containing the full user agent string. Typically it will be something like this:</p>
160
161<code>Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.0.4) Gecko/20060613 Camino/1.0.2</code>
162
163
164<h2>$this->agent->accept_lang()</h2>
165<p>Lets you determine if the user agent accepts a particular language. Example:</p>
166
167<code>if ($this->agent->accept_lang('en'))<br />
168{<br />
169&nbsp;&nbsp;&nbsp;&nbsp;echo 'You accept English!';<br />
Derek Allardc6441282007-07-04 23:54:32 +0000170}</code>
adminada5fa32006-10-09 21:40:36 +0000171
172<p class="important"><strong>Note:</strong> This function is not typically very reliable
173since some browsers do not provide language info, and even among those that do, it is not always accurate. </p>
174
175
176
177<h2>$this->agent->accept_charset()</h2>
178<p>Lets you determine if the user agent accepts a particular language. Example:</p>
179
180<code>if ($this->agent->accept_charset('utf-8'))<br />
181{<br />
182&nbsp;&nbsp;&nbsp;&nbsp;echo 'You browser supports UTF-8!';<br />
Derek Allardc6441282007-07-04 23:54:32 +0000183}</code>
adminada5fa32006-10-09 21:40:36 +0000184
185<p class="important"><strong>Note:</strong> This function is not typically very reliable
186since some browsers do not provide character-set info, and even among those that do, it is not always accurate. </p>
187
188
189
190</div>
191<!-- END CONTENT -->
192
193
194<div id="footer">
195<p>
196Previous Topic:&nbsp;&nbsp;<a href="uri.html">URI Class</a>
197&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
198<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
199<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
200Next Topic:&nbsp;&nbsp;<a href="validation.html">Validation Class</a>
Derek Allardc6441282007-07-04 23:54:32 +0000201</p>
Derek Allardd2df9bc2007-04-15 17:41:17 +0000202<p><a href="http://www.codeigniter.com">CodeIgniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2007 &nbsp;&middot;&nbsp; <a href="http://ellislab.com/">Ellislab, Inc.</a></p>
adminada5fa32006-10-09 21:40:36 +0000203</div>
204
205</body>
206</html>