0

I have been using setEnv in an .htaccess file, but the webhost recently updated the server to PHP 5.4 and since then my site has crashed due to the use of setEnv. They tell me that the function has been deprecated in PHP 5.4, but I don't see this documented anywhere.

Can anybody direct me towards an alternative method of defining an environmental variable, that can be used to retrieve include files on all pages without changing the path.

I've been using something like this:

// in .htaccess file
SetEnv INC_FILE path\include.php

// in every relevant .php file
require_once(getenv('INC_FILE'));

Due to this code it has been easy to move the files through different enviroments dev, test, production - without changing any file content. This has been also the purpose.

Which alternatives can you recommend, now that I can't use this feature. Alternative ways to do a similar task are also welcome.

Thanks.

nicolaib
  • 627
  • 5
  • 26
  • Define "_crashed_"? What do the error logs say? Anyways, `getenv` works fine in PHP 5.4, it's not [deprecated](http://www.php.net/manual/en/migration54.deprecated.php). Your example works just fine for me with Apache 2.4.3 and PHP 5.4.14/18 in my local testing environment. Maybe they need to update Apache and/or mod_env. – ndm Aug 17 '13 at 14:31
  • Internal Server Error 500 was the error. I can't see anywhere, that it should have been deprecated. – nicolaib Aug 17 '13 at 16:50
  • But the setEnv is probably an Apache function - I guess. Could it have been deprecated due to an Apache update? I don't see this anywhere either. – nicolaib Aug 17 '13 at 16:55
  • The Apache error log should give you more detailed information about the problem. Ask your hoster if you don't have direct access to these log files. `SetEnv` is provided by the [`mod_env`](http://httpd.apache.org/docs/2.4/mod/mod_env.html) module. So if that single like is causing the problem, then maybe there's a bug in `mod_env`, or it's outdated, not compatible anymore with the used Apache version, or maybe it was disabled/removed, etc... It's pretty much impossible to tell without at least seeing the Apache error logs. – ndm Aug 17 '13 at 17:04
  • My webhost tells me that they can't activate the module - which means that I have to find an alternative solution. Can anybody suggest a solution e.g. coded in PHP or other solution (can it be solved by setting an environment variable in php.ini)? – nicolaib Aug 19 '13 at 13:04

1 Answers1

1

mod_rewrite

Depending on your server setup it might be possible to use mod_rewrite to set an environment variable, however even if it works you may run into problems with redirects and stuff.

.htaccess

RewriteRule ^ - [L,E=INC_FILE:stats\\stats.php] 

app.php

require_once getenv('INC_FILE');

PHP configuration files

Another alternative would of course be to use .php configuration files. However, making the values defined in that config file automatically available to all scripts (just like environment variables), requires some php.ini tweaking. Using the auto_preped_file option you can define a file that is parsed before any other php file.

Using auto prepending

php.ini

auto_prepend_file = "path\to\your\config\file.php"

Alternatively it may be possible to set the value via .htaccess:

php_value auto_prepend_file "path\to\your\config\file.php"

config.php

// you could for example use a constant
define('INC_FILE', 'path\include.php');

// or use putenv() if you want to continue using getenv()
putenv('INC_FILE=path\include.php');

app.php

require_once INC_FILE;
// or
require_once getenv('INC_FILE');

Using manual include

If you can't/don't want to use auto_prepend_file, then you would have to include the configuration file in ever file where you need to the values:

app.php

require_once 'path\to\your\config\file.php';

require_once INC_FILE;
// or
require_once getenv('INC_FILE');

PHP.ini config variables

Regarding your question in the comments, no, you cannot define environment variables in php.ini configuration files, but you can add easily add custom configuration variables which can be read using get_cfg_var(), though it's not good practice to do so. Example:

php.ini

[MyCustomSettings]
my_custom_settings.inc_file = "path\include.php"

app.php

require_once get_cfg_var('my_custom_settings.inc_file');
Community
  • 1
  • 1
ndm
  • 59,784
  • 9
  • 71
  • 110
  • Man you're a genius :-) I'll go through the different solutions and test which one will work best. At least I already found out that the autoprepend in php.ini solution works. Thanks a lot! – nicolaib Aug 20 '13 at 22:48