开发者

PHP Quickie: check browser type

i need code to check browser type what i want is something like this

<?php
//check br开发者_C百科owser type
if( $browserTYPE = 'Firefox')
{
     echo ('1');
}
else echo('2');

F1! f1!


$_SERVER['HTTP_USER_AGENT'];

This can be used with get_browser() http://php.net/get_browser

In your case I would suggest:

<?php
$browser = get_browser(null, true);
$browserTYPE = $browser['browser'];


Firstly, why would you need to do this? What is it about Firefox (in your example) that works so differently to Chrome, Opera, Safari, IE, etc etc that you need to check specifically for it? And what about different versions of the browsers (Firefox 2 is still Firefox but very very different to current versions)?

Anyway, if you really do need to do this then as others have said, the official way to determine is to look at the user agent string:

if(strpos($_SERVER[‘HTTP_USER_AGENT’],'Firefox') {
    //user runs firefox
} else {
    //user runs something else.
}

PHP also provides a function called get_browser() which makes things a bit easier (you don't have to know the format of the user agent string, though internally it does itself use the same user agent string as above, and as the documentation page says, you do need to have a browsecap.ini file - read the doc page to find out more).

$browser = get_browser(null, true);
if($browser == 'Firefox') {    
    //user runs firefox
}

However, bear in mind that end users can change the user agent string in their browser config. This is used quite widely, especially in cases where a site tries to block a particular browser.

So whatever information you do get out of the user agent string, you can never be certain it's going to be accurate. It may be Firefox pretending to be IE or Chrome pretending to be Firefox, or any other combination.

They can even block the user agent string entirely (I know of at least one web security product that does this by default without the user even knowing about it).

There are also more browsers out there than you'd think. Beyond the few well known ones, most of the others use the rendering engines from the main ones. For example, Flock and Iceweasel use the Firefox engine, so their pages work exactly like Firefox, but they may not be presented as Firefox in the user agent string. There are also a whole raft of browsers that use the IE rendering engine in a similar way.

So the short answer is to read the user agent string, but the long answer is don't rely on it being accurate.


if(stristr($_SERVER['HTTP_USER_AGENT'],"Firefox") !== false){
    //Uses firefox
}else{
    //Does nto use firefox
}


use

$_SERVER[‘HTTP_USER_AGENT’]

like

if ( strpos($_SERVER['HTTP_USER_AGENT'], 'Firefox')

you can see example here

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜