Walrus
Walrus

Reputation: 20464

PHP PDO write configuration file

User interface allows for easy set up of a CMS. The user enters the host, database, username and password for their database the first time they activate the site in a web form.

I want to then write that information in the config file replacing the PHP variables that currently contain 'temp' with the users values.

<?php
$host = 'temp';
$database = 'temp';
$username = 'temp';
$password = 'temp';

try { 
    $fdbh = new PDO("mysql:host=$host;dbname=$database", $username, $password);
    $fdbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
    }
catch(PDOException $fe) { 
    include('basic/create.php');
    }

?>

So I want to $_POST the values here in their respective places and then physically write them into the document. Possible?

Upvotes: 0

Views: 340

Answers (2)

Pekka
Pekka

Reputation: 449613

I guess one standard approach would be having a template file with placeholders:

<?php
$host = '#HOST#';
$database = '#DB#';
$username = '#USER#';
$password = '#PASS#';

then loading that template using file_get_contents(), inserting the actual values using str_replace() and then writing the result into the final file using file_put_contents().

Upvotes: 1

DTukans
DTukans

Reputation: 349

you must put DB config values in another file and include them. 'basic/create.php' output form to enter config values and on form submit rewrite DB config file with user entered values

Upvotes: 0

Related Questions