0

I need to limit what browsers can user use to view on my page. I want to allow only Chrome, FireFox 4+ and all WebKit based browsers. If user use i.e. explorer, PHP will produce output i.e.: "You have not supported browser, use Chrome, Firefox 4+ or WebKit based browser!" How can I do it?

Koga
  • 135
  • 9
  • 1
    Or do it the proper way on the client with object support detection. – epascarello Jan 18 '13 at 17:26
  • or use search option http://stackoverflow.com/questions/2257597/reliable-user-browser-detection-with-php – fsw Jan 18 '13 at 17:28
  • 1
    Don't, if you can avoid it at all. There are more reasons against it than I can enumerate in this limited comment space (and I'm not even an expert). If you want to enable special behavior for some browsers, do feature protection (IIUC, this is what epascarello suggests). If you don't support some browsers at all and the break your website to the point of unusability, add a notice but don't hide the entire page. –  Jan 18 '13 at 17:32

4 Answers4

1

PHP sniffer is a library that handles extracting information about the user and user-agent (browser).

It uses the same data that get_browser() or $_SEREVER['HTTP_USER_AGENT'] can give you but it formats it into a nicely structured object that you can use in your code.

Mihai Stancu
  • 15,848
  • 2
  • 33
  • 51
1

Use $_SERVER\['HTTP_USER_AGENT'\] or get_browser().

But you should really ask yourself why this is necessary. If your site doesn't work with all feature, than it's ok to show a message saying:

Please upgrade your browser in order to use all features.

You can also detect whether specific JS functions/objects exists so you won't run into Undefined identifier errors (credits to epascarello).

Community
  • 1
  • 1
ComFreek
  • 29,044
  • 18
  • 104
  • 156
1

$_SERVER['HTTP_USER_AGENT'] will give you browser details, and from that you can work your way up to verification good luck

This may be so lame, since I am PHP newbie but, I would check if someone is using Mozilla (firefox) by doing this:

        $browser  = $_SERVER['HTTP_USER_AGENT'];

        if (strpos($browser,'Mozilla') !== false) {
            echo 'You are using Mozilla';
        } else {
              echo 'You are not using Mozilla';
            }
0

You can check server variable:

<?php 
 echo $_SERVER['HTTP_USER_AGENT'];
?> 
Viacheslav Kondratiuk
  • 8,493
  • 9
  • 49
  • 81