2012-03-02 63 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);