Automatically save php variables without a database

In the administration area of ​​my site there is a form that is designed for the host name, username and password for the mysql database that the site uses. These values ​​are currently hardcoded into the php class. But who can I link it so that the form can edit the variables in the php class (and save the results on the server, in other words, the variables are hard-coded). Usually I have to store such things in a database, but obviously this cannot be done.

+3
source share
2 answers

Save the values ​​in the configuration file. And make sure it is not accessible from the Internet.

The simplest solution is to store the values ​​in the configuration array - the user enters the values, you generate an array from it, and then file_put_contents("config.php", "<?php $config = " . var_export($config)). With this method, whenever you need a config array, all you have to do is include config.phpand it is.

This is unverified code, for example, for purposes only. Depending on your situation, you may need to decide the conditions of the race, file_put_contentsthis is not enough. The bulk of the code above: var_exportreturns a valid php code that you can eval(if you're evil enough) or an echo to the file and include this later.

+4
source

- . script, DB . .

$fh = fopen('config.php', 'w');
fwrite($fh, chr(60) . "?php\n");
fwrite($fh, sprintf("define('DB_HOST', '%s');\n", addslashes($_POST['DB_HOST'])));
fwrite($fh, sprintf("define('DB_USER', '%s');\n", addslashes($_POST['DB_USER'])));
fwrite($fh, sprintf("define('DB_PASS', '%s');\n", addslashes($_POST['DB_PASS'])));
fclose($fh);
+5

Source: https://habr.com/ru/post/1756406/


All Articles