Automatically load a config.php file for all pages before anything else

落花浮王杯 提交于 2019-11-30 00:49:14

问题


Does anyone know how to set up something where a specific file can be loaded automatically for all pages before anything else?

For example if I have a config.php file and I want this file to be loaded anytime anyone visits a page on my website.

In here I would have some configuration info that is required to load prior to anything else.

I don't want to do any includes on another php file for this I just want this to be loaded every time automatically before anything else. Basically a universal include.


回答1:


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";



回答2:


Use the require control structure. Place it at the top of each php file before any code, but after the "<?php" tag.

http://php.net/manual/en/function.require.php



来源:https://stackoverflow.com/questions/14884439/automatically-load-a-config-php-file-for-all-pages-before-anything-else

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!