<p>I can set the PHP include path in the <code>php.ini</code>:</p>

<pre><code>include_path = /path/to/site/includes/
</code></pre>

<p>But then other websites are affected so that is no good.</p>

<p>I can set the PHP include in the start of every file:</p>

<pre><code>$path = '/path/to/site/includes/';
set_include_path(get_include_path() . PATH_SEPARATOR . $path);
</code></pre>

<p>But that seems like bad practice and clutters things up. </p>

<p>So I can make an include of that and then include it into every file:</p>

<pre><code>include 'includes/config.php';
</code></pre>

<p>or</p>

<pre><code>include '../includes/config.php';
</code></pre>

<p>This is what I'm doing right now, but the include path of <code>config.php</code> will change depending on what is including it. </p>

<p>Is there a better way? Does it matter?</p>

[{'answer_id': 24631, 'author': 'Erik van Brakel', 'author_id': 909, 'author_profile': 'https://Stackoverflow.com/users/909', 'pm_score': 5, 'selected': True, 'text': '<p>If you\'re using apache as a webserver you can override (if you allow it) settings using <em>.htaccess</em> files. See <a href="http://us2.php.net/configuration.changes" rel="nofollow noreferrer">the PHP manual</a> for details.</p>\n\n<p>Basically you put a file called <em>.htaccess</em> in your website root, which contains some PHP <code>ini</code> values. Provided you configured Apache to allow overrides, this site will use all values in your PHP config, + the values you specify in the <em>.htaccess</em> file.</p>\n\n<blockquote>\n  <p>Can be used only with <code>PHP_INI_ALL</code> and <code>PHP_INI_PERDIR</code> type directives</p>\n</blockquote>\n\n<p>as stated in the page I linked. If you click through to the full listing, you see that the include path is a <code>PHP_INI_ALL</code> directive.</p>\n'}, {'answer_id': 24695, 'author': 'Gary Richardson', 'author_id': 2506, 'author_profile': 'https://Stackoverflow.com/users/2506', 'pm_score': 0, 'selected': False, 'text': '<p>You can set <code>include_path</code> in your php.ini file too. I\'m a perl guy, so I expect to be able to load includes and have <code>include</code> do the right thing. I have all my includes in a specific directory, which is added to <code>include_path</code>. I can do things like </p>\n\n<pre><code>require_once "ClassName.php";\n</code></pre>\n\n<p>I don\'t need to worry about relative paths or locations of files.</p>\n\n<p>I\'ve also written my own <code>CustomRequire</code> to do things like</p>\n\n<pre><code>function CustomRequire ($file) {\n    if(defined(\'MYINCLUDEPATH\')) {\n        require_once MYINCLUDEPATH . "/$file";\n    } else {\n        require_once $file;\n    }\n}\n</code></pre>\n\n<p>That way I can change how I do includes at a later date. Of course, you still need to find a way to include your include code :)</p>\n'}, {'answer_id': 25100, 'author': 'ceejayoz', 'author_id': 1902010, 'author_profile': 'https://Stackoverflow.com/users/1902010', 'pm_score': 1, 'selected': False, 'text': '<p>Depending on how your host is set up, you may be permitted to place a <code>php.ini</code> file in the root of your home directory with extra configuration directives.</p>\n'}, {'answer_id': 25107, 'author': 'Pierre-Yves Gillier', 'author_id': 2692, 'author_profile': 'https://Stackoverflow.com/users/2692', 'pm_score': 3, 'selected': False, 'text': "<p>Erik Van Brakel gave, IMHO, one of the best answers.</p>\n\n<p>More, if you're using Apache &amp; Virtual hosts, you can set up includes directly in them. Using this method, you won't have to remember to leave php_admin commands in your .htaccess.</p>\n"}, {'answer_id': 66774, 'author': 'djn', 'author_id': 9673, 'author_profile': 'https://Stackoverflow.com/users/9673', 'pm_score': 3, 'selected': False, 'text': '<p>Use a <em>php.ini</em> file in website <em>root</em>, if your setup uses <em>PHP</em> as <em>CGI</em> (the most frequent case on shared hosts) with the same syntax as the server-wide <em>php.ini</em>; put it into <code>.htaccess</code> if you have PHP as an Apache module (do a <code>phpinfo()</code> if unsure):</p>\n\n<pre><code>php_value include_path "wherever"\n</code></pre>\n\n<p>Note that per-folder <code>php.ini does</code> not affects <em>subfolders</em>.</p>\n'}, {'answer_id': 803335, 'author': 'Sherri', 'author_id': 97816, 'author_profile': 'https://Stackoverflow.com/users/97816', 'pm_score': 1, 'selected': False, 'text': '<p>Your application should have a config file written in PHP. Then include that with a relative page into every page in the program. That config file will have a variable for the path to the includes dir, templates dir, images dir, etc.</p>\n'}, {'answer_id': 3619057, 'author': 'Gras Double', 'author_id': 289317, 'author_profile': 'https://Stackoverflow.com/users/289317', 'pm_score': 2, 'selected': False, 'text': '<p>Why do you think append to include path is bad practice?</p>\n\n<p>This code near top of root script shouldn\'t be that bad...</p>\n\n<pre><code>$path = \'/path/to/site/includes/\';\nset_include_path($path . PATH_SEPARATOR . get_include_path());\n</code></pre>\n\n<p>IMHO the main advantage is that it\'s portable and compatible not only with Apache</p>\n\n<p>EDIT: I saw a drawback of this method: small performance impact. see <a href="http://www.geeksengine.com/article/php-include-path.html" rel="nofollow noreferrer">http://www.geeksengine.com/article/php-include-path.html</a></p>\n'}]
['https://Stackoverflow.com/questions/24622', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2118/']