Modifying Priyadi’s Browser Sniff Plugin
Well, since Safari became my favorite browser for everyday surfing on the internet, formerly always using Firefox or Opera, hence everything, of course are done with Safari
, one of these is for blogging activities. Unfortunately, i got annoying probem when blogging using the absolutely amazing Safari, it caused by Priyadi’s Browser Sniff plugin, yeah actually its a pretty kewl plugin and very useful for me, but the result of it detection is unmatched properly. I mean, the Windows Vista (i’m currently using it, and my beautiful Safari has installed on there), has detected as Mac OS X, understand? not yet? Okay, let me say in other word,
…so, Safari on Windows Vista has detected as Safari for Mac OS X
, it totally odd, eh?
.
Before we go, let’s see the original code of Priyadi’s browser Sniff to detect Safari browser specifically, (only on Apple and/or Intel’s Mac OS X):
259 260 261 262 263 264 265 | } elseif (preg_match('#Safari/([a-zA-Z0-9.]+)#i', $ua, $matches)) { $browser_name = 'Safari'; $browser_code = 'safari'; $browser_ver = $matches[1]; $os_name = "Mac OS"; $os_code = "macos"; $os_ver = "X"; |
To make it work properly, unavoidably, the code above need to be fixed, and after investigating throughout the php script of that plugin during a couple of minutes, I successfully get a simple way to fix it by adding some functions into there. And the fix code would look something like this:
259 260 261 262 263 264 265 266 | } elseif (preg_match('#Safari/([a-zA-Z0-9.]+)#i', $ua, $matches)) { $browser_name = 'Safari'; $browser_code = 'safari'; $browser_ver = $matches[1]; list($os_name, $os_code, $os_ver) = pri_unix_detect_os($ua); if (!$os_name) { list($os_name, $os_code, $os_ver) = pri_windows_detect_os($ua); } |
As you can see above, there’s NO new code has been added, i just added some functions rules that simply detects and get the correct Safari datas as according to the operating system is on Mac OS X or on Windows (Vista). Notice that, fix code above work wonderfully to detect Safari NOT only on Windows Vista, but also Safari on another Windows NT derivative too, likes 2000, XP, and 2003
.
Perhaps you’re thinking: “is that modification has made the Priyadi’s browser Sniff to NO unmatched detection of other browser type on specific operating system?” You’re right in posing this question, because Priyadi’s browser Sniff actually still can’t detect another browsers type correctly, and one of these is Netscape (Navigator) 9.0b2 where be detected as Mozilla Firefox 2.0.0.4, Netscape 9.0b3 as Mozilla Firefox 2.0.0.6, Netscape 9.0.0.1 and 9.0.0.2 as Mozilla Firefox 2.0.0.8, and Netscape 9.0.0.3 (the latest version) as Mozilla Firefox 2.0.0.9, and perhaps the future version too. It happened on all Windows NT derivative, Mac OS X, and Linux (i’ve tested it).
Must be remembered: the default code of Priyadi’s browser Sniff just can detect Netscape (now, it called “Navigator” or sometime “Netscape Navigator”) correctly ’til version 9.0 only. Ask why? …’cause since Netscape Communications released the latest version of their browser (Navigator 9.0), bringing back the “Navigator” name and changing the user interface to match the one used in Mozilla Firefox 2. In other words, Navigator 9.0 shares an architecture with the latest Mozilla technologies, as such: Navigator 9.0 will let you install extensions that are compatible with Firefox. Thereby, there’re a bunch has been changed, one of these is the user agent, it happened since Navigator version 9.0bx and up.
By the way, below is the Priyadi’s browser Sniff default code to detect Netscape (before called “Navigator”) lower or equal to 9.0 on Windows, Mac OS X, and Linux:
367 368 369 370 371 372 373 374 375 | } elseif (preg_match('#Netscape[0-9]?/([a-zA-Z0-9.]+)#i', $ua, $matches)) { $browser_name = 'Netscape'; $browser_code = 'netscape'; $browser_ver = $matches[1]; if (preg_match('/Windows/i', $ua)) { list($os_name, $os_code, $os_ver) = pri_windows_detect_os($ua); } else { list($os_name, $os_code, $os_ver) = pri_unix_detect_os($ua); } |
…and here the Priyadi’s code to detect Navigator or Netscape Navigator 9.0bx and up (with Firefox fixes or after using Mozilla technologies) both on Windows, and *Nix (Linux & Mac OS X) too:
385 386 387 388 389 390 391 392 393 394 | } elseif (preg_match('#^Mozilla/([a-zA-Z0-9.]+)#i', $ua, $matches)) { $browser_name = 'Netscape Navigator'; $browser_code = 'netscape'; $browser_ver = $matches[1]; if (preg_match('/Win/i', $ua)) { list($os_name, $os_code, $os_ver) = pri_windows_detect_os($ua); } else { list($os_name, $os_code, $os_ver) = pri_unix_detect_os($ua); } } |
Fortunately, Priyadi’s browser Sniff came with a bunch of condision statement, and of course, i’ll use it to fix that plugin for at twice. But wait, its very possible that there’re someone who has done it, thus searching in google is be a good job. At the most 15 minutes, google bring me to a nice place, that’s Arif Suparlan’s blog. Am i get a solution on there? absolutely NO, but i found that guys has fixed, *far of day before I do this*
>-, the Priyadi’s browser Sniff using pretty nice ways to make the new Safari on Windows XP can be detected properly. Arif’s fix code as seen here:
1 2 3 4 5 6 7 8 9 10 | } elseif (preg_match('#Safari/([a-zA-Z0-9.]+)#i', $ua, $matches)) { $browser_name = 'Safari'; $browser_code = 'safari'; $browser_ver = $matches[1]; list($os_name, $os_code, $os_ver) = pri_windows_detect_os($ua); if (!$os_name) { $os_name = "Mac OS"; $os_code = "macos"; $os_ver = "X"; } |
Don’t forget, Arif’s pretty nice fix code above work totally same like my fix code, that’s can detect Safari on all Windows NT derivative too (2000, XP, 2003, Vista).
Now, back to our second problem, …so same as the first fix for Safari, this second modification is to make Netscape Navigator version 9.0bx and up will not be detected as Mozilla Firefox 2.0.0.x on Windows NT derivative, Linux, and Mac OS X by Priyadi’s browser Sniff. It also not too difficult to do, ask why? …because since it plugin is using simple if elseif statement, so we just might need only a few times to comprehending about how that plugin work, and of course, we can do the same kind like our first fix for Safari.
Okay, like our first modification code, here we’ll need to adding some function into there to get correct browser user agent, again on the certain operating system so that it can be detected correctly. And here is our working code:
1 2 3 4 5 6 7 8 9 | } elseif (preg_match('#(Navigator)/([a-zA-Z0-9.]+)#i', $ua, $matches)) { $browser_name = 'Netscape Navigator'; $browser_code = 'navigator'; $browser_ver = $matches[2]; if (preg_match('/Windows/i', $ua)) { list($os_name, $os_code, $os_ver) = pri_windows_detect_os($ua); } else { list($os_name, $os_code, $os_ver) = pri_unix_detect_os($ua); } |
Please keep in mind, the working code above can properly detect Netscape/Navigator 9.0bx and up on all Windows NT derivatives, also on Linux, and Mac OS X
.
It latest modification still is questionable, eh?. So i attempt to test post a comment on a “Post” in my own blog using another browsers type. Firstly, i use Phoenix 0.5 browser on Mac OS X (i’m using my friend’s Powerbook), …and the result? oppss, it simply detected as Mozilla Firefox 0.5
. Note that this not only happened in Phoenix 0.5 on Windows NT derivative, but also in Phoenix all version, 0.1 to 0.5, on *Nix operating system (Linux and Mac OS X). So, it mean we need to fix Priyadi’s code again, again and again
.
Well, please take a look at Priyadi’s browser Sniff’s php script in line 165:
165 166 167 168 169 170 171 172 173 | } elseif (preg_match('#(Firefox|Phoenix|Firebird)/([a-zA-Z0-9.]+)#i', $ua, $matches)) { $browser_name = 'Mozilla Firefox'; $browser_code = 'firefox'; $browser_ver = $matches[2]; if (preg_match('/Windows/i', $ua)) { list($os_name, $os_code, $os_ver) = pri_windows_detect_os($ua); } else { list($os_name, $os_code, $os_ver) = pri_unix_detect_os($ua); } |
To make it work, we just need a bit trick: copy the whole of that code, then paste it to whatever you want, but of course, still in the Priyadi’s php plugin, and lastly, remove the “Firefox” and “Firebird” words from the statement. Below is our modification result:
1 2 3 4 5 6 7 8 9 | } elseif (preg_match('#(Phoenix)/([a-zA-Z0-9.]+)#i', $ua, $matches)) { $browser_name = 'Phoenix'; $browser_code = 'phoenix'; $browser_ver = $matches[2]; if (preg_match('/Windows/i', $ua)) { list($os_name, $os_code, $os_ver) = pri_windows_detect_os($ua); } else { list($os_name, $os_code, $os_ver) = pri_unix_detect_os($ua); } |
Done ! ![]()
Last but not least, perhaps still there’s some browsers type that still can’t be detected correctly by this pretty nice plugin, so please let me know about that, at least i’ll try to seeking for a fix, and of course, it will be posted here. Thanks you
.
PS: You can test the default output of Priyadi’s browser Sniff plugin by post a comment in Priyadi’s blog.


From Jakarta
Using
Test post using Safari 3.0.4 on Windows Vista
From Jakarta
Using
And this’s a test using Netscape Navigator 9.0.0.3 on Windows Vista
From Jakarta
Using
A test using Phoenix 0.5 on Windows Vista
From Solo
Using
weeks
, postingannya panjang amat, gw gak kuat ngebaca semuanya, jadi pusing ndiri 
From Jakarta
Using
dengkul,
namanya juga artikel, kul, saya sengaja biar kayak di koran githu
From Plano
Using
Tes dengan Opera
imcw’s last blog post..Blog Milis Dokter Umum
From Ismaning
Using
lumayan
tes pake Maxton ah 
From Ismaning
Using
lha, kok kedeteknya IE7, hayooo gmn?
From Jakarta
Using
Thanks dah testing pake Maxthon… and i will be back as soon as possible with the fix, okay?
From Jakarta
Using
@imcw
hehehehehe… pak dokter, browser opera itu sebenarnya cool ‘khan? bisa mencegah mata perih sewaktu blogging

From Ismaning
Using
Stanch, kok kelamaan yah
From Jakarta
Using
Detected.. ! hehehehe….
, btw, sorry too late buat kasih respon 
Stanch’s last blog post..Commentluv v0.992 Now Available
From Ismaning
Using
kekekeke, seep
skrng tes pake Grand Paradiso 3.0a7 
From Ismaning
Using
lhaaa kok nggak Using Grand Paradiso 3.0a7 kekekeke
hayo harus tanggung ****b lg dunk

From Chicago
Using
i’ve commented in priyadi’s blog @ browser detection section, see comment number 304. what was i said on there, this plugin was detects Safari version incorrectly, it should say: 3.0.3 rather than 522.12.1, and your fix code on this post is still can’t detect it proprerly
king regards
-luke
Luke’s last blog post..Gordon Freeman’s Legendary Air Duct Crawling Skills
From Jakarta
Using
@Luke
Its really easy to make Safari can be detected properly, like what you want. You can use the following code:
…and now, this plugin has detected it correctly
@koboi
liat sendiri khan kalo dah kedetek, hihihihi..
btw, koboi, semua yg kita inginkan itu dah ada di dalam plugin tsb, tinggal disesuakan dengan model UAS dari masing-masing browser. 
Stanch’s last blog post..Commentluv v0.992 Now Available
From Chicago
Using
Stanch, big thanks to you, now i am using above code
The latest Luke’s blog post is Pro Tip: Don’t Mess Up Your MX Record
From Jakarta
Using
okay luke, you are welcome
The latest Stanch’s blog post is Tentang Konsep Pluralisme Agama