17

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.

Yaron
  • 10,166
  • 9
  • 45
  • 65
Yeak
  • 2,470
  • 9
  • 45
  • 71

2 Answers2

24

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";
Idrizi.A
  • 9,819
  • 11
  • 47
  • 88
kittycat
  • 14,983
  • 9
  • 55
  • 80
  • You might want to mention exactly HOW to make a php.ini file and what EXACTLY he should write in it. – grepsedawk Feb 14 '13 at 21:38
  • @Alex.Piechowski make a php.ini file?? Every PHP installation already has one. Also I have told OP should put the path to the file as the value. – kittycat Feb 14 '13 at 21:39
  • Yeah, I commented on the post what you only had a link in that answer, I appoligize. – grepsedawk Feb 14 '13 at 21:44
  • I tried using htaccess but it wouldnt work. any ideas what might cause it to stop. Or can you tell me the correct format to put in the htaccess assuming my files is in /root/config/config.php – Yeak Feb 14 '13 at 21:48
  • @Alex.Piechowski it likely means your PHP is running as CGI so `php_value` is not a recognized Apache directive. That method will only work if PHP is running as an Apache module. – kittycat Feb 14 '13 at 22:32
  • How do I determine cgi vs. appache? I think it IS appache, the 500 says `Apache Server at example.com Port 80` – grepsedawk Feb 14 '13 at 22:34
  • @Alex.Piechowski run `phpinfo();` to see if it is. If you see a section titled Apache like in this [example](http://hosting.iptcom.net/phpinfo.php#module_apache) it means PHP is running as an Apache module, if you do not see that section it is running as CGI. – kittycat Feb 14 '13 at 22:40
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/24537/discussion-between-cryptic--and-alex-piechowski) – kittycat Feb 14 '13 at 23:57
-3

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

horatio
  • 1,426
  • 8
  • 7
  • If you saw his post, he said `"I dont want to do any includes on another php file for this i just want this to be loaded everytime automatically before anything else. Basically a universal include."` – grepsedawk Feb 14 '13 at 22:01
  • Ya i dont want to use any includes or requires. Thats how i have it now but wanna change it – Yeak Feb 14 '13 at 22:20