Reputation: 29
I have a file named config.php, it stores the variables that connect to a database, (Username, password etc) I want to be able to edit these settings from a html form(I have it setup)
I however cannot find out how to redefine these variables from the form, I send the form to the page updateDatabaseSettings.php but don't know how to then change them.
Your help in this would be greatly appreciated.
The code I have so far:
config.php
$DBUSER="root";
$DBPASS="";
$DBHOST="127.0.0.1";
$DBNAME="mydb";
updateDatabaseSettings.php
$newDBHOST = $_POST['dbhost'];
$newDBNAME = $_POST['dbname'];
$newDBUSER = $_POST['dbuser'];
$newDBPASS = $_POST['dbpass'];
Say the user input 192.168.1.2 for $newDBHOST I want that to replace the text in $DBHOST
Thanks
Upvotes: 0
Views: 108
Reputation: 508
you have many choices so it is hard to say but I would create two instances of DB connection - one default and second variable connection. Default connection can take configuration info from config.php and you can always connect to default db and second instance will always take configuration info directly from POST and create new connection when POST request is called.
Also you can use global variables and just rewrite them when you need or change config.php file directly with PHP (look fopen,fwrite,fclose,... functions).
Upvotes: 0
Reputation: 5766
Your updateDatabaseSettings.php
script would need to overwrite the contents of the config.php
file.
A simple approach would be to construct a string containing all of the new content (including the <?php ?>
tags and all the variable declarations). You could then pass that string to file_put_contents()
to overwrite the config.php
file.
Remember to check your file permissions though. You need to allow the webserver to write to config.php
, or it won't work.
Upvotes: 3
Reputation: 10658
I guess include config.php in updateDatabaseSettings.php and replace the values ? Or even set these variables as global ? Then I suppose you have to kill and init a new db connection. hard to answer, would need to know how your app is architectured.
Upvotes: -1