views:

429

answers:

4

Hello,

How can I import a variable from an external file? What I want to do is to have a configuration file in which I can write all my website settings and then to import these settings to every file, so I can set the website skin and things like that..

How can I do this? Thanks!

+6  A: 

Look at this :

http://php.net/manual/en/function.parse-ini-file.php

you'll be happy :)

remi bourgarel
This one is perfect. Fast and easy to use.I "discovered" it some month ago, I'm glad I did.
Arkh
+2  A: 

It depends on how you want to store your configuration. You can just include a php file that has stuff like:

$config['stuff'] = "value";

but you can also use a config (ini) file or a xml file. PHP has standard functions available to read config files or xml files, so that´s easy as well.

jeroen
+3  A: 

You can have a file with configuration and then include it on every script, like jeroen told you:

config.inc.php

$config['dbname'] = 'myDB';
$config['dbuser'] = 'user';

...

then in your scripts

include_once('config.inc.php');

You could also use inheritance where you have a model for example that uses the config and then you can extend that model class.

mandril
+1  A: 

You can use auto_prepend_file to prepend your settings in every php scripts that is executing. Its inside the php.ini, or you can use htaccess(php_value auto_prepend_file "path/mysettings.php", or using ini_set(). The file must be a valid or existing.

PHPWDev