Home > Software design >  Safely update a file-based setting variable in PHP
Safely update a file-based setting variable in PHP

Time:05-20

For my PHP web app, I have a pre/post -install settings file, say settings-config.php.

From this post, we can search and replace exact content in a file using PHP: Find and replace in a file

Thus, we can replace an exact string. Say...

'My foo' --becomes--> 'My bar'

...but that is for matching exact characters.

But, for a setting, the web admin may have typed an extra space somewhere, et cetera.

My situation

I have a settings file. The setting allows the app be installed. When finished, I need to set this to false.

| settings-config.php :

$allowinstall = true; // change to 'true' to allow install

Based on that Answer (above), I created this:

| search-replace script :

$conf_file = './settings-config.php';
$conf_contents = file_get_contents($conf_file);
$conf_contents = str_replace("allowinstall = true", "allowinstall = false", $conf_contents);
file_put_contents($conf_file, $conf_contents);

But, from human factors, the file might be different

| settings-config.php* : (variants)

$allowinstall = true;
$allowinstall =  true;
$allowinstall  = true;
$allowinstall = true ;
$allowinstall  =  true ;
ET_CETERA;

...or if the impossible happens (which it often does in programming), it could possibly be...

$allowinstall = truth;
$allowinstall =  tru;
$allowinstall  = truee;
$allowinstall = ture ;
$allowinstall  =  utre ;
ET_CETERA;

So, the simple search-replace script (above) isn't good for this.

What I need

I want every line starting with $allowinstall = to become this:

$allowinstall = false;
  • The search-replace script should probably use ^ and a regex to make sure that $allowinstall = is the beginning of the content of the line, so a comment won't match (ie //$allowinstall = won't be changed)
  • I want to match every instance, so if it happens to be set more than once, all variables will be set to $allowinstall = false;
  • Something like this:

| search-replace script :

$conf_file = './settings-config.php';
$conf_contents = file_get_contents($conf_file);
$conf_contents = str_replace(
  preg_match("^allowinstall".'/any space/'."=".*),
  "allowinstall = false",
  $conf_contents
);
file_put_contents($conf_file, $conf_contents);

I don't know how to write that code safely, but I think it would be best if I could. Either way...

What is the "proper" search-replace script, for a situation like this, to update/reset a specific setting in a .php file using PHP?

CodePudding user response:

To do this using var_export, you start with a simple PHP file containing nothing but your data in form of an array, something like this:

<?php

$config = [
    'foo' => 123,
    'bar' => 'abc'
];

That file you can include where you need it, then you have your $config variable available to read the values you need.

Then you manipulate the content of your array, f.e. $config['foo'] = 'xyz';. If you do a var_export($config); now, this will get you

array (
  'foo' => 'xyz',
  'bar' => 'abc',
)

That is the "old" array syntax, but they are interchangeable, so that doesn't really matter much. What is still missing, is the <?php tag, the actual assignment of this array to a variable, and a trailing ; after it - so these need to be added manually.

$new = '<?php $config = ' . var_export($config, true) . ';';

var_export with second parameter set to true, because we want it to return the value, not output it directly. This gives you

<?php $config = array (
  'foo' => 'xyz',
  'bar' => 'abc',
);

- and that is perfectly valid PHP syntax now, and can be written to the file as-is.

  • Related