2012-03-02 130 views
3

我有一个名为variables.php更改变量的值保存到file.php

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

我知道我可以使用include,然后使用它们,因为我用其他变量访问这些变量,但我想知道,我如何更改这些变量值。我不是指$dbhost = "other_value";。我的意思是如何将新值保存到该文件。我正在阅读有关PHP和文件打开/写入/关闭,但我无法找到一种方法,如何放置自己,并取代我想要的价值。

我唯一的信息是变量的名称和现在保存的值。我不知道在哪一行我可以找到该变量 - 如果这改变了任何东西。所以问题是,如何改变(使用PHP)变量dbhost到其他任何东西,该用户将形式。 (形式部分不是问题,可以说用户值已保存到$_POST['uservalue']

在此先感谢。

+1

这个论坛可以帮助你http://forums.devshed.com/php-development-5/change-variable-in-config-file-using-php- 53166.html – Rikesh 2012-03-02 12:34:18

回答

3

也许你应该考虑把你的配置文件中的一个.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目录或可通过网络访问的任何其他目录之外,因为人们将能够下载该文件看看它的内容...

6

注意新增04/2013:

这是一个坏主意。你不应该像这样修改配置文件。如果你有可以由用户管理的配置,你应该将它存储在数据库中,或者在最坏的情况下以通用文件格式(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); 
2

原来的答复,我找不到任何理由变化这些值。

我能想到的唯一理智的用例是创建这样的文件从头开始。

所以,你可以使用var_export()有正确转义值准备好被写入文件

+0

嗯,我想要一个文件模板,当用户第一次尝试运行这个“Web应用程序”时,它会问他这些变量。所以他不必乱用编辑php文件。类似joomla instalator〜的东西。那么,从头开始创建它也可能是个好主意。 – Kedor 2012-03-02 12:55:21

+0

这应该是被接受的答案。我的情况太糟糕了。 – DaveRandom 2013-04-19 08:17:53

4

例如,你有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); 
?> 

PS1)要小心! ! !我不知道你为什么要这样做。你做的事很危险!我没有看到任何类似的编码。你很容易被黑客入侵,因为有人可能会在PHP文件中编写一个变量,它会像一个函数一样执行并获取你的整个站点。

p.s.2)不使用.php扩展名,而是使用其他内容(即'.txt')和保存前的FILTER值!

p.s.3)使用MYSQL数据库,而不是写入文件。是的,这需要花费30分钟以上,但你得到了很多安全。

1

我成功通过此:

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