5

I Have mad an installer for my website. but I don't how to replace hostname,username, password and database. with the values which are coming from input fields. This is the code from config/database.php file.

$db['default'] = array(
    'dsn'   => '',
    'hostname' => 'db_host',
    'username' => 'db_user',
    'password' => 'db_pass',
    'database' => 'db_name',
    'dbdriver' => 'mysqli',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => (ENVIRONMENT !== 'production'),
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE
);

but this code gives me that error.

enter image description here

and this code in controller.

function configure_database() {
    // write database.php
    $data_db = file_get_contents('./application/config/database.php');
   // session_start();
    $data_db = str_replace('db_name',   $_POST['dbname'],   $data_db);
    $data_db = str_replace('db_user',   $_POST['username'], $data_db);
    $data_db = str_replace('db_pass',   $_POST['password'], $data_db);
    $data_db = str_replace('db_host',   $_POST['hostname'], $data_db);
    file_put_contents('./application/config/database.php', $data_db);
  }
0

2 Answers 2

2

You could format the config/database.php as follows :

$db['default']['username'] = "%USERNAME%";
$db['default']['password'] = "%PASSWORD%";
$db['default']['database'] = "%DATABASE%";
$db['default']['hostname'] = "%HOSTNAME%";
$db['default']['dbdriver'] = "mysqli";
$db['default']['dbprefix'] = "";
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = "";
$db['default']['char_set'] = "utf8";
$db['default']['dbcollat'] = "utf8_general_ci";

Then write to it using this method :

function configure_database() {
    // write database.php
    $data_db = file_get_contents('config/database.php');
    // session_start();
    $temporary = str_replace("%DATABASE%", $_POST['dbname'],   $data_db);
    $temporary = str_replace("%USERNAME%", $_POST['username'], $temporary);
    $temporary = str_replace("%PASSWORD%", $_POST['password'], $temporary);
    $temporary = str_replace("%HOSTNAME%", $_POST['hostname'], $temporary);
    // Write the new database.php file
    $output_path = './application/config/database.php';
    $handle = fopen($output_path,'w+');
    // Chmod the file, in case the user forgot
    @chmod($output_path,0777);
    // Verify file permissions
    if(is_writable($output_path)) {
        // Write the file
        if(fwrite($handle,$temporary)) {
            return true;
        } else {
            return false;
        }
    } else {
        return false;
    }
}

Basically it checks on every line for a match and then replace them with the posted data, and lastly write it to the database config again.

Reference : CodeIgniter Installer

Sign up to request clarification or add additional context in comments.

Comments

0

If you would like to dynamically set a config item or change an existing one, you can do so using:

$this->config->set_item('item_name', 'item_value');

Where item_name is the $config array index you want to change, and item_value is its value.

So you can try this:

 // validate the input
 if(isset($_POST['dbname']){
     $this->config->set_item('hostname',$_POST['dbname']);
 }
 ...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.