note 109445 modified in function.include by aharvey

From: Date: Mon, 23 Jul 2012 07:01:57 +0000
Subject: note 109445 modified in function.include by aharvey
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-189365@lists.php.net to get a copy of this message
1. Don&#39;t use include to include any kind of config.php file if you can avoid it, it&#39;s inadvisable from a security standpoint. You should use a config.ini file outside of your www tree. See http://php.net/manual/en/function.parse-ini-file.php&#13;&#10;&#13;&#10;2. Depending on whether your server supports it, you can set a &#34;root&#34; directory variable for your scripts in your .htaccess file e.g. if your scripts are under /var/www/application you can set a variable in /var/www/application/.htaccess like:&#13;&#10;&#13;&#10;SetEnv APPROOT &#39;/var/www/application&#39;&#13;&#10;&#13;&#10;This variable will appear in the $_SERVER superglobal. You can then use it in the head of a php file. For example, if a particular file is in /var/www/application/classes, you might put this:&#13;&#10;&#60;?php&#13;&#10;chdir($_SERVER[&#39;APPROOT&#39;]);&#13;&#10;?&#62;&#13;&#10;Then you can include files relative to the application root directory without worrying about which directory it&#39;s in, or which directory the file that&#39;s including it in turn is in e.g. &#13;&#10;&#60;?php&#13;&#10;include(&#39;functions/strings.php&#39;);&#13;&#10;?&#62;&#13;&#10;Would include /var/www/application/functions/strings.php, not /var/www/application/classes/functions/strings.php&#13;&#10;&#13;&#10;If you have to move your application, you can easily edit the &#34;APPROOT&#34; in one place. --was-- 1. Don't use include to include any kind of config.php file if you can avoid it, it's inadvisable from a security standpoint. You should use a config.ini file outside of your www tree. See http://php.net/manual/en/function.parse-ini-file.php 2. Depending on whether your server supports it, you can set a "root" directory variable for your scripts in your .htaccess file e.g. if your scripts are under /var/www/application you can set a variable in /var/www/application/.htaccess like: SetEnv APPROOT '/var/www/application' This variable will appear in the $_SERVER superglobal. You can then use it in the head of a php file. For example, if a particular file is in /var/www/application/classes, you might put this: <?php chdir($_SERVER['APPROOT']); ?> Then you can include files relative to the application root directory without worrying about which directory it's in, or which directory the file that's including it in turn is in e.g. <?php include('functions/strings.php'); ?> Would include /var/www/functions/strings.php, not /var/www/classes/functions/strings.php If you have to move your application, you can easily edit the "APPROOT" in one place. http://php.net/manual/en/function.include.php

« previous php.notes (#189365) next »