blob: 50f4b77f4d2bc3a9a7b76cc5004c0153f3c714fc [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
5<title>Code Igniter User Guide</title>
6
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() {
15 myHeight = new fx.Height('nav', {duration: 400});
16 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' />
25<meta name='description' content='Code Igniter User Guide' />
26
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>
36<td><h1>Code Igniter User Guide Version 1.5.0b1</h1></td>
37<td id="breadcrumb_right"><a href="../toc.html">Full Table of Contents</a></td>
38</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">
48<a href="http://www.codeigniter.com/">Code Igniter Home</a> &nbsp;&#8250;&nbsp;
49<a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
50User Agent Class
51</td>
52<td id="searchbox"><form method="get" action="http://www.google.com/search"><input type="hidden" name="as_sitesearch" id="as_sitesearch" value="www.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>
53</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
71<p>Like most other classes in Code Igniter, the User Agent class is initialized in your controller using the <dfn>$this->load->library</dfn> function:</p>
72
73<code>$this->load->library('user_agent');</code>
74<p>Once loaded, the object will be available using: <dfn>$this->agent</dfn></p>
75<p>You can also set your own class variable name. Please see the <a href="loader.html">Loader Class</a> for more info.</p>
76
77<h2>User Agent Definitions</h2>
78
79<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
80various user agent arrays if needed.</p>
81
82<h2>Example</h2>
83
84<p>When the User Agent class is initialized it will attempt to determine whether the user agent browsing your site is
85a web browser, a mobile device, or a robot. It will also gather the platform information if it is available.
86
87
88<code>
89$this->load->library('user_agent');<br />
90<br />
91if ($this->agent->is_browser())<br />
92{<br />
93&nbsp;&nbsp;&nbsp;&nbsp;$agent = $this->agent->browser().' '.$this->agent->version();<br />
94}<br />
95elseif ($this->agent->is_robot())<br />
96{<br />
97&nbsp;&nbsp;&nbsp;&nbsp;$agent = $this->agent->robot();<br />
98}<br />
99elseif ($this->agent->is_mobile())<br />
100{<br />
101&nbsp;&nbsp;&nbsp;&nbsp;$agent = $this->agent->mobile();<br />
102}<br />
103else<br />
104{<br />
105&nbsp;&nbsp;&nbsp;&nbsp;$agent = 'Unidentified User Agent';<br />
106}<br />
107<br />
108echo $agent;<br />
109<br />
110echo $this->agent->platform(); // Platform info (Windows, Linux, Mac, etc.)
111</code>
112
113
114
115
116<br />
117<h1>Function Reference</h1>
118
119
120<h2>$this->agent->is_browser()</h2>
121<p>Returns TRUE/FALSE (boolean) if the user agent is a known web browser.</p>
122
123<h2>$this->agent->is_mobile()</h2>
124<p>Returns TRUE/FALSE (boolean) if the user agent is a known mobile device.</p>
125
126<h2>$this->agent->is_robot()</h2>
127<p>Returns TRUE/FALSE (boolean) if the user agent is a known robot.</p>
128
129<p class="important"><strong>Note:</strong>&nbsp; The user agent library only contains the most common robot
130definitions. It is not a complete list of bots. There are hundreds of them so searching for each one would not be
131very efficient. If you find that some bots that commonly visit your site are missing from the list you can add them to your
132<dfn>application/config/user_agents.php</dfn> file.</p>
133
134<h2>$this->agent->is_referral()</h2>
135<p>Returns TRUE/FALSE (boolean) if the user agent was referred from another site.</p>
136
137
138<h2>$this->agent->browser()</h2>
139<p>Returns a string containing the name of the web browser viewing your site.</p>
140
141<h2>$this->agent->version()</h2>
142<p>Returns a string containing the version number of the web browser viewing your site.</p>
143
144<h2>$this->agent->mobile()</h2>
145<p>Returns a string containing the name of the mobile device viewing your site.</p>
146
147<h2>$this->agent->robot()</h2>
148<p>Returns a string containing the name of the robot viewing your site.</p>
149
150<h2>$this->agent->platform()</h2>
151<p>Returns a string containing the platform viewing your site (Linux, Windows, OS X, etc.).</p>
152
153<h2>$this->agent->referrer()</h2>
154<p>The referrer, if the user agent was referred from another site. Typically you'll test for this as follows:</p>
155
156<code> if ($this->agent->is_referral())<br />
157{<br />
158&nbsp;&nbsp;&nbsp;&nbsp;echo $this->agent->referrer();<br />
159}</code>
160
161
162<h2>$this->agent->agent_string()</h2>
163<p>Returns a string containing the full user agent string. Typically it will be something like this:</p>
164
165<code>Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.0.4) Gecko/20060613 Camino/1.0.2</code>
166
167
168<h2>$this->agent->accept_lang()</h2>
169<p>Lets you determine if the user agent accepts a particular language. Example:</p>
170
171<code>if ($this->agent->accept_lang('en'))<br />
172{<br />
173&nbsp;&nbsp;&nbsp;&nbsp;echo 'You accept English!';<br />
174}</cod>
175
176<p class="important"><strong>Note:</strong> This function is not typically very reliable
177since some browsers do not provide language info, and even among those that do, it is not always accurate. </p>
178
179
180
181<h2>$this->agent->accept_charset()</h2>
182<p>Lets you determine if the user agent accepts a particular language. Example:</p>
183
184<code>if ($this->agent->accept_charset('utf-8'))<br />
185{<br />
186&nbsp;&nbsp;&nbsp;&nbsp;echo 'You browser supports UTF-8!';<br />
187}</cod>
188
189<p class="important"><strong>Note:</strong> This function is not typically very reliable
190since some browsers do not provide character-set info, and even among those that do, it is not always accurate. </p>
191
192
193
194</div>
195<!-- END CONTENT -->
196
197
198<div id="footer">
199<p>
200Previous Topic:&nbsp;&nbsp;<a href="uri.html">URI Class</a>
201&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
202<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
203<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
204Next Topic:&nbsp;&nbsp;<a href="validation.html">Validation Class</a>
205<p>
206<p><a href="http://www.codeigniter.com">Code Igniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2006 &nbsp;&middot;&nbsp; <a href="http://www.pmachine.com">pMachine, Inc.</a></p>
207</div>
208
209</body>
210</html>