Knowledge Base

How to set the PHP include path

This article describes several methods for setting the include path in PHP. By using include paths, you can centralize code that your web site frequently uses. Additionally, some features, such as PEAR, require you to set the include path so PHP can locate the appropriate files.

You should create the include directory at the username directory level (that is, one level above the public_html directory). This ensures that sensitive files are not in the public_html directory, which anyone can access.

Method #1: Use a custom php.ini file

You can use a custom php.ini file to specify the include path. If you have not already set up a custom php.ini file, please read this article before you proceed.

To set the include path using a custom php.ini file:

  1. Open the php.ini file in an editor. You can do this by logging into your account over SSH, or by using the cPanel File Manager.
  2. Add the following line to the php.ini file. Replace USERNAME with your A2 Hosting username and replace include_directory with the include directory's name:
    include_path = ".:/home/USERNAME/include_directory"
  3. Save the file. The include path is now set.

Method #2: Use the set_include_path() function

Instead of setting the include path globally in a configuration file, you can set the path directly in a script file. To do this, you use the set_include_path() function.

When you set the include path using this method, it is only effective for the duration of the script's execution. The include path that you specify does not affect any other running scripts.

The following sample code demonstrates how to set the include path using the set_include_path() function:

<?php
    set_include_path(".:/usr/lib/php:/usr/local/lib/php:/home/USERNAME/php");
?>

Method #3: Use the .htaccess file

A few of our VPS and dedicated servers use Apache modules instead of CGI binaries to run PHP. If your server uses an Apache module to run PHP, you can modify the .htaccess file in your web site's document root directory.

To set the include path using the .htaccess file:

  1. Open the .htaccess file in an editor. You can do this by logging into your account over SSH, or by using the cPanel File Manager.
  2. Add the following line to the .htaccess file. Replace path with the include directory's path:
    php_value include_path ".:/path"
  3. Save the file. The include path is now set.

More Information