21

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?

2

4 Answers 4

28

Look at this :

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

you'll be happy :)

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

Comments

12

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.

Comments

2

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.

Comments

2

You can use auto_prepend_file to prepend your settings in every PHP scripts that is executing. It's 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.

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.