get_browser() returns FALSE
Asked Answered
A

3

11

- Running PHP 5.3.8 on Linux-

To start, we have solved this issue to the point where the function returns the expected values. However there are still a lot of unanswered questions I've got, and the "solution" is more of a hack-around than anything.

I've spent the better part of a day on this issue, so bear with me as I explain what was done. To start, the issue is that the return value of get_browser() is FALSE, which is not a documented return value. This leads me to assume that FALSE being returned means some sort of error state within the function.

The test code, after many iterations, became just a simple var_dump(get_browser($agent, true)). I ran tests with both passing the user agent string directly, as well as passing no arguments, e.g. var_dump(get_browser()), that all had the same return values.

What was tried/verified, with no change in return value:

browscap.ini:

  • Have the latest version, also tested a few previous versions

Permissions:

  • bowscap.ini - Initial permissions were 644, but I have tried everything from 644-777

  • Directory containing browscap.ini - Initial permissions were 755, tried 777 as well

  • Verified that PHP can access the file and directory with other functions like file()

User Agent

  • Tried passing a manual user agent string

  • Tried passing $_SERVER['HTTP_USER_AGENT']

  • Verified my user agent string with a friend in a far away land - get_browser() returned values as expected.

php.ini

  • The browscap setting points to the correct location

  • verified again with echo count(file(ini_get('browscap')));

Error Logs

  • Checked PHP & Apache error logs for any mention of 'browscap' or anything even closely related - nothing out of the ordinary.

File Structure

This is where I suspect that the error comes from. browscap.ini lives in /var/php/, which has suitable permissions as noted above. My thought was that maybe PHP couldn't access this directory, or something along those lines. However, this directory is also where sessions are stored, so that becomes less likely.

THE "SOLUTION"

What solved the issue was moving browscap.ini to the public web directory. I'm curious as to why this is the case, especially given the undocumented return value. The "solution" works, but is not the solution I thought I would find...

Does get_browser() have special permissions requirements, or anything like that? file() could access the directory and file just fine, but get_browser() could not (presumably). I've practically pulled my hair out over this issue and would love some resolution!

Thanks for reading!

Amourpropre answered 8/5, 2012 at 23:13 Comment(5)
What are you using this feature for? The user-agent header and recognizing it isn't exactly reliable. You would probably have better luck testing the browser's capabilities with javascript and sending this info to the server. This however shouldn't be necessary either, since you would typically handle variances 100% client side.Kalgoorlie
@Kalgoorlie This is for maintenance on a (fairly large) existing codebase. The values from this call are used all over the place in both preprocessing and serving content. I'm always open to new/better ways of doing things, but it would probably be too much work to change the whole system in its current state.Amourpropre
I'm curious as to What the value of browsecap was in phi.ini. Was it the full path /var/php/browsecap.ini or relative browsecap.ini? or were you changing it throughout your testing?Chamaeleon
@Chamaeleon It was the full path, never changed, and I verified the path's accuracy many times, usually by the code: echo count(file(ini_get('browscap')));, which returned the expected result. This means that file() could open and read the file just fine, so get_browser() should have been able to as well.Amourpropre
@Amourpropre get_browser will return false for a few reasons, one being failure to open the ini file, failed memory allocation or an error reading the file. I'm suspecting it was not opening the file properly for some reason. See line 469-470 hereChamaeleon
A
4

Actually, even not documented on the manual page, the get_browser function can return FALSE for multiple reasons.

At least a look into the underlying source code let one assume that.

I suggest you take a look in there and then let me know if you have additional questions. I might be able to answer them then.

Adamite answered 14/5, 2012 at 13:32 Comment(1)
I've looked at the various reasons that it could return false, but none of them make any sense, given the environment. My best guess for the error is some problem with reading/opening the file, and my specific questions are relating to this theory, e.g. if file() can access the file, why can't get_browser()? PHP has no problem opening, reading, and writing to this file with many functions, except get_browser(), which fails for an unknown reason.Amourpropre
A
7

You have tried around every required method.

http://php.net/manual/en/function.get-browser.php having 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.


What solved the issue was moving browscap.ini to the public web directory.

It is may be pointing to that location. i.e. public web directory

Does get_browser() have special permissions requirements, or anything like that?

Read permissions are required only.

Alixaliza answered 11/5, 2012 at 7:5 Comment(2)
The browscap.ini that was used was the latest version available. I also tried multiple previous versions that we had in our filesystem. As for the permissions, PHP had read permissions on the file and directory, and we even went so far as to try 777 permissions on both.Amourpropre
I mean to say there is browscap setting in php.ini which is actually path of that file. Check that which directory it is pointing.Alixaliza
A
4

Actually, even not documented on the manual page, the get_browser function can return FALSE for multiple reasons.

At least a look into the underlying source code let one assume that.

I suggest you take a look in there and then let me know if you have additional questions. I might be able to answer them then.

Adamite answered 14/5, 2012 at 13:32 Comment(1)
I've looked at the various reasons that it could return false, but none of them make any sense, given the environment. My best guess for the error is some problem with reading/opening the file, and my specific questions are relating to this theory, e.g. if file() can access the file, why can't get_browser()? PHP has no problem opening, reading, and writing to this file with many functions, except get_browser(), which fails for an unknown reason.Amourpropre
C
4

I had the exact same issue as the original poster. The solution? php.ini required an absolute path to the browscap.ini file.

So, even though PHP found the file and it appeared in phpinfo()'s output, the following line was the problem:

browscap = browscap.ini

With that line, getBrowser() returned false.

However, changing it to an absolute path worked, like so:

browscap = /etc/browscap.ini

Hope this helps someone! It's a strange one...

Curious answered 6/9, 2012 at 8:51 Comment(1)
Interestingly this wasn't the cause of the issue I was experiencing - I originally had a relative path, but changed to absolute in my debugging. Though looking at the relevant source code shows a few places where FALSE gets returned.Amourpropre

© 2022 - 2024 — McMap. All rights reserved.