31733e08 |
1 | <?php |
2 | class userAgent { |
3 | // userAgent PHP class |
4 | // get user agent and tell us what Browser is accessing |
5 | // |
1defa974 |
6 | // function userAgent([$ua_string]) |
7 | // CONSTRUCTOR; reads UA string (or takes the optional given UA string) and gets info from that into our variables. |
31733e08 |
8 | // |
9 | // var $uastring |
10 | // the plain User Agent string |
11 | // var $brand |
12 | // returns the User Agent brand name |
13 | // var $version |
14 | // the User Agent version |
15 | // |
23585ba2 |
16 | // function getBrand() |
17 | // returns the User Agent Brand Name |
18 | // function getVersion() |
19 | // returns the User Agent version |
20 | // |
31733e08 |
21 | // function isns() |
22 | // returns true if User Agent seems to be Netscape brand, false if not |
23 | // function isns4() |
24 | // returns true if User Agent seems to be Netscape Communicator 4.x, false if not |
25 | // function isie() |
26 | // returns true if User Agent seems to be a version of Internet Exploder, false if not |
27 | // function geckobased() |
28 | // returns true if User Agent seems to be a Gecko-based browser, false if not |
29 | // function geckodate() |
30 | // returns the Gecko date when it's a Gecko-based browser, 0 if not |
1defa974 |
31 | // function khtmlbased() |
32 | // returns true if User Agent seems to be a KHTML-based browser, false if not |
33 | |
34 | // collection of some known User Agent Strings: |
35 | // Mozilla/5.0 (compatible; Konqueror/3; Linux 2.4.18; X11; i686) |
36 | // Mozilla/5.0 (X11; U; Linux i686; de-AT; rv:1.3b) Gecko/20030114 |
37 | // Mozilla/4.0 (compatible; MSIE 5.0; Windows 95; DigExt) |
38 | // Mozilla/4.0 (compatible; MSIE 6.0; Windows 98; Win 9x 4.90) |
39 | // Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1) |
40 | // Mozilla/4.75 [de] (Win98; U) |
41 | // Opera/5.12 (Windows 2000; U) [de] |
42 | // Mozilla/5.0 (Windows; U; Win 9x 4.90; de-DE; m18) Gecko/20010131 Netscape6/6.01 |
43 | // Mozilla/5.0 (Windows; U; Windows NT 5.1; de-DE; rv:1.0.1) Gecko/20020823 Netscape/7.0 |
44 | // Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/51 (like Gecko) Safari/51 |
45 | // Lynx/2.8.4rel.1 libwww-FM/2.14 SSL-MM/1.4.1 OpenSSL/0.9.6g |
46 | // Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en-US; rv:1.0.1) Gecko/20021109 Chimera/0.6+ |
47 | // Mozilla/5.0 (Windows; U; Win 9x 4.90; en-US; rv:1.3a) Gecko/20021207 Phoenix/0.5 |
48 | // Mozilla/5.0 Galeon/1.2.7 (X11; Linux i686; U;) Gecko/20021204 |
49 | // Mozilla/4.0 (compatible; MSIE 5.0; Windows XP) Opera 6.05 [ja] |
50 | // Mozilla/4.0 (compatible; MSIE 5.12; Mac_PowerPC) OmniWeb/4.1.1-v424.6 |
23585ba2 |
51 | // Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.4b) Gecko/20030516 Mozilla Firebird/0.6 |
52 | // Mozilla/5.0 (Windows; U; WinNT4.0; en-US; rv:1.5a) Gecko/20030728 Mozilla Firebird/0.6.1 |
53 | |
31733e08 |
54 | |
31733e08 |
55 | var $uastring; |
56 | var $brand; |
57 | var $version; |
58 | |
1defa974 |
59 | function userAgent($ua_string = "") { |
31733e08 |
60 | // *** constructor *** |
1defa974 |
61 | if (strlen($ua_string)) { |
62 | $this->uastring = $ua_string; |
63 | } |
64 | else { |
65 | // read raw UA string |
66 | $this->uastring = $_SERVER["HTTP_USER_AGENT"]; |
67 | } |
31733e08 |
68 | |
69 | // get UA brand and version |
70 | $this->brand = "Unknown"; $this->version = 0; |
1defa974 |
71 | if (ereg("([0-9a-zA-Z\.]+)/([0-9a-zA-Z\.+]+)", $this->uastring, $regs)) { |
72 | $this->brand = $regs[1]; // this is a reasonable default :) |
31733e08 |
73 | $this->version = $regs[2]; // this is a reasonable default :) |
74 | } |
1defa974 |
75 | if (ereg("Netscape6/([0-9a-zA-Z\.+]+)", $this->uastring, $regs)) { |
31733e08 |
76 | $this->brand = "Netscape"; |
77 | $this->version = $regs[1]; |
31733e08 |
78 | } |
1defa974 |
79 | elseif (ereg("Netscape/([0-9a-zA-Z\.+]+)", $this->uastring, $regs)) { |
31733e08 |
80 | $this->brand = "Netscape"; |
81 | $this->version = $regs[1]; |
82 | } |
1defa974 |
83 | elseif (ereg("Chimera/([0-9a-zA-Z\.+]+)", $this->uastring, $regs)) { |
84 | $this->brand = "Chimera"; |
85 | $this->version = $regs[1]; |
86 | } |
87 | elseif (ereg("Phoenix/([0-9a-zA-Z\.+]+)", $this->uastring, $regs)) { |
88 | $this->brand = "Phoenix"; |
89 | $this->version = $regs[1]; |
90 | } |
23585ba2 |
91 | elseif (ereg("Mozilla Firebird/([0-9a-zA-Z\.+]+)", $this->uastring, $regs)) { |
92 | $this->brand = "Mozilla Firebird"; |
93 | $this->version = $regs[1]; |
94 | } |
1defa974 |
95 | elseif (ereg("Galeon/([0-9a-zA-Z\.+]+)", $this->uastring, $regs)) { |
96 | $this->brand = "Galeon"; |
31733e08 |
97 | $this->version = $regs[1]; |
98 | } |
1defa974 |
99 | elseif (ereg("rv:([0-9a-zA-Z\.+]+)", $this->uastring, $regs) && strstr($this->uastring, "Mozilla/") && strstr($this->uastring, "Gecko/")) { |
31733e08 |
100 | $this->brand = "Mozilla"; |
101 | $this->version = $regs[1]; |
102 | } |
1defa974 |
103 | elseif (ereg("Opera[ /]([0-9a-zA-Z\.+]+)", $this->uastring, $regs)) { |
31733e08 |
104 | $this->brand = "Opera"; |
105 | $this->version = $regs[1]; |
106 | } |
1defa974 |
107 | elseif (ereg("OmniWeb/([0-9a-zA-Z\.+]+)", $this->uastring, $regs)) { |
108 | $this->brand = "OmniWeb"; |
109 | $this->version = $regs[1]; |
110 | } |
111 | elseif (ereg("Konqueror/([0-9a-zA-Z\.+]+)", $this->uastring, $regs)) { |
31733e08 |
112 | $this->brand = "Konqueror"; |
113 | $this->version = $regs[1]; |
114 | } |
1defa974 |
115 | elseif (ereg("Safari/([0-9a-zA-Z\.+]+)", $this->uastring, $regs)) { |
116 | $this->brand = "Safari"; |
117 | $this->version = $regs[1]; |
118 | } |
119 | elseif (ereg("AppleWebKit/([0-9a-zA-Z\.+]+)", $this->uastring, $regs)) { |
120 | $this->brand = "AppleWebKit"; |
121 | $this->version = $regs[1]; |
122 | } |
123 | elseif (ereg("MSIE ([0-9a-zA-Z\.+]+)", $this->uastring, $regs)) { |
31733e08 |
124 | $this->brand = "Microsoft Internet Explorer"; |
125 | $this->version = $regs[1]; |
126 | } |
1defa974 |
127 | elseif (ereg("Mozilla/([0-9a-zA-Z\.+]+)", $this->uastring, $regs) && !strstr($this->uastring, "compatible;") && !strstr($this->uastring, "Gecko/")) { |
128 | $this->brand = "Netscape"; |
129 | $this->version = $regs[1]; |
130 | if (intval($this->version) == 4) { $this->brand .= " Communicator"; } |
131 | } |
31733e08 |
132 | } |
133 | |
23585ba2 |
134 | function getBrand() { return $this->brand; } |
135 | function getVersion() { return $this->version; } |
136 | |
31733e08 |
137 | function isns() { |
138 | // set it static so that we don't have to call it that often |
139 | static $is_ns; |
140 | if (!isset($is_ns)) { |
141 | $is_ns = false; |
142 | if (strstr($this->brand, "Netscape")) { |
143 | $is_ns = true; |
144 | } |
145 | } |
146 | return $is_ns; |
147 | } |
148 | |
149 | function isns4() { |
150 | // set it static so that we don't have to call it that often |
151 | static $is_ns4; |
152 | if (!isset($is_ns4)) { |
153 | $is_ns4 = false; |
154 | if (strstr($this->brand, "Netscape") && (intval($this->version) == 4)) { |
155 | $is_ns4 = true; |
156 | } |
157 | } |
158 | return $is_ns4; |
159 | } |
160 | |
161 | function isie() { |
162 | // set it static so that we don't have to call it that often |
163 | static $is_ie; |
164 | if (!isset($is_ie)) { |
165 | $is_ie = false; |
166 | if (strstr($this->brand, "Internet Explorer")) { |
167 | $is_ie = true; |
168 | } |
169 | } |
170 | return $is_ie; |
171 | } |
172 | |
173 | function geckobased() { |
174 | // set it static so that we don't have to call it that often |
175 | static $is_gecko; |
176 | if (!isset($is_gecko)) { |
177 | $is_gecko = false; |
178 | if (strstr($this->uastring, "Gecko/")) { |
179 | $is_gecko = true; |
180 | } |
181 | } |
182 | return $is_gecko; |
183 | } |
184 | |
185 | function geckodate() { |
186 | // set it static so that we don't have to call it that often |
187 | static $gdate; |
188 | if (!isset($gdate)) { |
189 | $gdate = 0; |
190 | if (ereg("Gecko/([0-9]+)", $this->uastring, $regs)) { |
191 | $gdate = $regs[1]; |
192 | } |
193 | } |
194 | return $gdate; |
195 | } |
1defa974 |
196 | |
197 | function khtmlbased() { |
198 | // set it static so that we don't have to call it that often |
199 | static $is_khtml; |
200 | if (!isset($is_khtml)) { |
201 | $is_khtml = false; |
202 | if (strstr($this->brand, "Konqueror") || strstr($this->brand, "Safari") || strstr($this->brand, "AppleWebKit")) { |
203 | $is_khtml = true; |
204 | } |
205 | } |
206 | return $is_khtml; |
207 | } |
31733e08 |
208 | } |
23585ba2 |
209 | ?> |