在 PHP 中安全更新基于文件的设置变量
Safely update a file-based setting variable in PHP
对于我的 PHP 网络应用程序,我有一个 pre/post 安装设置文件,比如 settings-config.php
。
从这个post,我们可以使用PHP搜索和替换文件中的确切内容:Find and replace in a file
因此,我们可以替换一个精确的字符串。说...
'My foo' --becomes--> 'My bar'
...但那是为了匹配精确的字符。
但是,对于设置,网络管理员可能在某处输入了额外的 space,等等。
我的情况
我有一个设置文件。该设置允许安装该应用程序。完成后,我需要将其设置为 false
.
| 设置-config.php:
$allowinstall = true; // change to 'true' to allow install
基于that Answer (above),我创建了这个:
| 搜索替换脚本 :
$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);
但是,由于人为因素,文件可能会有所不同
| 设置-config.php*:(变体)
$allowinstall = true;
$allowinstall = true;
$allowinstall = true;
$allowinstall = true ;
$allowinstall = true ;
ET_CETERA;
...或者如果不可能发生(在编程中经常发生),它可能是...
$allowinstall = truth;
$allowinstall = tru;
$allowinstall = truee;
$allowinstall = ture ;
$allowinstall = utre ;
ET_CETERA;
所以,简单的搜索替换脚本(上面)不适合这个。
我需要的
我希望以 $allowinstall =
开头的每一行都变成这样:
$allowinstall = false;
- 搜索替换脚本可能应该使用
^
和正则表达式来确保 $allowinstall =
是行内容的开头,因此注释不会匹配(即 //$allowinstall =
不会改变)
- 我想匹配每一个实例,所以如果恰好设置了不止一次,所有的变量都会设置为
$allowinstall = false;
- 像这样:
| 搜索替换脚本 :
$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);
我不知道如何安全地编写该代码,但我认为最好是可以。无论哪种方式...
对于这种情况,使用 PHP update/reset .php 文件中的特定设置的“正确”搜索替换脚本是什么?
要使用 var_export
执行此操作,您从一个简单的 PHP 文件开始,其中只包含数组形式的数据,如下所示:
<?php
$config = [
'foo' => 123,
'bar' => 'abc'
];
您可以在需要的地方包含该文件,然后您可以使用 $config 变量来读取所需的值。
然后你操纵数组的内容,f.e。 $config['foo'] = 'xyz';
。如果你现在做一个var_export($config);
,这会让你
array (
'foo' => 'xyz',
'bar' => 'abc',
)
这是“旧的”数组语法,但它们可以互换,所以这并不重要。仍然缺少的是 <?php
标记,这个数组实际分配给一个变量,以及它后面的尾随 ;
- 所以这些需要手动添加。
$new = '<?php $config = ' . var_export($config, true) . ';';
var_export第二个参数设置为true,因为我们希望它是return的值,而不是直接输出。这给你
<?php $config = array (
'foo' => 'xyz',
'bar' => 'abc',
);
- 现在是完全有效的 PHP 语法,可以写入文件 as-is.
对于我的 PHP 网络应用程序,我有一个 pre/post 安装设置文件,比如 settings-config.php
。
从这个post,我们可以使用PHP搜索和替换文件中的确切内容:Find and replace in a file
因此,我们可以替换一个精确的字符串。说...
'My foo' --becomes--> 'My bar'
...但那是为了匹配精确的字符。
但是,对于设置,网络管理员可能在某处输入了额外的 space,等等。
我的情况
我有一个设置文件。该设置允许安装该应用程序。完成后,我需要将其设置为 false
.
| 设置-config.php:
$allowinstall = true; // change to 'true' to allow install
基于that Answer (above),我创建了这个:
| 搜索替换脚本 :
$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);
但是,由于人为因素,文件可能会有所不同
| 设置-config.php*:(变体)
$allowinstall = true;
$allowinstall = true;
$allowinstall = true;
$allowinstall = true ;
$allowinstall = true ;
ET_CETERA;
...或者如果不可能发生(在编程中经常发生),它可能是...
$allowinstall = truth;
$allowinstall = tru;
$allowinstall = truee;
$allowinstall = ture ;
$allowinstall = utre ;
ET_CETERA;
所以,简单的搜索替换脚本(上面)不适合这个。
我需要的
我希望以 $allowinstall =
开头的每一行都变成这样:
$allowinstall = false;
- 搜索替换脚本可能应该使用
^
和正则表达式来确保$allowinstall =
是行内容的开头,因此注释不会匹配(即//$allowinstall =
不会改变) - 我想匹配每一个实例,所以如果恰好设置了不止一次,所有的变量都会设置为
$allowinstall = false;
- 像这样:
| 搜索替换脚本 :
$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);
我不知道如何安全地编写该代码,但我认为最好是可以。无论哪种方式...
对于这种情况,使用 PHP update/reset .php 文件中的特定设置的“正确”搜索替换脚本是什么?
要使用 var_export
执行此操作,您从一个简单的 PHP 文件开始,其中只包含数组形式的数据,如下所示:
<?php
$config = [
'foo' => 123,
'bar' => 'abc'
];
您可以在需要的地方包含该文件,然后您可以使用 $config 变量来读取所需的值。
然后你操纵数组的内容,f.e。 $config['foo'] = 'xyz';
。如果你现在做一个var_export($config);
,这会让你
array (
'foo' => 'xyz',
'bar' => 'abc',
)
这是“旧的”数组语法,但它们可以互换,所以这并不重要。仍然缺少的是 <?php
标记,这个数组实际分配给一个变量,以及它后面的尾随 ;
- 所以这些需要手动添加。
$new = '<?php $config = ' . var_export($config, true) . ';';
var_export第二个参数设置为true,因为我们希望它是return的值,而不是直接输出。这给你
<?php $config = array (
'foo' => 'xyz',
'bar' => 'abc',
);
- 现在是完全有效的 PHP 语法,可以写入文件 as-is.