6

I need to set the php_value session.save_path in .htaccess. This seems to require a full path, a relative path doesn't seem to work.

My webapplication runs on both Windows and Linux servers and I'd like to keep the .htaccess file the same on both systems, for deployment reasons.

Is it possible to reference the directory where .htaccess is, in the .htaccess file itself, something like this :

php_value session.save_path "<%systempath>/sessions"

with <%systempath> being automatically filled in on each system ?

2

4 Answers 4

2

You can automatically prepend a php file which will configure php with ini_set()

# .htaccess
php_value auto_prepend_file "autoprepend.php"

Then:

// autoprepend.php
ini_set('session.save_path', __DIR__ . '/sessions');

This way it will work for any php script within the directory.

1

I don’t think this is possible with any Apache directive.

But you can do that with PHP:

ini_set('session.save_path', dirname(__FILE__).'/sessions');

Here __FILE__ is the magic constant that holds the file system path to the current PHP script file and dirname returns the parent directory of that file.

2
  • Thanx but I'd much rather do it in .htaccess since I have multiple scripts that use sessions. With .htaccess I'd only have to set this path in one place...
    – Dylan
    Feb 10, 2011 at 13:42
  • +1 @Dylan: Apache does not support variables usage in .htaccess. mod_env allows you to set (only) environment variables, and mod_rewrite allows you to use them in rewrite rules and conditions, but that's it.
    – netcoder
    Feb 10, 2011 at 16:38
1

I know you can use some environment variables.

You can check if it will posible or not with

%{ENV:variable}
1
  • Works only in mod_rewrite unfortunately.
    – netcoder
    Feb 10, 2011 at 16:38
1

in apache .htaccess for example apply php_value only a php files.

SetEnvIf Request_URI ".php$" IS_PHP
<If "-T env('IS_PHP')">
    php_value auto_prepend_file "./init.php"
</If>
1
  • on windows SetEnvIf not work for me. However, PHP (with var_dump(getenv('IS_PHP')); ) show IS_PHP = true Feb 19, 2022 at 11:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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