CodeIgniter Forums

Full Version: How edit config field?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi
I have field in config.php file named "token"
I need change it for always when use change token
Actually,write new data to file
Maybe?
I played around with this a little today using CI 4 here is the code, should work for CI 3.

There is no error handling or checks but it doe's work.

I' am planning on writing a config file editor later when I have the time.

PHP Code:
    public function index()
    {
        
$data = [];

        
// The path to the Config file.
        
$fileName APPPATH.'Config\App.php';

        
// Get the Config file contents.
        
$appConfig file_get_contents($fileName);

        
// The string to search for
        
$search  "public ".'$indexPage'." = 'index.php';";
        
        
// The string to replace string with.
        
$replace "public ".'$indexPage'." = '';";
        
        
// The subject is the whole file.
        
$subject $appConfig;

        
// Replace the string with new string value.
        
$ac str_replace($search$replace$subject);

        
// Write the file back out.
        
$result file_put_contents($fileName$ac);

        
/**
         * Dispaly the file in a textarea on output
         * 
         * <label>App Config</label><br>
         * <textarea rows="200" cols="143">
         *      <?php echo $config;?>
         * </textarea>
         */
        
        
$data['config'] = $ac;

        return 
view('welcome_message'$data);
    } 

At least it is something to get you started.

If you use the code make sure you add in protect and check for errors.

You will need to change the paths for CI 3.
(02-02-2020, 10:11 AM)InsiteFX Wrote: [ -> ]I played around with this a little today using CI 4 here is the code, should work for CI 3.

There is no error handling or checks but it doe's work.

I' am planning on writing a config file editor later when I have the time.

PHP Code:
public function index()
 {
 
$data = [];

 
// The path to the Config file.
 
$fileName APPPATH.'Config\App.php';

 
// Get the Config file contents.
 
$appConfig file_get_contents($fileName);

 
// The string to search for
 
$search  "public ".'$indexPage'." = 'index.php';";
 
 
// The string to replace string with.
 
$replace "public ".'$indexPage'." = '';";
 
 
// The subject is the whole file.
 
$subject $appConfig;

 
// Replace the string with new string value.
 
$ac str_replace($search$replace$subject);

 
// Write the file back out.
 
$result file_put_contents($fileName$ac);

 
/**
 * Dispaly the file in a textarea on output
 * 
 * <label>App Config</label><br>
 * <textarea rows="200" cols="143">
 *      <?php echo $config;?>
 * </textarea>
 */
 
 
$data['config'] = $ac;

 return 
view('welcome_message'$data);
 } 

At least it is something to get you started.

If you use the code make sure you add in protect and check for errors.

You will need to change the paths for CI 3.

I had guessed
Thanks