Show php include path

Show php include path

Five ways to create include path for PHP

  1. Use php.ini
  2. Use .htaccess
  3. Use ini_set function
  4. Use set_include_path function
  5. Manually code the 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.

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:

  1. Include folder is at your hosting account’s home directory. It’s a good idea to create include folder at the home directory level (which is one level above the public_html folder) so that sensitive files are placed outside the publicly accessible web folder (where public_html is) — just an extra precaution. Add the following line in your custom php.ini file: include_path = «.:/home/your_cpanel_username/your_include_folder_name» Make sure to replace your_cpanel_username with your actual cPanel username and replace your_include_folder_name with the actual folder name you have created in your hosting account’s home directory. Setting up include path this way is recommended as no one can access folders in your home directory which is one level higher than your web root (public_html) directory.
  2. Include folder is at your hosting account’s web root directory. Web root directory is the folder you place all your web pages. When you connect to your hosting account via a FTP client, you should normally see a folder called public_html. This is the web root directory. Note that it might be named as something else by your hosting company. Please refer to your hosting account setup email or contact your hosting company if you are not sure. Add the following line in your custom php.ini file: include_path = «.:/home/your_cpanel_username/webroot/your_include_folder_name» Make sure to replace your_cpanel_username with your actual cPanel username. Replace webroot with the actual folder name of the web root directory (e.g. public_html), and replace your_include_folder_name with the actual folder name you have created in your hosting account web root directory. Note that there is a potential security issue related to the include folder if it’s placed in web root directory. Click here to jump to the bottom section of this page to see how to fix it.
  3. On Windows, the include folder can be any folder on your hard disk. For example, two include paths defined in php.ini for my local site: include_path = «.;C:\test;C:\php\PEAR» php.ini is at C:\WINNT on Windows 2000 or Windows XP.
Читайте также:  Centre table content html

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:

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:

  1. Include folder is at your hosting account’s home directory. Add the following line in your custom .htaccess file: php_value include_path «.:/home/your_cpanel_username/your_include_path» Make sure to replace your_cpanel_username with your actual cPanel username and replace your_include_path with the actual folder name you have created in your hosting account home directory. Setting up include path this way is recommended as no one can access folders in your home directory which is one level higher than your web root (public_html) directory.
  2. Include folder is at your hosting account’s web root directory. Add the following line in your .htaccess file: php_value include_path «.:/home/your_cpanel_username/webroot/your_include_path» Make sure to replace your_cpanel_username with your actual cPanel username. Replace webroot with the actual folder name of the web root directory (e.g. public_html), and replace your_include_folder_name with the actual folder name you have created in your hosting account web root directory.
  3. On Windows, the include folder can be any folder on your hard disk. For example, two include paths defined in .htaccess for my local site: php_value include_path «.;C:\test;C:\php\PEAR» Note that if you use .htaccess to create include path, it causes conflict with the one created in your php.ini file. You can only create include path in either .htaccess or php.ini on Windows but not both.

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.
*/

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

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.

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.

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:

    Use .htaccess file This is the easier way to do. Create a .htaccess file and place it in the include folder. Inside .htaccess file, add the following line: deny from all This denies anyone from accessing files in the include folder. Basically, a 403 Forbidden page is displayed when someone tries to view any page in this folder.

Set access permissions on the include folder

The screenshot below is FileZilla’s file attributes window. FileZilla is a open source free FTP client.

To secure your include path, connect to your hosting account via FTP client and browse to your web root. Right-click on the include folder name and select File Attributes. Three are three frames to manage three types of users — Owner, Group, Public. For Public Permission, uncheck all three checkboxes. This will prevent users from accessing files in this directory from internet.

Hope this article has given you some new ideas about creating include path in PHP. Happy PHPing!

Other Recent Articles from the Web Development category:

Copyright © 2023 GeeksEngine.com. All Rights Reserved.

This website is hosted by HostGator.

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.

Источник

get_include_path

Gets the current include_path configuration option value.

Parameters

This function has no parameters.

Return Values

Returns the path, as a string, or false on failure.

Examples

Example #1 get_include_path() example

// Or using ini_get()
echo ini_get ( ‘include_path’ );
?>

See Also

  • ini_get() — Gets the value of a configuration option
  • restore_include_path() — Restores the value of the include_path configuration option
  • set_include_path() — Sets the include_path configuration option
  • include — include

User Contributed Notes 1 note

If you are trying to find a file on your include path, there is a better way: http://php.net/manual/en/function.stream-resolve-include-path.php

  • PHP Options/Info Functions
    • assert_​options
    • assert
    • cli_​get_​process_​title
    • cli_​set_​process_​title
    • dl
    • extension_​loaded
    • gc_​collect_​cycles
    • gc_​disable
    • gc_​enable
    • gc_​enabled
    • gc_​mem_​caches
    • gc_​status
    • get_​cfg_​var
    • get_​current_​user
    • get_​defined_​constants
    • get_​extension_​funcs
    • get_​include_​path
    • get_​included_​files
    • get_​loaded_​extensions
    • get_​required_​files
    • get_​resources
    • getenv
    • getlastmod
    • getmygid
    • getmyinode
    • getmypid
    • getmyuid
    • getopt
    • getrusage
    • ini_​alter
    • ini_​get_​all
    • ini_​get
    • ini_​parse_​quantity
    • ini_​restore
    • ini_​set
    • memory_​get_​peak_​usage
    • memory_​get_​usage
    • memory_​reset_​peak_​usage
    • php_​ini_​loaded_​file
    • php_​ini_​scanned_​files
    • php_​sapi_​name
    • php_​uname
    • phpcredits
    • phpinfo
    • phpversion
    • putenv
    • set_​include_​path
    • set_​time_​limit
    • sys_​get_​temp_​dir
    • version_​compare
    • zend_​thread_​id
    • zend_​version
    • get_​magic_​quotes_​gpc
    • get_​magic_​quotes_​runtime
    • restore_​include_​path

    Источник

Оцените статью