Skip to content

Instantly share code, notes, and snippets.

@2braincells2go
Forked from mainakibui/config.php
Created August 4, 2016 04:17
Show Gist options
  • Save 2braincells2go/616e5893cc8763d945c0d5513b7aea25 to your computer and use it in GitHub Desktop.
Save 2braincells2go/616e5893cc8763d945c0d5513b7aea25 to your computer and use it in GitHub Desktop.
How to Create a Form Updatable Config File
<?php
$dbhost = "svr.kit-b.com";
$dbuname = "kit-b_user";
$dbpass = "kit-b_pass";
$dbname = "kit-b_db";
$prefix = "dbpref_";
$user_prefix = "userpref_";
$dbtype = "10002";
?>
<?PHP
require_once 'config.php';
IF (ISSET($_POST["Submit"])) {
//if data has been posted update config file
$fname = "config.php";//name/path of config file
$fhandle = fopen($fname,"r");//Open for reading only; place the file pointer at the beginning of the file.
$content = fread($fhandle,filesize($fname));//Read from file pointer onwards
//Remove unwanted elements
$find = array('<?php','?>',';',' ','"','\n');
$replace = array("");
$data = str_replace($find, $replace, $content);
//Create array
$array = preg_split("/[$=]/",$data);
array_shift($array);//remove empty first array
//use odd key value in array1 as key in array2
foreach (array_chunk($array, 2) as $pair) {
list($key, $value) = $pair;
$assoc[$key] = $value;
}
//change array values and recreate string
$string = "<?php \n";
foreach ($assoc as $key => $value):
if ($assoc[$key] != $_POST[$key]){
$string .= '$'.$key.' = "'.trim($_POST[$key]).'";'."\n";
}
else{
$string .= '$'.$key.' = "'.trim($value).'";'."\n";
}
endforeach;
$string .= "?>";
//write to file
$fhandle = fopen($fname,"w");
fwrite($fhandle,$string);
fclose($fhandle);
header ("Location: ".$_SERVER['PHP_SELF']);//reload page to get new form values
}
//header
echo "/**** <b>Author:</b> <a href='https://twitter.com/mainakibui' target='_blank'> @mainakibui </a> <br>
**** <b>Website:</b> <a href='http://kit-b.com/' target='_blank'> www.kit-b.com </a> <br>
**** <b>Title:</b> How to Create a Form Updatable Config File <br>
****/";
//header
?>
<form action="" method="post" name="edit_config" id="edit_config">
<p>
<input name="dbhost" type="text" id="dbhost" value="<?php echo $dbhost;?>">
DB Host
</p>
<p>
<input name="dbuname" type="text" id="dbuname" value="<?php echo $dbuname;?>">
DB Username
</p>
<p>
<input name="dbpass" type="password" id="dbpass" value="<?php echo $dbpass;?>">
DB Pass </p>
<p>
<input name="dbname" type="text" id="dbname" value="<?php echo $dbname;?>">
DB Name </p>
<p>
<input name="prefix" type="text" id="prefix" value="<?php echo $prefix;?>">
DB Prefix</p>
<p>
<input name="user_prefix" type="text" id="user_prefix" value="<?php echo $user_prefix;?>">
Userprefix</p>
<p>
<input name="dbtype" type="text" id="dbtype" value="<?php echo $dbtype;?>">
DB Type </p>
<p>
<input type="submit" name="Submit" value="Save">
</p>
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment