PHP - How Detect if Output Buffering is Enabled - php

Is there a simple way to detect in PHP if output_buffering is enabled in php.ini? I'd like to be able to display a message if it is not enabled.
Within my application I tried using an htaccess file to automatically enable it but it seems it does not work in all server environments and in some cases it gives a nasty error.
Thank you very much!

You can access the output_buffering value in the php.ini file by doing:
var_dump(ini_get('output_buffering'));
But I think what you are looking for is ob_get_level() (or ob_get_status()):
var_dump(ob_get_level());
Returns the level of nested output
buffering handlers or zero if output
buffering is not active.

You can check any INI setting in PHP with the ini_get method. http://php.net/ini_get
ini_get('output_buffering');
Likewise, you can change most INI settings with ini_set:
ini_set('output_buffering', 'on');

simple
check by
echo ini_get('output_buffering');
or run a file calling phpinfo(); function it will list all veriables containing values check the value for 'output_buffering ' in list.

I think you can go
if(!ob_start())
{
}

Related

disable_functions php.ini eval function still work

I got a little problem trying to disable some function in my php.
First of all, i`m not the owner of the server so I can't change the master php.ini configuration. But I tried to change it with the directive the server owner give me.
Here is the line I put in the php.ini file I created
disable_functions=eval,exec,passthru,shell_exec,system,proc_open,popen,curl_exec,curl_multi_exec,parse_ini_file,show_source
in my phpinfo() I can see in the local value and the master value that those function are disabled.
But my problem start here.
In the same file in witch i run the phpinfo() and I can confirm that the function are supposed to be disabled, I run an eval() and a shell_exec() and the eval() still work but the shel_exec() is disabled.
Why can't I disable eval()?
eval is a language construct, not a function, so it can't be disabled. See http://www.php.net/eval for more info.
You can try building https://github.com/mk-j/PHP_diseval_extension to disable eval.

Warning: ob_start() [ref.outcontrol]: output handler 'ob_gzhandler' conflicts with zlib output compression'

i am using the Gzip commpressionand Zlib commpression to speed up my website
I have used below code
ob_start("ob_gzhandler");
in common file that are include on all pages and
lib.output_compression = On
But after this i get the error like
"Warning: ob_start() [ref.outcontrol]: output handler 'ob_gzhandler' conflicts with 'zlib output compression' in E:\xampp\htdocs\projects\trunk\index.php on line 2"
Can any one suggest me what's wrong in it?
You should check if the zlib library loaded then clean the turn off output buffering by doing ob_end_clean()
You can add this line in the top of your file :
<?php if (extension_loaded('zlib')){ ob_end_clean(); ob_start('ob_gzhandler');} ?>
Search line below in your php.ini file:
zlib.output_compression = On
change for:
zlib.output_compression = Off
In your php.ini, search 'zlib' and toggle to On
Recommendation : Dont use PHP Zlib compression, Turn it OFF
but try to Turn on Output Buffering
Which helps Processed HTML are Started to Buffer immediately without waiting.. Which helps to speed up some mili secs.
Dont use too much PHP echo for normal HTML codes.
Use
Webserver Gzip compressions
Minify the HTML outputs
Use Opcache and Static cache generators to Speedup your website 100% Gain.
It's the same. You only need to do one of them, not both.
I had the same problem and your answer was very helpful.
Search line below in your php.ini file:
zlib.output_compression = On
change for:
zlib.output_compression = Off
However I could not figure out where to locate the php.ini file or the zlib.
I took a few days off and I looked over the above from a new perspective. My Hosting provider is "Hostinger" http://api.hostinger.in/redir/21246281 they use the New Control Panel and you will access PHP Configuration here:
ps for cpanel users I will give an update if needed.
Here is an example of a php configure page on the Hostinger panel:
PHP Configuration
PHP version
PHP 5.2
PHP 5.3
PHP 5.4
PHP 5.5
PHP 5.6
PHP 7.0
Choose which PHP version you would like enabled for your account.
Zlib Compression
Enabled
Disabled
Whether to transparently compress pages. If this option is set to "On" in php.ini, pages are compressed if the browser sends an "Accept-Encoding: gzip" or "deflate" header. "Content-Encoding: gzip" (respectively "deflate") and "Vary: Accept-Encoding" headers are added to the output. In runtime, it can be set only before sending any output.
Display Errors
Enabled / Disabled This determines whether errors should be printed to the screen as part of the output or if they should be hidden from the user.
Max Input Vars
Here is an example of the php configure admin panel page just make the change. Hope this was helpuful to someone.
goodloktimes#gmail.com

Using file_exists with safe_mode enabled

I am trying to check if a file exists on the server. The difficulty that I am having is that file_exists returns false for a file that I know exists on the server.
My server is configured withsafe_mode = on in php.ini. Because of this I've added the directory that contains the file I want to check on using
include_path=".:/path/to/dir"
I've also added the path to
safe_mode_include_dir = /path/to/dir
file_exists("/path/to/dir/file") still returns FALSE.
Thanks in advance
If you have the ability to do so, disable safe_mode. It has been deprecated by the PHP development team, and is gone as of PHP 5.4.0, so you will need to stop depending on it sooner or later.
If you must use safe_mode, add the directory to open_basedir, not include_path.
Sorry, but it's just not possible to circumvent safe mode restrictions - that's sort of the point of safe mode. If you want to include the file, you will still be able to do so if you have set safe_mode_include_dir to a path that will allow it to be accessed, but there is no way to get stat() related functions to work with it.
EDIT
A horrible and incredibly dangerous and unreliable work around might be this:
function file_exists_safemode ($file) {
$oldErrorLevel = error_reporting(E_ALL);
$oldDisplayErrors = ini_get('display_errors');
ini_set('display_errors', 1);
ob_start();
include $file;
$result = ob_get_clean();
ini_set('display_errors', $oldDisplayErrors);
error_reporting($oldErrorLevel);
return strpos($result, 'failed to open stream') === FALSE;
}
...but it is so nasty in so many ways, and I definitely do not recommend this as an approach.

Is there a way in PHP to output the error log defined in the apache configuration?

I created a PHP script to read the error log file in a customized visual format, but right now, I have the path to the error log file hard-coded in, which works fine for me, but I would like to find out if there's a way to pull the path to the error_log automatically so it can work on any server without further configuration.
You can use ini_get to obtain the error_log path in PHP.
$error_log = ini_get('error_log');
Otherwise, you'd be relegated to using something like:
<?php
ob_start();
phpinfo(INFO_CONFIGURATION);
$phpinfo = ob_get_contents();
ob_end_clean();
preg_match('#error_log</td><td\b[^>]*>(.*?)</td>#', $phpinfo, $matches);
$error_log = $matches[1];
Note that if there is no error_log set, $error_log will return:
<i>no value</i>
you can pull it from the ErrorLog Directive
http://httpd.apache.org/docs/current/mod/core.html#errorlog
The only way to get it in PHP is by installing something like ApacheAccessor. Wouldn't call it portable though, as I've seldomly seen it installed, but wildly guessing default paths is default distro's is about your only other option.

How can I change php.ini by PHP?

I want to do the equivalent to the following line in file php.ini, but from PHP.
short_open_tag = On
Is it possible?
I tried this:
<?php
if (!ini_get('short_open_tag')) {
ini_set('short_open_tag', 'On');
}
$a = 1;
?>
<?=$a;?>
which outputs <?=$a;?>, so it's not working.
Yes, ini_set() is what you want.
An example:
if (!ini_get('short_open_tag')) {
ini_set('short_open_tag', 'on');
}
If you are using PHP 5.3, short_open_tag is no longer an option.
Description of core php.ini directives
Short tags have been deprecated as of PHP 5.3 and may be removed in PHP 6.0.
If you want to change it during a session and forget about it later, use ini_get() and ini_set(). If you want to actually modify php.ini programmatically, you can parse the ini file using parse_ini_file(), change your options and rewrite back to disk. See here for more.
Or you can write your own string replacement routine using the normal opening of a file, preg_replace(), etc.
Although you can use ini_set, be careful (quoted from the PHP documentation):
Not all the available options can be changed using ini_set(). There is a list of all available options in the appendix.
If you are changing options, like magic_quotes and short_open_tags, that's OK. But if you are going to change safe_mode, enable_dl, etc., the function will fail silently.
Many of the options specified above as examples are obsolete/removed security options in former versions of PHP. Consult the documentation if the behavior of ini_set is unexpected (e.g., does not work)
Please edit the php.ini file (just remove the ; and restart your Apache server):
Replace
;short_open_tag = On
with
short_open_tag = On
Now it will work.

Categories