How can I change php.ini by PHP? - 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.

Related

PHP putenv() not updating

On my apache instance it is setting an env variable APP_ENV=development. I am trying to change this dynamically on my PHP side (in the instance of firing up test suite) like so:
putenv('APP_ENV=testing')
var_dump(getenv('APP_ENV')); // still returns development
I have tried:
Starting php in safe mode in php.ini
Setting safe_mode_allowed_env_vars = PHP_ APP_ in php.ini
Update:
I am using PHP version 5.4.16 and notice that safe mode has been deprecated. I'm not sure if this means putenv will even work for overwriting or even unsetting existing envs?
You are using an Apache variable, so, you should use apache_setenv() and apache_getenv()
apache_setenv('APP_ENV', 'testing');
To recover it use:
apache_getenv('APP_ENV');
The docs say the list needs to be comma delimited.
try PHP_,APP_

Changing PHP configuration dynamically

I have multiple system running on PHP 5.3 that are 15-10 years old, I can't go on those and change anything.
Problem is: some of those system are using PHP register_globals = on that is depreciate on PHP 5.3 and doesn't even exist in PHP 5.4 (http://php.net/manual/en/security.globals.php).
I am developing a new system and would like to turn PHP register_globals = off But I can't because all those old system that NEEDS it.
So I thought about dynamically changing the register_globals to off in my script using string ini_set ( string $varname , string $newvalue ) but the documentation (http://php.net/manual/en/function.ini-set.php) baffles me a bit:
Sets the value of the given configuration option. The configuration option will keep this new value during the script's execution, and will be restored at the script's ending.
Does that mean that if I have two script running at the same time lets say... One of my old system that really needs register_globals = on and my new system that use ini_set() to register_globals = off that the old script will be running with the newly changed setting? Or will it keep the setting in my PHP.ini file and the new system will run on the ini_set() configuration?
--EDIT--
After some test (see code below) #Jacob was right and it seems PHP create a context for each script and that ini_set() only change the configuration of the script its in.
TestWithIni_Set.php
<?php
ini_set('register_globals', '0');
$i = 0;
while(true)
{
if($i == 0)
{
phpinfo();
}
$i++;
}
?>
TestWithoutIni_Set.php
<?php
phpinfo();
?>
So I ran TestWithIni_Set.php first, then will TestWithIni_Set.php was executing (the infinite while loop) I ran TestWithoutIni_Set.php.
Unfortunatly, it seams like I can't change register_globals value with ini_set(). I tried the following:
//Knowing that ini_set() parameters are strings I tried those anyway.
ini_set('register_globals', '0');
ini_set('register_globals', 0);
ini_set('register_globals', 'off');
ini_set('register_globals', 'Off');
ini_set('register_globals', false);
ini_set('register_globals', 'false');
Then to make sure I didn't had something wrong in my code I tried:
ini_set('log_errors', '0');
Just to see if it would work and it did. Then value of log_errors for the script with ini_set() was off and the value of log_errors for the script without ini_set() was on.
But now I have a different problem.
How can I change the value of register_globals for only the running script if I can't change it using ini_set()?
I added a .user.ini file to my new system dir with the only line being register_globals=off.
I also went and uncomment the following line in my PHP.ini file:
user_ini.filename = ".user.ini"
I also made sure only a local user had the rights to make changes to that dir. So somebody couldn't go and upload a new user define .ini file for my server within this dir.
For exemple: IIS user can only read the file in this dir and can't add/modify/remove any file.

How do I find out the php start flags <? or <?php

Is it possible to find out with php if the short start-flag <? does suffice in a script?
is there a ini-variable or do I need to program some function using output-buffering to see the results?
short_open_tag in php.ini turns <? on/off. However, <?=...?> always works in recent PHP versions.
You can retrieve its value using ini_get() - but you cannot change it using ini_set. You can set it using a .htaccess containing php_flag short_open_tag on though.
So you should never use <? for PHP blocks but <?php. For expressions <?= is fine if you don't need to support ancient PHP versions.
Yes, there is an ini variable, and it is called short_open_tags. So simply put ini_get with sort_open_tags as the parameter should return true if short tags is available on the server:
ini_get('short_open_tag')
I did some double check now:
<?php
check_configuration(); // on a blank linux install check for some config flags
function check_configuration(){
$test_short_open_tag=false;
?><? $test_short_open_tag=true; ?><?php
if(!ini_get('short_open_tag') or !$test_short_open_tag){
die( '<br>ERROR: please allow \'short_open_tag\' in php.ini or .htaccess to allow the use of "<?"<br><br>');
}
}

PHP - How Detect if Output Buffering is Enabled

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())
{
}

Why is "<?" no longer working and instead only "<?php" works?

I was using xampp to develop locally and then I installed the PHP from the direct installer. Now in some of my PHP code, only PHP code that starts with "<?php" is correctly parsed. Anything that starts with "<?" or "<?=" is completely ignored and just left as is.
How can I adjust the configuration to parse either tokens?
This is a php.ini setting named
short_open_tag = 1 # (enabled)
I recommend you to disable short_open_tag and only work with <?php. When short_open_tag is enabled, it can collide with the XML processing instruction <?xml as both the PHP open tag and XML PI start with a <?.
By using only <? as start preprocessor startup, you can get the preprocessor confused with well formed XML documents. XML stands <? for processing-instruction, imagine an XHTML document with embeded XML that requires XSLT processing... The preprocessor will get confused with the stylesheet processing instruction and will throw an error.
It's higly recomended to use the <?php processor starting tag, try using the
short_open_tag = Off in your php.ini. Also, you can try using <?php ini_set('short_open_tag', 'On'); > if you are getting problems.
You can set short_open_tag = On in the php.ini
It's a configuration option, more information on: http://www.php.net/ini.core (look for short_open_tag).
For the newer version:
short_open_tag = On

Categories