[PHP] Permanently Modifying Variables

Soldato
Joined
14 Feb 2006
Posts
4,644
Location
Surrey, UK
Evening all :)

I have a config.php file for my site which, in short, contains a few variables and nothing else. These are sitewide variables and the file is included on most pages of the site.

What I'm looking at doing is being able to modify the values of these variables in config.php though a web interface.

For example in config.php:
Code:
$c_enablesite = true;

And I would like a "Disable Site" link on a page, which would then change the value of '$c_enablesite' to 'false' in config.php.

What's the best way to go about doing this? Are there any prewritten scripts or classes that could help at all?

Thanks,
Jon
 
That setup seems kind of counter-intuitive. Sounds to me like your config file could be XML instead, in which writing to will be far easier & more logical, and more adapted to this kind of scenario. :)
 
There is a couple of things you could do:

1: If you have an existing database for your site, create a new table with the values.

Pro: Easy to access/modify
Con: Will add more DB calls

2: Instead of .php file you import, you could create an .ini file with the values. Then create a script which will read and modify it. You would also have to modify your website's code to read from the ini file when it runs.

pro: easy to set up and use.
con: having to change code.

You could also just directly modify the .php file with the values, however it is harder to parse and write, and may cause errors in the rest of your site if you make a mistake.
 
if it needs to be modifiable from a web front end, I'd go with psyreen's idea of xml.
 
Thanks for all the suggestions! The database method would be an easy one to set up, but I think I'll look into the XML way :)

Thanks again!
Jon
 
There is a couple of things you could do:

1: If you have an existing database for your site, create a new table with the values.

Pro: Easy to access/modify
Con: Will add more DB calls

2: Instead of .php file you import, you could create an .ini file with the values. Then create a script which will read and modify it. You would also have to modify your website's code to read from the ini file when it runs.

pro: easy to set up and use.
con: having to change code.

Why is "you have to parse the INI file" not a con of the .ini file method? With intelligent indexing, fetching just the options you need from the database is potentially quicker than parsing an entire .ini file.

SiriusB said:
You could also just directly modify the .php file with the values, however it is harder to parse

http://uk.php.net/include/ ?

If you mean it's harder to generate, then it's really just a case of:

Code:
foreach ( $options as $name => $value )
    $output .= 'define("' . addslashes($name) . '", "' . addslashes($value) . '");';
 
Would making the config.php file a viewable page not be better than having to remake his global variables into an xml then having to link that into to the whole site? Make it a user/pass form when you load the page, set up a user/pass in the php code so it's not viewable and use a session variable to show that are you logged in. When you are logged in the config.php file shows checkboxes to alter settings into file.
 
Back
Top Bottom