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 | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 75 | .. php:class:: CI_User_agent |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 76 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 77 | .. php:method:: is_browser([$key = NULL]) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 78 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 79 | :param string $key: Optional browser name |
| 80 | :returns: TRUE if the user agent is a (specified) browser, FALSE if not |
| 81 | :rtype: bool |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 82 | |
Andrey Andreev | 20d93e4 | 2013-09-23 16:22:23 +0300 | [diff] [blame] | 83 | Returns TRUE/FALSE (boolean) if the user agent is a known web browser. |
| 84 | :: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 85 | |
Andrey Andreev | 20d93e4 | 2013-09-23 16:22:23 +0300 | [diff] [blame] | 86 | if ($this->agent->is_browser('Safari')) |
| 87 | { |
| 88 | echo 'You are using Safari.'; |
| 89 | } |
| 90 | elseif ($this->agent->is_browser()) |
| 91 | { |
| 92 | echo 'You are using a browser.'; |
| 93 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 94 | |
Andrey Andreev | 20d93e4 | 2013-09-23 16:22:23 +0300 | [diff] [blame] | 95 | .. note:: The string "Safari" in this example is an array key in the list of browser definitions. |
| 96 | You can find this list in **application/config/user_agents.php** if you want to add new |
| 97 | browsers or change the stings. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 98 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 99 | .. php:method:: is_mobile([$key = NULL]) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 100 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 101 | :param string $key: Optional mobile device name |
| 102 | :returns: TRUE if the user agent is a (specified) mobile device, FALSE if not |
| 103 | :rtype: bool |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 104 | |
Andrey Andreev | 20d93e4 | 2013-09-23 16:22:23 +0300 | [diff] [blame] | 105 | Returns TRUE/FALSE (boolean) if the user agent is a known mobile device. |
| 106 | :: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 107 | |
Andrey Andreev | 20d93e4 | 2013-09-23 16:22:23 +0300 | [diff] [blame] | 108 | if ($this->agent->is_mobile('iphone')) |
| 109 | { |
| 110 | $this->load->view('iphone/home'); |
| 111 | } |
| 112 | elseif ($this->agent->is_mobile()) |
| 113 | { |
| 114 | $this->load->view('mobile/home'); |
| 115 | } |
| 116 | else |
| 117 | { |
| 118 | $this->load->view('web/home'); |
| 119 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 120 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 121 | .. php:method:: is_robot([$key = NULL]) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 122 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 123 | :param string $key: Optional robot name |
| 124 | :returns: TRUE if the user agent is a (specified) robot, FALSE if not |
| 125 | :rtype: bool |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 126 | |
Andrey Andreev | 20d93e4 | 2013-09-23 16:22:23 +0300 | [diff] [blame] | 127 | Returns TRUE/FALSE (boolean) if the user agent is a known robot. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 128 | |
Andrey Andreev | 20d93e4 | 2013-09-23 16:22:23 +0300 | [diff] [blame] | 129 | .. note:: The user agent library only contains the most common robot definitions. It is not a complete list of bots. |
| 130 | There are hundreds of them so searching for each one would not be very efficient. If you find that some bots |
| 131 | that commonly visit your site are missing from the list you can add them to your |
| 132 | **application/config/user_agents.php** file. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 133 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 134 | .. php:method:: is_referral() |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 135 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 136 | :returns: TRUE if the user agent is a referral, FALSE if not |
| 137 | :rtype: bool |
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 TRUE/FALSE (boolean) if the user agent was referred from another site. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 140 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 141 | .. php:method:: browser() |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 142 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 143 | :returns: Detected browser or an empty string |
| 144 | :rtype: string |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 145 | |
Andrey Andreev | 20d93e4 | 2013-09-23 16:22:23 +0300 | [diff] [blame] | 146 | 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] | 147 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 148 | .. php:method:: version() |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 149 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 150 | :returns: Detected browser version or an empty string |
| 151 | :rtype: 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 version number of the web browser viewing your site. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 154 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 155 | .. php:method:: mobile() |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 156 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 157 | :returns: Detected mobile device brand or an empty string |
| 158 | :rtype: string |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 159 | |
Andrey Andreev | 20d93e4 | 2013-09-23 16:22:23 +0300 | [diff] [blame] | 160 | 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] | 161 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 162 | .. php:method:: robot() |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 163 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 164 | :returns: Detected robot name or an empty string |
| 165 | :rtype: string |
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 | Returns a string containing the name of the robot viewing your site. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 168 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 169 | .. php:method:: platform() |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 170 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 171 | :returns: Detected operating system or an empty string |
| 172 | :rtype: string |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 173 | |
Andrey Andreev | 20d93e4 | 2013-09-23 16:22:23 +0300 | [diff] [blame] | 174 | 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] | 175 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 176 | .. php:method:: referrer() |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 177 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 178 | :returns: Detected referrer or an empty string |
| 179 | :rtype: string |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 180 | |
Andrey Andreev | 20d93e4 | 2013-09-23 16:22:23 +0300 | [diff] [blame] | 181 | 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] | 182 | |
Andrey Andreev | 20d93e4 | 2013-09-23 16:22:23 +0300 | [diff] [blame] | 183 | if ($this->agent->is_referral()) |
| 184 | { |
| 185 | echo $this->agent->referrer(); |
| 186 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 187 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 188 | .. php:method:: agent_string() |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 189 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 190 | :returns: Full user agent string or an empty string |
| 191 | :rtype: string |
Andrey Andreev | 20d93e4 | 2013-09-23 16:22:23 +0300 | [diff] [blame] | 192 | |
| 193 | Returns a string containing the full user agent string. Typically it will be something like this:: |
| 194 | |
| 195 | Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.0.4) Gecko/20060613 Camino/1.0.2 |
| 196 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 197 | .. php:method:: accept_lang([$lang = 'en']) |
Andrey Andreev | 20d93e4 | 2013-09-23 16:22:23 +0300 | [diff] [blame] | 198 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 199 | :param string $lang: Language key |
| 200 | :returns: TRUE if provided language is accepted, FALSE if not |
| 201 | :rtype: bool |
Andrey Andreev | 20d93e4 | 2013-09-23 16:22:23 +0300 | [diff] [blame] | 202 | |
| 203 | Lets you determine if the user agent accepts a particular language. Example:: |
| 204 | |
| 205 | if ($this->agent->accept_lang('en')) |
| 206 | { |
| 207 | echo 'You accept English!'; |
| 208 | } |
| 209 | |
| 210 | .. note:: This method is not typically very reliable since some browsers do not provide language info, |
| 211 | and even among those that do, it is not always accurate. |
| 212 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 213 | .. php:method:: languages() |
Andrey Andreev | 20d93e4 | 2013-09-23 16:22:23 +0300 | [diff] [blame] | 214 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 215 | :returns: An array list of accepted languages |
| 216 | :rtype: array |
Andrey Andreev | 20d93e4 | 2013-09-23 16:22:23 +0300 | [diff] [blame] | 217 | |
| 218 | Returns an array of languages supported by the user agent. |
| 219 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 220 | .. php:method:: accept_charset([$charset = 'utf-8']) |
Andrey Andreev | 20d93e4 | 2013-09-23 16:22:23 +0300 | [diff] [blame] | 221 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 222 | :param string $charset: Character set |
| 223 | :returns: TRUE if the character set is accepted, FALSE if not |
| 224 | :rtype: bool |
Andrey Andreev | 20d93e4 | 2013-09-23 16:22:23 +0300 | [diff] [blame] | 225 | |
| 226 | Lets you determine if the user agent accepts a particular character set. Example:: |
| 227 | |
| 228 | if ($this->agent->accept_charset('utf-8')) |
| 229 | { |
| 230 | echo 'You browser supports UTF-8!'; |
| 231 | } |
| 232 | |
| 233 | .. note:: This method is not typically very reliable since some browsers do not provide character-set info, |
| 234 | and even among those that do, it is not always accurate. |
| 235 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 236 | .. php:method:: charsets() |
Andrey Andreev | 20d93e4 | 2013-09-23 16:22:23 +0300 | [diff] [blame] | 237 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 238 | :returns: An array list of accepted character sets |
| 239 | :rtype: array |
Andrey Andreev | 20d93e4 | 2013-09-23 16:22:23 +0300 | [diff] [blame] | 240 | |
Andrey Andreev | 1f811c3 | 2014-01-09 01:02:38 +0200 | [diff] [blame] | 241 | Returns an array of character sets accepted by the user agent. |
| 242 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 243 | .. php:method:: parse($string) |
Andrey Andreev | 1f811c3 | 2014-01-09 01:02:38 +0200 | [diff] [blame] | 244 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 245 | :param string $string: A custom user-agent string |
| 246 | :rtype: void |
Andrey Andreev | 1f811c3 | 2014-01-09 01:02:38 +0200 | [diff] [blame] | 247 | |
| 248 | Parses a custom user-agent string, different from the one reported by the current visitor. |