[英]Put config variables into php file
The system I'm working on has its own installation page to configure database connection. 我正在使用的系统有其自己的安装页面,用于配置数据库连接。 Basically, the user is filling some
$_POST
fields like host, dbname, password
etc.. then once he proceed further I want to store all of this variables in a file. 基本上,用户正在填充一些
$_POST
字段,例如host, dbname, password
等。然后,一旦他进一步进行操作,我想将所有这些变量存储在文件中。
The final file should goes like this: 最终文件应如下所示:
<?php
$config["db"]["host"] = "host";
$config["db"]["username"] = "user";
$config["db"]["password"] = "user_pass";
?>
But no matter what, the output is like this: 但是无论如何,输出是这样的:
<?php
$config["db"]["host"] = "";
$config["db"]["username"] = "";
$config["db"]["password"] = "";
?>
So it seems that all data from last quotes disappears. 因此,似乎所有来自最后引号的数据都消失了。 This is my code:
这是我的代码:
$file = 'core/database.core.php';
$content = "<?php \r\n";
$content .= '$config["db"]["host"] = "' . $_POST['db_host'] . '";' . "\r\n";
$content .= '$config["db"]["username"] = "' . $_POST['db_user'] . '";' . "\r\n";
$content .= '$config["db"]["password"] = "' . $_POST['db_pass'] . '";' . "\r\n";
$content .= "?>";
file_put_contents($file, $content);
Var dump of $content variable outputs only this: $ content变量的变量转储仅输出以下内容:
string(114) ""
The source code in browser show this: 浏览器中的源代码显示如下:
string(114) "<?php
$config["db"]["host"] = "127.0.0.1";
$config["db"]["username"] = "";
$config["db"]["password"] = "";
?>"
I've been trying in many ways and I have no idea how can i figure it out to work properly the way i want to achieve. 我一直在尝试多种方式,但我不知道如何才能以自己想要的方式正常工作。 Any suggestions?
有什么建议么?
Try to use var_export(); 尝试使用var_export();
$config["db"]["host"] = $_POST['db_host'];
$config["db"]["username"] = $_POST['db_user'];
$config["db"]["password"] = $_POST['db_pass'];
$content = "<?php ".PHP_EOL." \$config = ".var_export($config,true)."; ".PHP_EOL." ?>";
If that wont work, check first if all fields are set right!!! 如果那行不通,请先检查所有字段设置是否正确!!!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.