[User Request] Fungsi PHP untuk detect browser (tujuan: filtering)
- Wednesday, August 19, 2009, 1:11
- Catatan Lepas, Khas Ahlul, Lagi Iseng, Pemograman, Tips & Trick, Tutorial
- 796 views
- 4 comments
Skrip ini saya gunakan juga di situs saya http://paneldotid.com
Memang keterbatasan beberapa Browser membuat situs kadang tidak tampil sempurna, oleh karena itu ada baiknya kita melakukan filtering agar situs kita dapat dibuka pada browser yang benar.
function browser_info($agent=null) {
// Declare known browsers to look for
$known = array('msie', 'firefox', 'safari', 'webkit', 'opera', 'netscape',
'konqueror', 'gecko');
// Clean up agent and build regex that matches phrases for known browsers
// (e.g. "Firefox/2.0" or "MSIE 6.0" (This only matches the major and minor
// version numbers. E.g. "2.0.0.6" is parsed as simply "2.0"
$agent = strtolower($agent ? $agent : $_SERVER['HTTP_USER_AGENT']);
$pattern = '#(?<browser>' . join('|', $known) .
')[/ ]+(?<version>[0-9]+(?:\.[0-9]+)?)#';
// Find all phrases (or return empty array if none found)
if (!preg_match_all($pattern, $agent, $matches)) return array();
// Since some UAs have more than one phrase (e.g Firefox has a Gecko phrase,
// Opera 7,8 have a MSIE phrase), use the last one found (the right-most one
// in the UA). That's usually the most correct.
$i = count($matches['browser'])-1;
return array("browser" => $matches['browser'][$i], "version" => $matches['version'][$i]);
}
contoh pemakaian:
if(browser_info()->browser == "msie") {
echo "Maaf situs ini tidak support IE, silahkan download firefox";
}
Semoga bermanfaat
About the Author
4 Comments on “[User Request] Fungsi PHP untuk detect browser (tujuan: filtering)”
Trackbacks
Write a Comment
Gravatars are small images that can show your personality. You can get your gravatar for free today!





makasih ya mas da posting artikel ini, walaupun nunggu 1 jam tapi bermanfaat..
mas cara penggunaannya di web gmn yah?