我得到了一个名为variables.php 的文件
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'password';
$dbname = 'database_name';
?>
我知道我可以使用include
访问这些变量,然后像使用任何其他变量一样使用它们,但我想知道,我如何更改这些变量的值。我不是说$dbhost = "other_value";
。我的意思是,我怎样才能将新值保存到那个文件中。我读到了关于php和文件打开/写入/关闭的文章,但我找不到一种方法,如何放置自己并替换我想要的值。
我所拥有的唯一信息是变量的名称,以及现在保存到它的值。我不确定我能在哪一行找到那个变量——如果它改变了什么的话。因此,问题是,如何将(使用php)变量dbhost
更改为其他任何变量,用户将在表单中输入。(表单部分不是问题,假设用户值已保存到$_POST['uservalue']
)
提前谢谢。
注意2013年4月添加:
这是个坏主意您不应该像这样修改配置文件。如果您有可以由用户管理的配置,那么您应该将其存储在数据库中,或者在最坏的情况下,以通用文件格式(ini、XML等)存储。存储数据库连接详细信息的配置文件永远不应该由应用程序修改,只能由管理员手动修改,因为文件的安全性很重要,而且这种事件应该非常罕见。
对动态修改的文件调用include
/require
会带来麻烦。
下方的原始答案
function change_config_file_settings ($filePath, $newSettings) {
// Get a list of the variables in the scope before including the file
$old = get_defined_vars();
// Include the config file and get it's values
include($filePath);
// Get a list of the variables in the scope after including the file
$new = get_defined_vars();
// Find the difference - after this, $fileSettings contains only the variables
// declared in the file
$fileSettings = array_diff($new, $old);
// Update $fileSettings with any new values
$fileSettings = array_merge($fileSettings, $newSettings);
// Build the new file as a string
$newFileStr = "<?php'n'n";
foreach ($fileSettings as $name => $val) {
// Using var_export() allows you to set complex values such as arrays and also
// ensures types will be correct
$newFileStr .= "'${$name} = " . var_export($val, true) . ";'n";
}
// Closing ?> tag intentionally omitted, you can add one if you want
// Write it back to the file
file_put_contents($filePath, $newFileStr);
}
// Example usage:
// This will update $dbuser and $dbpass but leave everything else untouched
$newSettings = array(
'dbuser' => 'someuser',
'dbpass' => 'newpass',
);
change_config_file_settings('variables.php', $newSettings);
例如,您有settings.php
,并且已经设置了$your_variable='hi Mike';
,您希望将其更改为'hi Joshua'
:
<?php
$file='settings.php';
//read file
$content=file_get_contents($file);
// here goes your update
$content = preg_replace('/'$your_variable='"(.*?)'";/', '$your_variable="hi Joshua";', $content);
//write file
file_put_contents($file);
?>
p.s.1)小心!我不知道你为什么要用那种方法。你在做一件非常危险的事!我还没见过这样的代码。你很容易被黑客入侵,因为有人可能会在PHP文件中写一个变量,它会像函数一样执行,并获得整个网站。
p.s.2)在保存之前不要使用.php扩展名,而是使用其他扩展名(即".txt")和FILTER值!
p.s.3)使用MYSQL数据库,而不是写入文件。是的,这需要多花30分钟,但你会得到更多的安全。
也许您应该考虑将配置文件放在.ini文件中,该文件看起来如下:
dbhost = "localhost"
dbuser = "root"
dbpass = "password"
dbname = "database_name"
然后使用读取文件
$config = parse_ini_file( "/the/path/to/file.ini" );
然后您访问以下值:
connect_to_db( $config[ 'dbhost' ], $config[ 'dbuser' ] );
然后,您可以直接更改值,如:
$config[ 'dbhost' ] = 'new host';
然后你写这样的文件:
$f = fopen( "/the/path/to/file.ini", "w" );
foreach ( $config as $name => $value )
{
fwrite( $f, "$name = '"$value'"'n" );
}
fclose( $f );
请注意,如果值可能包含双引号,那么在这样写之前,您需要在循环中转义$value
:
$value = str_replace( '"', ''"', $value );
此外,请记住将.ini文件放在htdocs
目录或任何其他可通过网络访问的目录之外,因为人们可以下载该文件并查看其内容。。。
我找不到任何理由更改这些值。
我能想到的唯一合理的用例是从头开始创建这样的文件。
因此,您可以使用var_export()
使正确转义的值准备好写入文件
我成功了:
$file = 'file.php';
$content = file_get_contents($file);
$replacement = 'replace';
$content = preg_replace('/find/', $replacement, $content);
file_put_contents($file, $content);