更改保存到file.php的变量值

更改保存到file.php的变量值,php,file,Php,File,我得到了一个名为variables.php的文件 <?php $dbhost = 'localhost'; $dbuser = 'root'; $dbpass = 'password'; $dbname = 'database_name'; ?> 我知道我可以使用include访问这些变量,然后像使用任何其他变量一样使用它们,但我想知道,如何更改这些变量的值。我不是指$dbhost=“other_value”。我的意思是如何将新值保存到该文件中。我在读p

我得到了一个名为variables.php的文件

<?php
   $dbhost = 'localhost';
   $dbuser = 'root';
   $dbpass = 'password';
   $dbname = 'database_name';
?>

我知道我可以使用
include
访问这些变量,然后像使用任何其他变量一样使用它们,但我想知道,如何更改这些变量的值。我不是指
$dbhost=“other_value”。我的意思是如何将新值保存到该文件中。我在读php和文件打开/写入/关闭,但我找不到一种方法,如何放置自己并替换我想要的值

我仅有的信息是变量的名称,以及当前保存到该变量的值。我不确定我能在哪一行找到这个变量——如果它改变了什么。所以问题是,如何(使用php)将变量
dbhost
更改为用户将在表单中输入的任何其他内容。(表单部分不是问题,假设用户值已保存到
$\u POST['uservalue']


谢谢。

< P>也许你应该考虑把你的配置文件放在一个.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
目录或任何其他可通过web访问的目录之外,因为人们可以下载该文件并查看其内容…

注意于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);
函数更改\配置\文件\设置($filePath,$newSettings){
//在包含文件之前,获取作用域中变量的列表
$old=get_defined_vars();
//包括配置文件并获取其值
包括($filePath);
//包含文件后,获取作用域中变量的列表
$new=get_defined_vars();
//找出差异-在此之后,$fileSettings只包含变量
//在文件中声明
$fileSettings=array_diff($new,$old);
//使用任何新值更新$fileSettings
$fileSettings=array\u merge($fileSettings,$newSettings);
//将新文件构建为字符串

$newFileStr=“我找不到任何理由更改这些值

我能想到的唯一合理的用例是从头创建这样一个文件


因此,您可以使用
var_export()
将正确转义的值准备好写入文件

例如,您有
设置.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);
?>
我成功的原因是:

$file = 'file.php';
$content = file_get_contents($file);
$replacement = 'replace';
$content = preg_replace('/find/', $replacement, $content);
file_put_contents($file, $content);

这个论坛可能会帮助你,我想有一个模板的文件,当用户会尝试运行这个“网络应用程序”"这是第一次,它会要求他提供这些变量。这样他就不必去编辑php文件。类似于joomla instalator~的东西。好吧,从头开始创建它也可能是个好主意。这应该是公认的答案。我的答案很糟糕。为什么这是个坏主意?我见过这样做的大型开源网站。还有我指的是大的