53

I'm using the get_browser() function in an attempt to warn people that their browser doesn't support Javascript. Actually I'm using it to tell them they can use certain parts of a web application I'm building. I've decided to properly use Javascript because I'm tired of listening to developers that are scared of using it and I've seen some great potential in the language. I digress, here is the error.

browscap ini directive not set

Now I'd imagine this means I need somthing set it the php.ini. Can someone give me some direction on this one?

Gordon
  • 312,688
  • 75
  • 539
  • 559
Robert Hurst
  • 8,902
  • 5
  • 42
  • 66
  • 1
    The browscap feature will tell you if the browser supports Javascript, but not if they disabled it. You need to tell them another way. – Fabien Ménager Jan 10 '10 at 12:20
  • This may be the solution you are looking for : http://stackoverflow.com/questions/121203/how-to-detect-if-javascript-is-disabled – Fabien Ménager Jan 10 '10 at 12:21
  • Oh man... people **disable** javascript (even with extensions like "NoScript"). It's not about browser support, and besides, using Javascript for core features will mean many users with accessibility issues won't be able to use your website. DO NOT rely on user agent sniffing and also read on progressive enhancement. – Camilo Martin Dec 26 '13 at 21:37

4 Answers4

81

I don't think this is the "best" solution to detect is a browser supports what you need for your website :

  • first of all, browsers can lie -- they can send whatever thay want as User-Agent
    • And even if a given version of a support should support what you need, Javascript can still be disabled.
  • second, there are more "proper" way to detect what a browser can do or not, when it comes to Javascript.

For the second point, you should test if the browser actually supports what you need -- and not rely on a list such as the browscap one.


Still, to answer your question about browscap : there is a note at the bottom of the manual page for get_browser that says (quoting) :

Note: In order for this to work, your browscap configuration setting in php.ini must point to the correct location of the browscap.ini file on your system. browscap.ini is not bundled with PHP, but you may find an up-to-date » php_browscap.ini file here.
While browscap.ini contains information on many browsers, it relies on user updates to keep the database current. The format of the file is fairly self-explanatory.

So, you have to :

  • download the browscap file,
  • and set the browscap directive in your php.ini file, so it points to the file you downloaded.
Zuul
  • 16,217
  • 6
  • 61
  • 88
Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
  • 2
    And if i cannot access my php.ini file, for example I'm on a webhost? How can i set browsercap location using ini_set()? – MakuraYami Jun 05 '12 at 10:05
  • 3
    browscap is flagged as PHP_INI_SYSTEM so it cannot be changed using ini_set or through htaccess. – Beniston Jun 08 '12 at 10:59
  • Can you please explain the best method to detect browsers? Any pointer is appreciated . . . – palerdot Sep 26 '12 at 14:06
  • To use Browscap in a situation where you don't have access to php.ini, you can use [phpbrowscap](https://github.com/GaretJax/phpbrowscap). Note that the browscap project is currently closed, but is being resurrected. – Synchro Nov 16 '12 at 08:11
  • @arundex It's more reliable to use feature detection than it is to check what browser it is. http://stackoverflow.com/questions/1294586/browser-detection-versus-feature-detection – Kevin Beal Jan 08 '13 at 17:28
  • 1
    My info.php show that all ok (directive browscap and extra/browscap.ini), but my PHP script that calls *get_browser()* function say "Warning: get_browser(): browscap ini directive not set". – Peter Krauss Feb 13 '18 at 23:40
21

I know this topic is old, but you can use something like:

<noscript>JavaScript must be enabled</noscript>

to display if JavaScript is not enabled.

Jeremy Harris
  • 211
  • 2
  • 3
  • This is helpful. Also, if anyone is new to this like me, the – 2147483647 May 07 '20 at 17:11
4

You can check the browser by using Browser class download it from Github

Configuration

      include(/your-path/Browser.php);
$browser = new Browser();
    if( $browser->getBrowser() == Browser::BROWSER_IE && $browser->getVersion() >= 8 ) 
        {
            echo "Your browser is Internet explorer version 8";                                                                                                                                    
    }
Manju
  • 747
  • 4
  • 10
  • 21
3

There is a bug in PHP that can also result in this error when the real problem is that the web server can't read the browscap file (e.g., if it's owned by root instead of www-data). See https://bugs.php.net/bug.php?id=74501

David Patterson
  • 1,780
  • 3
  • 17
  • 40
  • you're right! and sadly there's still no fix for this issue. I'm running Apache/MySQL/PHP under Windows (WAMP Server) but unfortunately all the efforts I've done in order to let the ini file be accessible didn't worked... – Power Engineering May 26 '20 at 12:44