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