0

Have a sub directory in a shared server.
So cannot modify php.ini.
php_value auto_prepend_file "config.php"
Want to include config.php but is not working as if it never been executed.

phpinfo.php is the same folder, and it shows auto_prepend_file no value

I also find this but just come into internal server error.

tech_me
  • 540
  • 2
  • 7
  • 20
  • You can use ini_set() to set the directive or just include/require the config.php in your script. https://www.php.net/manual/en/ini.core.php#ini.auto-prepend-file The site you are refering to is configuring the webserver. – Markus Zeller Aug 06 '23 at 13:29
  • Keep in mind that PHP 5.2 is horribly outdated and should no longer be used since years. Why not include config.php in your code to make it more obvious how the execution works? – Nico Haase Aug 06 '23 at 13:46
  • Thank you guys. I do not want to use the old **5.2** version, but just as I am modify a old system... And include `config.php` in my script is a good idea but I have to include it in many files in the same folder, so less helpful. – tech_me Aug 06 '23 at 13:57
  • This directive is executed for every request, so I don't think it's a good idea to set a relative path. Is that link from your hosting provider documentation? [How is PHP running on your server](https://stackoverflow.com/questions/16414054/find-out-how-php-is-running-on-server-cgi-or-fastcgi-or-mod-php)? – Álvaro González Aug 07 '23 at 09:25
  • @ÁlvaroGonzález I know its not a good idea, but the old system just as it before with some server side configurations changed by others. That link is not mine but just Googled from some where. – tech_me Aug 07 '23 at 13:08
  • If that old system you're seemingly migrating really relies on relative paths to skip `config.php` execution when not needed, it's rather straightforward to add a path check inside the file. But as you wish. BTW, you didn't answer the most important question. If your PHP interpreter does not run as Apache module, it will not honour any PHP directive set in Apache settings. – Álvaro González Aug 08 '23 at 06:05
  • @ÁlvaroGonzález Thank you. I am sorry I missed the important question, which is `cgi` (not Apache module). – tech_me Aug 08 '23 at 06:12

1 Answers1

1

.htaccess is a configuration file for the Apache HTTP Server and, as such, it can be used to configure PHP as long as it's running as Apache module. You have the php_value directive available, or otherwise you wouldn't be able to use it at all, what means your server has some Apache module running, but your current interpreter is not that one. You can only have one PHP version running as Apache module, but you can have as many versions as you want if running through other server APIs.

For CGI-based interpreters you can either use a custom php.ini file arranged by your hosting provider, if any (look for such file in your account), or create a .user.ini file (mind the leading dot) in your document root. That is a PHP INI file, it doesn't have anything to do with Apache, so you need to use the syntax as in any other PHP INI file rather than .htaccess syntax:

auto_prepend_file = "/path/to/config.php"

As you kindly noted, per-directory INI files are a PHP/5.3+ feature. So I think you're essentially out of luck with this path. You need to either use whatever Apache module version your hosting service provides, or contact their support to see if you have a custom php.ini file available for the CGI setup.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360