Powered by Translate |
Custom Search
| |
|
Five ways to create include path for PHP
One thing that people often complain about PHP is that there are always more than one way to do the same thing in PHP. This is very true for PHP includes. Here are 5 ways to create PHP include path.
Method #1: Use php.ini to create include path If PHP is compiled as a CGI binary, you can use php.ini to create include path. If you have no direct access to php.ini on the web server (e.g. you are using shared web hosting), you can create a custom php.ini on your hosting account's web root directory. Note that, you need to place your custom php.ini in every folder where include path is used.
Don't want to place php.ini in every subfolder?
GeeksEngine is hosted by LunarPages where PHP is compiled as CGI. In addition, LunarPages also uses suPHP to parse php scripts. An account can have multiple php.ini files in different folders so you can customize the php processing in different folders should your script require it. A php.ini file will not inherit down into subfolders. However, you can create a .htaccess file in the same folder as the php.ini file and place the following code into it: suPHP_ConfigPath /home/username/public_html/where "username" is your cPanel username. This will cause the php.ini file to affect all subfolders, unless a php.ini file is in a subfolder, at which point the php.ini in the subfolder takes precedence. To create include path in your php.ini file, download your current php.ini from your website by using a FTP client, or create a new one if you don't have it yet. Open php.ini file in your favorite text editor and add include path in either of the following two ways:
Note: Separate each include path by : (colon) on a Unix/Linux system. On Windows, the separator is ; (semi-colon). Here is the example given in php.ini file in C:\WINNT folder: UNIX: "/path1:/path2"
Method #2: Use .htaccess to create include path If PHP is compiled as a Apache module, you can use .htaccess to create include path. This file is placed at your web root directory.Download your .htaccess file if you have got one or create a new one. .htaccess is just a text file and can be edited by using a text editor. Open .htaccess in your text editor and use either of the following three options:
Method #3: Use ini_set function ini_set function sets the include_path configuration option and can be used in individual php file to create php settings on the fly. As it only works for the duration of the script, it has a slight performance penalty as ini_set is called every time the page runs. Not recommended. ini_set works in all PHP versions. Example 1: Include path is a relative path to a PHP page at web root.
<?
/* Assume this PHP file is report.php at web root. inc and common/new are relative path to this PHP page. Folder inc and common are in web root. */ ini_set("include_path", "inc"); ini_set("include_path", "common/new"); ?> Example 2: Include path is a relative path to a PHP page at /product/
<?
/* Assume this PHP file is /product/report.php. The folder product is in web root. ../inc and ../common/new are relative path to this PHP page. Folder inc and common are in web root. */ ini_set("include_path", "../inc"); ini_set("include_path", "../common/new"); ?> For detailed information about absolute path and relative path, refer to article Absolute Path and Relative Path Explained. Method #4: Use set_include_path function for PHP version >= 4.3.0 or PHP 5 set_include_path sets the include_path configuration option and can be used in individual php file to create php settings on the fly. As it only works for the duration of the script, it has a slight performance penalty as set_include_path is called every time the page runs. Not recommended.Example 1: Include path is a relative path to a PHP page at web root.
<?
/* Assume this PHP file is report.php at web root. inc and common/new are relative path to this PHP page. Folder inc and common are in web root. */ set_include_path("inc"); set_include_path("common/new"); ?> Example 2: Include path is a relative path to a PHP page at /product/
<?
/* Assume this PHP file is /product/report.php. The folder product is in web root. ../inc and ../common/new are relative path to this PHP page. Folder inc and common are in web root. */ set_include_path("include_path", "../inc"); set_include_path("include_path", "../common/new"); ?> Example 3: this example is from PHP.net website
<?
$path = "/usr/lib/pear"; set_include_path(get_include_path() . PATH_SEPARATOR . $path); ?> Note that get_include_path retrieves the current include path defined in php.ini file. PATH_SEPARATOR is a PHP constant. Making use of the PATH_SEPARATOR constant, it is possible to extend the include path regardless of the operating system. Based on what the operating system is, it can be used to get the separator character (used in php.ini) on the fly. On a Unix/Linux system, the separator is : (colon). On Windows, the separator is ; (semi-colon). PATH_SEPARATOR concatenates the include path defined by you to the one defined by your hosting company.
UNIX: "/path1:/path2:/path3"
Method #5: Manually code the include path in PHP page. You can define what folder is you include path in a PHP page like below. Note that, similar to ini_set or set_include_path, you need to use this code in individual PHP pages and it only lasts for the duration of the script. $_SERVER["DOCUMENT_ROOT"] returns the web root directory.
<?
$INC_DIR = $_SERVER["DOCUMENT_ROOT"]. "/inc/"; include($INC_DIR. "common.php"); ?> How to secure files in the include folder If you have placed the include folder in web root directory, because it can be accessed by anyone via internet and include files could contain sensitive information such as database password, you should always consider securing the folder and files in it. Normally, if the files in your include folder use file extension .php, or any other scripting file extensions such as .pl, .asp, .jsp, viewing the page should not reveal page content because these files are processed by web servers before the rendered HTML is sent to viewer's web browser. To have a 100% peace of mind and prevent any unfriendly probe attempt, it's always a good idea to secure the folder. There are two ways to secure the include folder:
Hope this article has given you some new ideas about creating include path in PHP. Happy PHPing! |
Copyright © 2011 GeeksEngine.com. All Rights Reserved. This website is hosted by LunarPages. No portion may be reproduced without my written permission. Software and hardware names mentioned on this site are registered trademarks of their respective companies. Should any right be infringed, it is totally unintentional. Drop me an email and I will promptly and gladly rectify it. |
Home | Kung Fu Timer | Feedback | Terms of Use | Privacy Policy |