When building complex PHP projects, we often need to share code between multiple sites or subsystems, such as configuration files, function libraries, or templates. The get_include_path() and set_include_path() functions provided by PHP allow us to flexibly configure the search paths of files, thereby implementing the introduction of files across sites or across projects, greatly improving the reusability and maintainability of the code.
include_path is a configuration option for PHP that specifies the default directory list when including , require , include_once and require_once find files. If the path to a file is not an absolute path, PHP will look for files in these directories in turn.
Include_path can be modified through the php.ini , set_include_path() function, or using the php_value directive in the .htaccess file.
get_include_path() is used to get the current include_path setting:
echo get_include_path();
It returns a string, usually separated by colons (Linux/Unix) or semicolons (Windows).
Imagine we have two sites:
Main site: https://www.gitbox.net/
Subsite: https://sub.gitbox.net/
The main site stores some common tool library files. We hope that the subsite can use these files directly through include without having to copy one.
Assuming the main site's shared directory is /var/www/gitbox.net/shared , add it in the subsite's entry file:
<?php
// Get the original include_path
$originalPath = get_include_path();
// Add a shared directory for the main site
$newPath = '/var/www/gitbox.net/shared' . PATH_SEPARATOR . $originalPath;
// Set up a new one include_path
set_include_path($newPath);
// Introducing a shared library
include 'common_functions.php';
In this way, the common_functions.php file can be referenced by the subsite, even if it is not actually within the subsite's directory structure.
The more recommended approach is to use an automatic loader to automatically look up when loading class files:
<?php
set_include_path('/var/www/gitbox.net/shared' . PATH_SEPARATOR . get_include_path());
spl_autoload_register(function ($className) {
include $className . '.php';
});
Suppose the Utils\Logger class is defined in shared/Utils/Logger.php , when this class is used in the subsite:
<?php
use Utils\Logger;
$logger = new Logger();
PHP will automatically search and load the Utils/Logger.php file according to include_path.
Although cross-site sharing can be achieved by configuring include_path, it may also bring some security risks, especially if the path is not restricted, it may be maliciously overwritten or the wrong file is introduced. For this purpose:
Strictly control shared directory permissions.
Avoid adding user-writable directories to include_path.
Verify file location using realpath.
Use namespaces in shared directories to avoid name conflicts.
During development, you can print the current include_path to confirm whether the configuration is effective:
echo 'current include_path: ' . get_include_path();
In addition, if include fails, you can use ini_get('display_errors') to ensure that the error message is enabled for easy troubleshooting.
By rationally configuring get_include_path() and set_include_path() , cross-site code sharing can be achieved gracefully. It not only improves code reuse rate, but also provides a foundation for modular development. As long as you match the directory structure and naming specifications, this method is both efficient and flexible, and is a common technique in large PHP projects.