Welcome Guest, Not a member yet? Register   Sign In
Overwriting the config.php file
#1

[eluser]Twisted1919[/eluser]
Ok, so until yesterday, i was saving my $config items into database, then in My_Controller, i was overwriting them with the database values using set_item() method but now i have much too many items, over 60 params and i don't want to query the database for the config everytime when somebody enters the site, is too much for nothing .

So, i came up with the overwriting config.php file solution , i have a custom library that connects to ftp, changes the mod of the config file to 0666, then it makes need changes using regexes, and after that, it changes the config file back to 0644 and closes the ftp connection .

So i end up having somethink like :
Code:
$file =  read_file(APPPATH.'config/config.php');
[...]
//$config values are taken from $_POST array
$config['sitename'] = 'My Site';
$config["site_title"] = 'Maybe Something Else';
[...]
//I am taking the new $config items from an array, so it's 'TRUE' instead of TRUE, you get the point ...
[...]
<?php
foreach($config AS $item=>$value)
  {
   if($value == 'TRUE' || $value == 'FALSE' || is_numeric($value))
    {
     $file = preg_replace('/(\$config\[(\'|\"){1}'.$item.'(\'|\"){1}\])(\s|\t)*=?(\s|\t)*(TRUE|FALSE|\d+)(\s)*;/ix','$config[\''.$item.'\'] = '.$value.' ;',$file);            
     }
   else
    {
     $file = preg_replace('/(\$config\[(\'|\"){1}'.$item.'(\'|\"){1}\])(\s|\t)*=?(\s|\t)*(\'|\"){0,1}.*(\'|\"){1}(\s)*;/ix','$config[\''.$item.'\'] = \''.$value.'\' ;',$file);        
    }    
}
write_file(APPPATH.'config/config.php', $file);
?>

Now , i'm not always sure of what value will have an item, by default i can wrap it in single quotes, then the client may add other single quote in his string, and could break all the config ...
Is there another way to do this , in a more clean and efficient way ?
Any help is appreciated.
Thanks.

P.S: All these items, will be overwritten from POST forms .
#2

[eluser]steelaz[/eluser]
Take a look at my post here - http://ellislab.com/forums/viewthread/131762/

You can have all your application settings in single database table and access them using $this->config->item() as you normally do.
#3

[eluser]Twisted1919[/eluser]
Looks nice, i had my own method which did something like that.
But i guess the down side of that is that you must query your database every time when the page loads, and i guess this is a bit too much, what's happening when the number of the items is too big ?
I'm just asking because of the performance, using the config file is faster than database , right ?
#4

[eluser]n0xie[/eluser]
Why are you overwriting the config file? You should either extend the config class, or think about why you need 60+ params in the first place. This looks like some major design issue...
#5

[eluser]Twisted1919[/eluser]
It's not a major design issue at all , this happens just because every little thing can be changed from backend, and when i say everything, i mean everything that you can think off, from pagination items,email addresses, email settings, comments settings, sitemap settings etc etc ...
I understand that taking this approach comes with a price, but i take my change in letting the site being fully administrable from backend.

It's just , i believed that using the config from a file instead of database, will save resources and page loading time... was i that badly wrong ?
#6

[eluser]Twisted1919[/eluser]
So, in the end, i think database option is more reliable and secure.
And, using query caching i don't have to worry about it any more.
Sorry for rushing in this problem as i did, but i was very confused .
Thank you for your answers .
#7

[eluser]steelaz[/eluser]
Number of items in solution I posted matters very little, you load all config values with one query and then you can use them throughout the code. The whole process takes about 0.05 seconds on first run and about 0.003 seconds after that.
#8

[eluser]Twisted1919[/eluser]
You are totally right Smile
Thank you for pointing to that library, it works like a charm Smile
#9

[eluser]BrianDHall[/eluser]
I did want to say though, your solution was quite novel and a pretty cool way to go about it. You might want to save the code somewhere for possible use in building an installation script or something like that sometime.

But for config you will generally find that 1 db query for a small set of data is not materially a bigger deal than reading through a configuration file.
#10

[eluser]Twisted1919[/eluser]
Thank you Brian, means allot for me, coming from a man with experience like you.(Saw your other posts)
I did save my code, actually i improved the FTP method, by destroying the quotes when the new values are added, this is just for my peace , and i offer this as the third method to manage the website configuration. And yes , your ideea for building the instalation script is very good, i'll use that too Smile




Theme © iAndrew 2016 - Forum software by © MyBB