admin | ada5fa3 | 2006-10-09 21:40:36 +0000 | [diff] [blame] | 1 | <!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">
|
| 14 | window.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>
|
admin | 4979a12 | 2006-10-11 19:40:07 +0000 | [diff] [blame^] | 37 | <td id="breadcrumb_right"><a href="../toc.html">Linear Table of Contents</a></td>
|
admin | ada5fa3 | 2006-10-09 21:40:36 +0000 | [diff] [blame] | 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> ›
|
| 49 | <a href="../index.html">User Guide Home</a> ›
|
| 50 | User 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 <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" /> <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.
|
| 67 | In 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>
|
admin | ada5fa3 | 2006-10-09 21:40:36 +0000 | [diff] [blame] | 75 |
|
| 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
|
| 79 | various 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
|
| 84 | a web browser, a mobile device, or a robot. It will also gather the platform information if it is available.
|
| 85 |
|
| 86 |
|
| 87 | <code>
|
| 88 | $this->load->library('user_agent');<br />
|
| 89 | <br />
|
| 90 | if ($this->agent->is_browser())<br />
|
| 91 | {<br />
|
| 92 | $agent = $this->agent->browser().' '.$this->agent->version();<br />
|
| 93 | }<br />
|
| 94 | elseif ($this->agent->is_robot())<br />
|
| 95 | {<br />
|
| 96 | $agent = $this->agent->robot();<br />
|
| 97 | }<br />
|
| 98 | elseif ($this->agent->is_mobile())<br />
|
| 99 | {<br />
|
| 100 | $agent = $this->agent->mobile();<br />
|
| 101 | }<br />
|
| 102 | else<br />
|
| 103 | {<br />
|
| 104 | $agent = 'Unidentified User Agent';<br />
|
| 105 | }<br />
|
| 106 | <br />
|
| 107 | echo $agent;<br />
|
| 108 | <br />
|
| 109 | echo $this->agent->platform(); // Platform info (Windows, Linux, Mac, etc.)
|
| 110 | </code>
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 | <br />
|
| 116 | <h1>Function Reference</h1>
|
| 117 |
|
| 118 |
|
| 119 | <h2>$this->agent->is_browser()</h2>
|
| 120 | <p>Returns TRUE/FALSE (boolean) if the user agent is a known web browser.</p>
|
| 121 |
|
| 122 | <h2>$this->agent->is_mobile()</h2>
|
| 123 | <p>Returns TRUE/FALSE (boolean) if the user agent is a known mobile device.</p>
|
| 124 |
|
| 125 | <h2>$this->agent->is_robot()</h2>
|
| 126 | <p>Returns TRUE/FALSE (boolean) if the user agent is a known robot.</p>
|
| 127 |
|
| 128 | <p class="important"><strong>Note:</strong> The user agent library only contains the most common robot
|
| 129 | definitions. It is not a complete list of bots. There are hundreds of them so searching for each one would not be
|
| 130 | very efficient. If you find that some bots that commonly visit your site are missing from the list you can add them to your
|
| 131 | <dfn>application/config/user_agents.php</dfn> file.</p>
|
| 132 |
|
| 133 | <h2>$this->agent->is_referral()</h2>
|
| 134 | <p>Returns TRUE/FALSE (boolean) if the user agent was referred from another site.</p>
|
| 135 |
|
| 136 |
|
| 137 | <h2>$this->agent->browser()</h2>
|
| 138 | <p>Returns a string containing the name of the web browser viewing your site.</p>
|
| 139 |
|
| 140 | <h2>$this->agent->version()</h2>
|
| 141 | <p>Returns a string containing the version number of the web browser viewing your site.</p>
|
| 142 |
|
| 143 | <h2>$this->agent->mobile()</h2>
|
| 144 | <p>Returns a string containing the name of the mobile device viewing your site.</p>
|
| 145 |
|
| 146 | <h2>$this->agent->robot()</h2>
|
| 147 | <p>Returns a string containing the name of the robot viewing your site.</p>
|
| 148 |
|
| 149 | <h2>$this->agent->platform()</h2>
|
| 150 | <p>Returns a string containing the platform viewing your site (Linux, Windows, OS X, etc.).</p>
|
| 151 |
|
| 152 | <h2>$this->agent->referrer()</h2>
|
| 153 | <p>The referrer, if the user agent was referred from another site. Typically you'll test for this as follows:</p>
|
| 154 |
|
| 155 | <code> if ($this->agent->is_referral())<br />
|
| 156 | {<br />
|
| 157 | echo $this->agent->referrer();<br />
|
| 158 | }</code>
|
| 159 |
|
| 160 |
|
| 161 | <h2>$this->agent->agent_string()</h2>
|
| 162 | <p>Returns a string containing the full user agent string. Typically it will be something like this:</p>
|
| 163 |
|
| 164 | <code>Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.0.4) Gecko/20060613 Camino/1.0.2</code>
|
| 165 |
|
| 166 |
|
| 167 | <h2>$this->agent->accept_lang()</h2>
|
| 168 | <p>Lets you determine if the user agent accepts a particular language. Example:</p>
|
| 169 |
|
| 170 | <code>if ($this->agent->accept_lang('en'))<br />
|
| 171 | {<br />
|
| 172 | echo 'You accept English!';<br />
|
| 173 | }</cod>
|
| 174 |
|
| 175 | <p class="important"><strong>Note:</strong> This function is not typically very reliable
|
| 176 | since some browsers do not provide language info, and even among those that do, it is not always accurate. </p>
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 | <h2>$this->agent->accept_charset()</h2>
|
| 181 | <p>Lets you determine if the user agent accepts a particular language. Example:</p>
|
| 182 |
|
| 183 | <code>if ($this->agent->accept_charset('utf-8'))<br />
|
| 184 | {<br />
|
| 185 | echo 'You browser supports UTF-8!';<br />
|
| 186 | }</cod>
|
| 187 |
|
| 188 | <p class="important"><strong>Note:</strong> This function is not typically very reliable
|
| 189 | since some browsers do not provide character-set info, and even among those that do, it is not always accurate. </p>
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 | </div>
|
| 194 | <!-- END CONTENT -->
|
| 195 |
|
| 196 |
|
| 197 | <div id="footer">
|
| 198 | <p>
|
| 199 | Previous Topic: <a href="uri.html">URI Class</a>
|
| 200 | ·
|
| 201 | <a href="#top">Top of Page</a> ·
|
| 202 | <a href="../index.html">User Guide Home</a> ·
|
| 203 | Next Topic: <a href="validation.html">Validation Class</a>
|
| 204 | <p>
|
| 205 | <p><a href="http://www.codeigniter.com">Code Igniter</a> · Copyright © 2006 · <a href="http://www.pmachine.com">pMachine, Inc.</a></p>
|
| 206 | </div>
|
| 207 |
|
| 208 | </body>
|
| 209 | </html> |