The PHP include path tells PHP where to look for files when using functions like include() and require(). By configuring the include path, you can centralize commonly used code and ensure PHP can locate libraries such as PEAR. On ruachost.com, you can set the include path in several ways.
Why Set the Include Path?
-
Centralize reusable code (e.g., libraries, configuration files).
-
Ensure PHP can locate external packages like PEAR.
-
Improve maintainability by avoiding long relative paths.
-
Enhance security by keeping sensitive files outside
public_html.
Methods to Set the Include Path
Method 1: Use the PHP Selector in cPanel
-
Log in to cPanel.
-
Go to Software → Select PHP Version.
-
Click the Options tab.
-
Locate the
include_pathoption. -
Enter your desired path (separate multiple directories with a colon
:). Example:.:/home/username/include -
Changes take effect immediately.
Method 2: Use a Custom php.ini File
-
Create or edit a
php.inifile in your account. -
Add the following line (replace
usernameandinclude_directory):include_path = ".:/home/username/include_directory" -
Save the file.
-
PHP will now use this include path for scripts in that directory.
Method 3: Use the set_include_path() Function
Set the include path directly in a PHP script:
<?php
set_include_path(".:/usr/lib/php:/usr/local/lib/php:/home/username/php");
?>
-
Effective only for the duration of the script’s execution.
-
Does not affect other scripts.
Method 4: Use the .htaccess File
If PHP runs as an Apache module, you can set the include path in .htaccess:
php_value include_path ".:/path/to/include"
-
Place this in your site’s root
.htaccessfile. -
Effective for all scripts in that directory.
Best Practices
|