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)?



This SO Q&A seems to answer your question

You want to use auto_prepend_file. Set this directive in your php.ini or .htaccess file to the path to your config.php file and any PHP file accessed will automatically have the contents of the config file prepended to it.

For .htaccess:

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.ini file or put it inside a .user.ini file just without the php_value part.

In Nginx you could add this line to server configuration inside location ~ \.php$

fastcgi_param PHP_VALUE "auto_prepend_file=/full/path/to/file/config.php";
  • Thank you! I use nginx. What should I do in case when I want to add only to specific directory files? For Apache I would use .htaccess in those directories.
    – Leeloo
    Aug 13 '17 at 12:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.