Questions about changing the php.ini

I am writing a script to change php.ini automatically through a series of replacements. The replacements are done by regex, so the script is overly complicated. (An example: I change the error_reporting line to error_reporting = E_ALL).

Two questions:

1 -
If a setting is set multiple times in php.ini, which one takes precedence, the first or the last?

display_errors = On
display_errors = Off

Which one will be used by php.ini? Or will I get an error and php won’t start?

2 - What is the best practice way to change php.ini? I am just using a shell script now, but I am hoping a better solution might exist.

Tried to edit my post, but was too late. On Question 1, the reason why I am curious is because I would like to just lump all of my changes to the bottom of the file, and leave the original setting intact, if possible.

I believe the last entry is the one used. Why not do a simple test yourself?

As far as using a script to change the php.ini Why do you want to do that? Seems like a great way for a hacker to nail you.

I moved my php.ini outside or the web root and would never automate a config file - but that’s just me.

  • At that point in the code. eg.
$orignal_value = ini_get('whatever');
ini_set('whatever', $new_value);
// do stuff while ini_set is new value
ini_set('whatever', $orignal_value);
// do stuff while ini_set is original value

I’ve had to but only very rarely.
eg. setting timezone and throwing more memory and time at a heavy script.

@Mittineague,

To one of the OP’s questions…

Does PHP care what order things are in?

For example, if you moved displayErrors = off from the top of the file to the bottom of the - and you only have ONE instance - would things still work as expected?

(Personally, I find the php.ini to be unwieldy in how it is documented and organized!!)

AFAIK PHP runs the code as it gets to it. So you could change things around. eg.

$var = $massive_memory_hogging_array;
$results = process ($var);
echo $results;
unset($var); // free up memory
// continue with the script

I’m not sure how PHP does GC, there seems to be some debate as to the “when it happens”, but in any case I’ve only done it for very long resource intensive scripts.

And yes, “ini” could be better. Some are not configurable with ini_set, some take boolean, some take Ints, some take “mixed”.

Thank goodness the documentation is there, I’d never remember all of the variations

My take is that most of the stuff in the php.ini is “set and forget”, so even if the php.ini file is no homecoming beauty, who cares?

For some things like displayErrors, maybe it would be nicer if they were in a single location.

What I would do to manage things is this…

Create “TO-DO” or “GO LIVE” section at the top and make yourself a checklist at the file top.

Being the unsophisticated programmer that I am, I have “Go Live” notes to remind myself of things I need to “switch” when going from Dev to Prod…

HTH

I guess I wasn’t very clear, sorry - I am not changing php ini settings during a php script, but rather editing the php.ini, mostly by a shell script (but also php scripts) so that the php.ini is setup exactly the way I want it in each development environment I make. I hate having to copy and paste php.inis, especially since they are different for each environment I have, so I’d rather write a script I can edit and run anytime I need to setup the php.ini in a certain per-defined way (development on ubuntu vs other distros or windows, staging, production, and so on).

The goal is to bypass any given php.ini settings and just tack on the things I want to change to the bottom of the php.ini. So it may say display_errors = Off on line 75, but on line 250 it says display_errors = On and so php counts the latter setting as what is used and ignores the former setting on line 75. I am worried this may result in errors (or not even work), so that is why I asked.

I thought php’s ini_set was a temporary function that ran only for the duration of a script, with each php script falling back onto the default and hard-coded php.ini when running for the first time. Can ini_set be used to make permanent changes to the php.ini? That would be exactly what I want if I could do that and I wouldn’t even have to worry about order of interpretation in php.ini’s settings. But the php.net manual says that ini_set “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.” Can it somehow be used to permanently alter the php.ini?

Not that I know of.

I suppose if the file were Writable there might be some way to make changes, but IMHO it would be better to just edit it.

I have my ONLINE php.ini set to maximum restrictions and when logged in as ADMIN change the settings using ini_set();

I also use a common config.php file that tests the environment and sets the following:

// config.php

define('LOCALHOST', 'localhost' === $_SERVER['SERVER_NAME'] );

if ( LOCALHOST ) {
  ini_set('error_log', APPPATH .'/php_error.php');
  error_reporting( -1);
  ini_set('display_errors', 1 );
}

Maybe try this:


defined('MODE') ?: define('MODE', 'DEFAULT'); 
switch(MODE) {
  case UBUNTU: 
     // settings
  break;

  case WINDOWS: 
     // settings
  break;

  case STAGING: 
     // settings
  break;

  default:
     // settings
}// endcase

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.