get_browser() returns FALSE - php

- 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!

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.

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.

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...

Related

PHP session.save_path ignored

I was having problems with my PHP website (SuiteCRM) not being able to log users in and I found it was due to not being able to write on the sessions directory.
I am able to fix it by creating the directory /tmp/php_sessions and giving it write permissions for the Apache user www-data. I see the directory get populated with files as users log in.
However, Ubuntu Xenial is deleting my entire tmp directory on reboots, so I have to redo this all over again every time. I decided to move my save_path elsewhere.
After changing things in my php.ini file, and restarting Apache, I can check that they are effective by running this simple script:
<?php
echo ini_get("session.save_path");
phpinfo();
?>
This shows me a double confirmation of the new path, first echoing /var/tmp/php_sessions and then, in the middle of all the phpinfo information, showing the same value as both Local Value and Master value for directive session.save_path.
BUT the directory that php is using is still the first one, /tmp/php_sessions! It seems that my setting is being ignored.
Am I overlooking something? Where could that old setting be buried? Or how can I make the new one effective?
(P.S. - I am not using a redis handler as in another similar SO question)
Ok, I solved my own problem and the general answer is as follows:
There are two more things that can be changing the path and need to be checked,
the PHP code of the application might be changing the ini directive, search the code for ini_set(session.save_path
the PHP code might be using the session_save_path PHP command to override the ini. Search the code for that also (and notice the two underscores _!)
And the specific answer for my case was that SuiteCRM uses session_save_path command to set its path with a value coming from the file config.php found at the web root. That's where I found the old setting, and changing it solved my problem (for good, I hope).

Including remote PHP file functions in local PHP file

Gone through related posts and found turning allow_url_include will does the trick. However when I did this :
remote file file.php at http://www.courierscripts.com
$content = file_get_contents('http://www.courierscripts.com/folder/file.php');
on my functions.php, was not able to use the functions of file.php. I also don't want to change my file.php to file.txt because everyone can see it.
Any other way?
If the file is on the same server, use absolute or relative path to it, not an url. Otherwise:
Short answer:
No, it's not possible.
Long answer:
Actually possible with conditions but I bet you won't like them.
It's obviously impossible if you don't have access to the target server (otherwise storing passwords in php config files like Wordpress does would be just one big security flaw).
First of all, file_get_contents returns a string. So you could eval it, but eval is very bad (you can search SO for the clues why).
OK, suppose you agree to eval what's coming from that server even after considering that someone might change the code and do whatever he wants on your machine. BUT you make an http request that is handles by the server (Apache, Nginx or whatever else).
The server knows that *.php files should not be handles as static files. For example, fastcgi. You can turn that off, for example, with RemoveHandler in Apache. But that would let everyone see the source code of files you expose this way.
So, after removing handlers and evaling the result, you could get the result. But be ready that someone you work with will punch you in the face for doing that ;)
UPD
For code sharing, use Composer to create a package and use it as a dependency.

Get file contents in Joomla

I'm trying to get a file's content into a variable but I don't get anything when I read the file. I used both methods JFile::read() and file_get_contents() but both return the same thing: a blank string, not an error , not any boolean values or anything.
I want to mention that I'm working on a Linux machine (just for 2 days) and recently I changed the permissions for the entire machine to 777 ( I don't know if this affects something or not).
Is the a connection between my OS, permissions and the php's file_get_contents()? or Joomla restricts file reading?
Also I want to mention that my file_get_contents() function was added manually by me in the index.php file , also the file I want to read was manually added in the same folder with index.php.
We had the same problem with one of our clients, it turned out it was a firewall issue. It was very hard to debug this issue. I suggest you check with your networking team.
I am assuming, of course, that you have set the PHP error reporting level to the maximum and that you have checked the error logs.
It seemed the problem was from my system . I messed it up when I changed the permissions so I have to re-install it.
Never change the permissions on a Linux for the entire file system.

wildcard not working to change directory

i cant use * or ? for change directory
chdir ("c:\win*\system*");
how it work, i cant guess full name of windows whats that, for example WIN or winxp or win7.
or how to guess whats full name of that
chdir() to a random wildcard directory supposed to be chosen by the system for you in case of collissions? Really?!
Paths are explicit. It's here or there... not somewhere, just look it up for me. And why try to access system directory from PHP in the first place? Got permissions setup right? Or you want to make modifications to users that use your script and can't guess the right path yourself?
Imagine that this guy's chdir() has to list paths in multiple
folders everytime to do one simple operation. Let's talk security,
performance... but most of all... common sense.
The things I see on SO...
PS: I -1'ed you. This question should not be asked by a developer with minimal understanding of... development stuff. Want to inject stuff into system dir, don't use PHP.
You are likely misunderstanding that chdir is equal to windows chdir (cd) in the windows operating system. But it is not.
However that should not pose you any problems because PHP and the shell can work together easily:
chdir(rtrim(`cd c:\win*\system* & cd`));
echo getcwd();
Example output:
C:\WINDOWS\system
If you know what command is for what, you can easily solve the issue.
Stumbled upon this:
CHDIR allows wildcards as path names, and changes to the first directory found whose name matches the wildcard specified. This is convenient with long directory names, as it is only necessary to type enough of the path to uniquely identify the directory, and then add a trailing "*" to the path name.
http://www.rcramer.com/tech/windows/cmd/ad.html#CHDIR
It is not PHP though...

file uploading problem with open_basedir and move_uploaded_file

I'm working with a legacy php script. There is a file upload form in it already, but I've added another one for a different section in the same script.
The $_file['file'] is there. But I keep getting a open_basedir restriction error, and if not that, I get a permission error.
I wouldn't mind working around these problems, but the thing is, the other form works fine. There are no differences in where the scripts are (same dir), nor are there any problems in where the files are going (I tried different dir, and same dir).
I'm going absolutely bat crazy trying to figure this out. Why is my file form not working? Why does it work for the other code? What is going on?
I've had similar issues which turned out to be to do with incorrect setting of the /tmp directory by the hosting provider (i.e. where PHP stores the temporary upload file). Might be worth a call to tech support.
Doesn't it have to be $_FILE['file'] instead of $_file['file']?
safe mode forces move_uploaded_file() to check whether the files or directories being operated upon have the same UID (owner) as the script that is being executed.
I would also recommend looking for ini_set() statements in the working code.
i dont know if you suffer from the same problem, but some time ago we had a problem with open_basedir and smarty. we were receiving problem from smarty code, but the problem was bad include_path (we were setting it through ini_set but without concatenating it with old include_path). in the result smarty tried to check if directory was readable creating possible path from root directory / which violated open_basedir. to sum up, check where do you get open_basedir error, what is the path that causes the problem.

Categories