Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 1 | ################ |
| 2 | User Agent Class |
| 3 | ################ |
| 4 | |
| 5 | The User Agent Class provides functions that help identify information |
| 6 | about the browser, mobile device, or robot visiting your site. In |
| 7 | addition you can get referrer information as well as language and |
| 8 | supported character-set information. |
| 9 | |
Andrey Andreev | 20d93e4 | 2013-09-23 16:22:23 +0300 | [diff] [blame] | 10 | .. contents:: |
Andrey Andreev | cc04209 | 2014-01-03 17:08:27 +0200 | [diff] [blame] | 11 | :local: |
Andrey Andreev | 20d93e4 | 2013-09-23 16:22:23 +0300 | [diff] [blame] | 12 | |
| 13 | .. raw:: html |
| 14 | |
Andrey Andreev | cc04209 | 2014-01-03 17:08:27 +0200 | [diff] [blame] | 15 | <div class="custom-index container"></div> |
Andrey Andreev | 20d93e4 | 2013-09-23 16:22:23 +0300 | [diff] [blame] | 16 | |
| 17 | ************************** |
| 18 | Using the User Agent Class |
| 19 | ************************** |
| 20 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 21 | Initializing the Class |
| 22 | ====================== |
| 23 | |
| 24 | Like most other classes in CodeIgniter, the User Agent class is |
| 25 | initialized in your controller using the $this->load->library function:: |
| 26 | |
| 27 | $this->load->library('user_agent'); |
| 28 | |
Andrey Andreev | 20d93e4 | 2013-09-23 16:22:23 +0300 | [diff] [blame] | 29 | Once loaded, the object will be available using: ``$this->agent`` |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 30 | |
| 31 | User Agent Definitions |
| 32 | ====================== |
| 33 | |
| 34 | The user agent name definitions are located in a config file located at: |
| 35 | application/config/user_agents.php. You may add items to the various |
| 36 | user agent arrays if needed. |
| 37 | |
| 38 | Example |
| 39 | ======= |
| 40 | |
| 41 | When the User Agent class is initialized it will attempt to determine |
| 42 | whether the user agent browsing your site is a web browser, a mobile |
| 43 | device, or a robot. It will also gather the platform information if it |
| 44 | is available. |
| 45 | |
| 46 | :: |
| 47 | |
Derek Jones | 06cd162 | 2011-10-05 15:30:47 -0500 | [diff] [blame] | 48 | $this->load->library('user_agent'); |
| 49 | |
| 50 | if ($this->agent->is_browser()) |
| 51 | { |
Andrey Andreev | 20d93e4 | 2013-09-23 16:22:23 +0300 | [diff] [blame] | 52 | $agent = $this->agent->browser().' '.$this->agent->version(); |
Derek Jones | 06cd162 | 2011-10-05 15:30:47 -0500 | [diff] [blame] | 53 | } |
| 54 | elseif ($this->agent->is_robot()) |
| 55 | { |
Andrey Andreev | 20d93e4 | 2013-09-23 16:22:23 +0300 | [diff] [blame] | 56 | $agent = $this->agent->robot(); |
Derek Jones | 06cd162 | 2011-10-05 15:30:47 -0500 | [diff] [blame] | 57 | } |
| 58 | elseif ($this->agent->is_mobile()) |
| 59 | { |
Andrey Andreev | 20d93e4 | 2013-09-23 16:22:23 +0300 | [diff] [blame] | 60 | $agent = $this->agent->mobile(); |
Derek Jones | 06cd162 | 2011-10-05 15:30:47 -0500 | [diff] [blame] | 61 | } |
| 62 | else |
| 63 | { |
Andrey Andreev | 20d93e4 | 2013-09-23 16:22:23 +0300 | [diff] [blame] | 64 | $agent = 'Unidentified User Agent'; |
Derek Jones | 06cd162 | 2011-10-05 15:30:47 -0500 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | echo $agent; |
| 68 | |
| 69 | echo $this->agent->platform(); // Platform info (Windows, Linux, Mac, etc.) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 70 | |
Andrey Andreev | 20d93e4 | 2013-09-23 16:22:23 +0300 | [diff] [blame] | 71 | *************** |
| 72 | Class Reference |
| 73 | *************** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 74 | |
Andrey Andreev | 20d93e4 | 2013-09-23 16:22:23 +0300 | [diff] [blame] | 75 | .. class: CI_User_agent |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 76 | |
Andrey Andreev | 20d93e4 | 2013-09-23 16:22:23 +0300 | [diff] [blame] | 77 | .. method:: is_browser([$key = NULL]) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 78 | |
Andrey Andreev | 20d93e4 | 2013-09-23 16:22:23 +0300 | [diff] [blame] | 79 | :param string $key: optional browser name |
| 80 | :returns: bool |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 81 | |
Andrey Andreev | 20d93e4 | 2013-09-23 16:22:23 +0300 | [diff] [blame] | 82 | Returns TRUE/FALSE (boolean) if the user agent is a known web browser. |
| 83 | :: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 84 | |
Andrey Andreev | 20d93e4 | 2013-09-23 16:22:23 +0300 | [diff] [blame] | 85 | if ($this->agent->is_browser('Safari')) |
| 86 | { |
| 87 | echo 'You are using Safari.'; |
| 88 | } |
| 89 | elseif ($this->agent->is_browser()) |
| 90 | { |
| 91 | echo 'You are using a browser.'; |
| 92 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 93 | |
Andrey Andreev | 20d93e4 | 2013-09-23 16:22:23 +0300 | [diff] [blame] | 94 | .. note:: The string "Safari" in this example is an array key in the list of browser definitions. |
| 95 | You can find this list in **application/config/user_agents.php** if you want to add new |
| 96 | browsers or change the stings. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 97 | |
Andrey Andreev | 20d93e4 | 2013-09-23 16:22:23 +0300 | [diff] [blame] | 98 | .. method:: is_mobile([$key = NULL]) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 99 | |
Andrey Andreev | 20d93e4 | 2013-09-23 16:22:23 +0300 | [diff] [blame] | 100 | :param string $key: optional mobile device name |
| 101 | :returns: bool |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 102 | |
Andrey Andreev | 20d93e4 | 2013-09-23 16:22:23 +0300 | [diff] [blame] | 103 | Returns TRUE/FALSE (boolean) if the user agent is a known mobile device. |
| 104 | :: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 105 | |
Andrey Andreev | 20d93e4 | 2013-09-23 16:22:23 +0300 | [diff] [blame] | 106 | if ($this->agent->is_mobile('iphone')) |
| 107 | { |
| 108 | $this->load->view('iphone/home'); |
| 109 | } |
| 110 | elseif ($this->agent->is_mobile()) |
| 111 | { |
| 112 | $this->load->view('mobile/home'); |
| 113 | } |
| 114 | else |
| 115 | { |
| 116 | $this->load->view('web/home'); |
| 117 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 118 | |
Andrey Andreev | 20d93e4 | 2013-09-23 16:22:23 +0300 | [diff] [blame] | 119 | .. method:: is_robot([$key = NULL]) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 120 | |
Andrey Andreev | 20d93e4 | 2013-09-23 16:22:23 +0300 | [diff] [blame] | 121 | :param string $key: optional robot name |
| 122 | :returns: bool |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 123 | |
Andrey Andreev | 20d93e4 | 2013-09-23 16:22:23 +0300 | [diff] [blame] | 124 | Returns TRUE/FALSE (boolean) if the user agent is a known robot. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 125 | |
Andrey Andreev | 20d93e4 | 2013-09-23 16:22:23 +0300 | [diff] [blame] | 126 | .. note:: The user agent library only contains the most common robot definitions. It is not a complete list of bots. |
| 127 | There are hundreds of them so searching for each one would not be very efficient. If you find that some bots |
| 128 | that commonly visit your site are missing from the list you can add them to your |
| 129 | **application/config/user_agents.php** file. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 130 | |
Andrey Andreev | 20d93e4 | 2013-09-23 16:22:23 +0300 | [diff] [blame] | 131 | .. method:: is_referral() |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 132 | |
Andrey Andreev | 20d93e4 | 2013-09-23 16:22:23 +0300 | [diff] [blame] | 133 | :returns: bool |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 134 | |
Andrey Andreev | 20d93e4 | 2013-09-23 16:22:23 +0300 | [diff] [blame] | 135 | Returns TRUE/FALSE (boolean) if the user agent was referred from another site. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 136 | |
Andrey Andreev | 20d93e4 | 2013-09-23 16:22:23 +0300 | [diff] [blame] | 137 | .. method:: browser() |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 138 | |
Andrey Andreev | 20d93e4 | 2013-09-23 16:22:23 +0300 | [diff] [blame] | 139 | :returns: string |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 140 | |
Andrey Andreev | 20d93e4 | 2013-09-23 16:22:23 +0300 | [diff] [blame] | 141 | Returns a string containing the name of the web browser viewing your site. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 142 | |
Andrey Andreev | 20d93e4 | 2013-09-23 16:22:23 +0300 | [diff] [blame] | 143 | .. method:: version() |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 144 | |
Andrey Andreev | 20d93e4 | 2013-09-23 16:22:23 +0300 | [diff] [blame] | 145 | :returns: string |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 146 | |
Andrey Andreev | 20d93e4 | 2013-09-23 16:22:23 +0300 | [diff] [blame] | 147 | Returns a string containing the version number of the web browser viewing your site. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 148 | |
Andrey Andreev | 20d93e4 | 2013-09-23 16:22:23 +0300 | [diff] [blame] | 149 | .. method:: mobile() |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 150 | |
Andrey Andreev | 20d93e4 | 2013-09-23 16:22:23 +0300 | [diff] [blame] | 151 | :returns: string |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 152 | |
Andrey Andreev | 20d93e4 | 2013-09-23 16:22:23 +0300 | [diff] [blame] | 153 | Returns a string containing the name of the mobile device viewing your site. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 154 | |
Andrey Andreev | 20d93e4 | 2013-09-23 16:22:23 +0300 | [diff] [blame] | 155 | .. method:: robot() |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 156 | |
Andrey Andreev | 20d93e4 | 2013-09-23 16:22:23 +0300 | [diff] [blame] | 157 | :returns: string |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 158 | |
Andrey Andreev | 20d93e4 | 2013-09-23 16:22:23 +0300 | [diff] [blame] | 159 | Returns a string containing the name of the robot viewing your site. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 160 | |
Andrey Andreev | 20d93e4 | 2013-09-23 16:22:23 +0300 | [diff] [blame] | 161 | .. method:: platform() |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 162 | |
Andrey Andreev | 20d93e4 | 2013-09-23 16:22:23 +0300 | [diff] [blame] | 163 | :returns: string |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 164 | |
Andrey Andreev | 20d93e4 | 2013-09-23 16:22:23 +0300 | [diff] [blame] | 165 | Returns a string containing the platform viewing your site (Linux, Windows, OS X, etc.). |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 166 | |
Andrey Andreev | 20d93e4 | 2013-09-23 16:22:23 +0300 | [diff] [blame] | 167 | .. method:: referrer() |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 168 | |
Andrey Andreev | 20d93e4 | 2013-09-23 16:22:23 +0300 | [diff] [blame] | 169 | :returns: string |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 170 | |
Andrey Andreev | 20d93e4 | 2013-09-23 16:22:23 +0300 | [diff] [blame] | 171 | The referrer, if the user agent was referred from another site. Typically you'll test for this as follows:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 172 | |
Andrey Andreev | 20d93e4 | 2013-09-23 16:22:23 +0300 | [diff] [blame] | 173 | if ($this->agent->is_referral()) |
| 174 | { |
| 175 | echo $this->agent->referrer(); |
| 176 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 177 | |
Andrey Andreev | 20d93e4 | 2013-09-23 16:22:23 +0300 | [diff] [blame] | 178 | .. method:: agent_string() |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 179 | |
Andrey Andreev | 20d93e4 | 2013-09-23 16:22:23 +0300 | [diff] [blame] | 180 | :returns: string |
| 181 | |
| 182 | Returns a string containing the full user agent string. Typically it will be something like this:: |
| 183 | |
| 184 | Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.0.4) Gecko/20060613 Camino/1.0.2 |
| 185 | |
| 186 | .. method:: accept_lang([$lang = 'en']) |
| 187 | |
| 188 | :param string $lang: language key |
| 189 | :returns: bool |
| 190 | |
| 191 | Lets you determine if the user agent accepts a particular language. Example:: |
| 192 | |
| 193 | if ($this->agent->accept_lang('en')) |
| 194 | { |
| 195 | echo 'You accept English!'; |
| 196 | } |
| 197 | |
| 198 | .. note:: This method is not typically very reliable since some browsers do not provide language info, |
| 199 | and even among those that do, it is not always accurate. |
| 200 | |
| 201 | .. method:: languages() |
| 202 | |
| 203 | :returns: array |
| 204 | |
| 205 | Returns an array of languages supported by the user agent. |
| 206 | |
| 207 | .. method:: accept_charset([$charset = 'utf-8']) |
| 208 | |
| 209 | :param string $charset: character set |
| 210 | :returns: bool |
| 211 | |
| 212 | Lets you determine if the user agent accepts a particular character set. Example:: |
| 213 | |
| 214 | if ($this->agent->accept_charset('utf-8')) |
| 215 | { |
| 216 | echo 'You browser supports UTF-8!'; |
| 217 | } |
| 218 | |
| 219 | .. note:: This method is not typically very reliable since some browsers do not provide character-set info, |
| 220 | and even among those that do, it is not always accurate. |
| 221 | |
| 222 | .. method:: charsets() |
| 223 | |
| 224 | :returns: array |
| 225 | |
Andrey Andreev | 1f811c3 | 2014-01-09 01:02:38 +0200 | [diff] [blame^] | 226 | Returns an array of character sets accepted by the user agent. |
| 227 | |
| 228 | .. method:: parse($string) |
| 229 | |
| 230 | :param string $string: A custom user-agent string |
| 231 | :returns: void |
| 232 | |
| 233 | Parses a custom user-agent string, different from the one reported by the current visitor. |