blob: 7b215a60568187722a91084044da80d417844ce2 [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 Jones733310d2009-02-11 01:13:43 +000031<td><h1>CodeIgniter User Guide Version 1.7.1</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
73<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
74various 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
79a web browser, a mobile device, or a robot. It will also gather the platform information if it is available.</p>
80
81
82<code>
83$this->load->library('user_agent');<br />
84<br />
85if ($this->agent->is_browser())<br />
86{<br />
87&nbsp;&nbsp;&nbsp;&nbsp;$agent = $this->agent->browser().' '.$this->agent->version();<br />
88}<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
114<h2>$this->agent->is_mobile()</h2>
115<p>Returns TRUE/FALSE (boolean) if the user agent is a known mobile device.</p>
116
117<h2>$this->agent->is_robot()</h2>
118<p>Returns TRUE/FALSE (boolean) if the user agent is a known robot.</p>
119
120<p class="important"><strong>Note:</strong>&nbsp; The user agent library only contains the most common robot
121definitions. It is not a complete list of bots. There are hundreds of them so searching for each one would not be
122very efficient. If you find that some bots that commonly visit your site are missing from the list you can add them to your
123<dfn>application/config/user_agents.php</dfn> file.</p>
124
125<h2>$this->agent->is_referral()</h2>
126<p>Returns TRUE/FALSE (boolean) if the user agent was referred from another site.</p>
127
128
129<h2>$this->agent->browser()</h2>
130<p>Returns a string containing the name of the web browser viewing your site.</p>
131
132<h2>$this->agent->version()</h2>
133<p>Returns a string containing the version number of the web browser viewing your site.</p>
134
135<h2>$this->agent->mobile()</h2>
136<p>Returns a string containing the name of the mobile device viewing your site.</p>
137
138<h2>$this->agent->robot()</h2>
139<p>Returns a string containing the name of the robot viewing your site.</p>
140
141<h2>$this->agent->platform()</h2>
142<p>Returns a string containing the platform viewing your site (Linux, Windows, OS X, etc.).</p>
143
144<h2>$this->agent->referrer()</h2>
145<p>The referrer, if the user agent was referred from another site. Typically you'll test for this as follows:</p>
146
147<code> if ($this->agent->is_referral())<br />
148{<br />
149&nbsp;&nbsp;&nbsp;&nbsp;echo $this->agent->referrer();<br />
150}</code>
151
152
153<h2>$this->agent->agent_string()</h2>
154<p>Returns a string containing the full user agent string. Typically it will be something like this:</p>
155
156<code>Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.0.4) Gecko/20060613 Camino/1.0.2</code>
157
158
159<h2>$this->agent->accept_lang()</h2>
160<p>Lets you determine if the user agent accepts a particular language. Example:</p>
161
162<code>if ($this->agent->accept_lang('en'))<br />
163{<br />
164&nbsp;&nbsp;&nbsp;&nbsp;echo 'You accept English!';<br />
165}</code>
166
167<p class="important"><strong>Note:</strong> This function is not typically very reliable
168since some browsers do not provide language info, and even among those that do, it is not always accurate. </p>
169
170
171
172<h2>$this->agent->accept_charset()</h2>
173<p>Lets you determine if the user agent accepts a particular character set. Example:</p>
174
175<code>if ($this->agent->accept_charset('utf-8'))<br />
176{<br />
177&nbsp;&nbsp;&nbsp;&nbsp;echo 'You browser supports UTF-8!';<br />
178}</code>
179
180<p class="important"><strong>Note:</strong> This function is not typically very reliable
181since some browsers do not provide character-set info, and even among those that do, it is not always accurate. </p>
182
183
184
185</div>
186<!-- END CONTENT -->
187
188
189<div id="footer">
190<p>
191Previous Topic:&nbsp;&nbsp;<a href="uri.html">URI Class</a>
192&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
193<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
194<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
195Next Topic:&nbsp;&nbsp;<a href="xmlrpc.html">XML-RPC Class</a>
196</p>
Derek Jonesfc395a12009-04-22 14:15:09 +0000197<p><a href="http://codeigniter.com">CodeIgniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2006-2009 &nbsp;&middot;&nbsp; <a href="http://ellislab.com/">Ellislab, Inc.</a></p>
Derek Allard2067d1a2008-11-13 22:59:24 +0000198</div>
199
200</body>
Derek Allard39b622d2008-01-16 21:10:09 +0000201</html>