This is a very simple and basic code that writes config $values to a PHP file via form.
Please inspect and get a professional opinion of code before using on a internet facing server as this was written on a localhost server with no internet.
This simple HTML (could have .php extension) for the form looks like this:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Config Update Form</title>
</head>
<body>
<form action="process.php" method="post" name="edit_config" id="edit_config">
<div class="container-fluid">
<div class="row">
<div class='col-md-3 form-group'>
<label class="control-label" for="uploadcare">Uploadcare API Key:</label>
<input name="uploadcare" type="text" required pattern="[a-zA-Z0-9-_@:/*$%&?!., ]{3,}" id="uploadcare" value="<?php echo $uploadcare;?>">
Enter your Uploadcare API key <small>(host images in cloud for <a href="https://qiksoft.com">free</a>)</small>
</div>
<div class='col-md-3 form-group'>
<label class="control-label" for="uploadcare2">Another Uploadcare API Key:</label>
<input name="uploadcare2" type="text" required pattern="[a-zA-Z0-9-_@:/*$%&?!., ]{3,}" id="uploadcare2" value="<?php echo $uploadcare2;?>">
Enter another Uploadcare API key <small>(host images in cloud for <a href="https://qiksoft.com">free</a>)</small>
</div>
</div>
<div class="row">
<div class='col-sm-6'>
<input type="submit" name="Submit" value="Save">
</div>
</div>
</div>
</form>
</body>
</html>
The process.php
is:
<?php
// Include the config.php file to get the current values
require_once 'config.php';
// Function to sanitize and validate inputs
function sanitize($input) {
$input = trim($input);
$input = stripslashes($input);
$input = htmlspecialchars($input);
return $input;
}
if (isset($_POST["Submit"])) {
$uploadcare = sanitize($_POST["uploadcare"]);
$uploadcare2 = sanitize($_POST["uploadcare2"]);
// Function to safely write to a file
function writeConfig($key, $value) {
$configFile = 'config.php';
$configData = file_get_contents($configFile);
$configData = preg_replace("/(\$" . $key . " = \")(.*?)(\";)/", "$1" . $value . "$3", $configData);
file_put_contents($configFile, $configData);
}
writeConfig('uploadcare', $uploadcare);
writeConfig('uploadcare2', $uploadcare2);
header("Location: index.php");
exit();
}
?>
Your config.php
should look like this:
<?php
// Configuration settings for the form
$uploadcare = "your_uploadcare_api_key_here";
$uploadcare2 = "your_second_uploadcare_api_key_here";
?>