I have a
config.php file which I want to include to all php files in the directory.
Is there a way to include a file by default (hence I won't need to write require 'config.php' in all files)?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
You want to use auto_prepend_file. Set this directive in your php.ini or .htaccess file to the path to your
config.phpfile and any PHP file accessed will automatically have the contents of the config file prepended to it.
php_value auto_prepend_file /full/path/to/file/config.php
Keep in mind this ONLY will work on a server where PHP is run as an Apache module. If PHP is run as a CGI you need to add edit it in your
php.inifile or put it inside a
.user.inifile just without the
Nginxyou could add this line to server configuration inside
location ~ \.php$
fastcgi_param PHP_VALUE "auto_prepend_file=/full/path/to/file/config.php";