2

I would like to prepend a PHP script automatically using .htaccess. I know that there is a directive to do this. I have the following in my .htaccess file in the web root directory:

php_value auto_prepend_file "./init.php"

The contents of the init.php file include:

<?php
    $_SERVER['WEB_ROOT']=
        str_replace($_SERVER['SCRIPT_NAME'],'',
        $_SERVER['SCRIPT_FILENAME']);

This calculates the path of the web root and puts it into the superglobal. It is the only practical way I can think of keep track of the web root for things like includes.

The problem is that this only works for for PHP scripts also in the web root. If the script itself is in a sub directory I get an error, because the init.php file is not in the current directory. I don’t want to specify an absolute path to init.php because the current is a bit volatile at the moment.

I can think of 2 solutions:

  • include a .htaccess file in the sub directories pointing to the parent directory using a relative path (../init.php)
  • include a init.php file in all of the subdirectories

Is there another way to specify a prepended file which works for all subdirectories?

Failing that, is there another way to put the web root into a superglobal without this nonsense?

Manngo
  • 14,066
  • 10
  • 88
  • 110

1 Answers1

0

Define the path relative to the auto-prepend file itsef.

init.php

define('WEBROOT', __DIR__);

You should then be able to use WEBROOT wherever in your application. Or if you want to stick to your current approach:

$_SERVER['WEB_ROOT'] = __DIR__;

Just curious. Why not just use $_SERVER['DOCUMENT_ROOT']. Doesn't that refer to your web root?

BeetleJuice
  • 39,516
  • 19
  • 105
  • 165
  • Thanks for your answer. However, it doesn’t work for me. I like your approach of using `__DIR__`, which is better than mine, but the problem is getting to the `init.php` file in the first place. The `.htaccess` file is in the web root, and so is the `init.php` file, but if I load from a sub directory the `init.php` file is in the wrong place. – Manngo Aug 12 '17 at 06:46
  • Apropos `$_SERVER['DOCUMENT_ROOT']`, this appears to be the main document root. I have a number of virtual hosts, and this doesn’t appear to refer to them. – Manngo Aug 12 '17 at 06:48
  • @Manngo, I'm a bit confused. `__DIR__` returns the directory of the current file, so if you're using the same `init.php` as the `auto_prepend_file`, then no matter which script you actually run, that `init.php` and its directory don't change. What am I missing? – BeetleJuice Aug 12 '17 at 12:48
  • For testing purposes, I have `print __DIR__;` in `init.php`. Both are in the web root directory. If I execute a PHP file (`articles.php`) from a sub directory, I get `Fatal error: Unknown: Failed opening required 'init.php' (include_path='.:/usr/share/pear:/usr/share/php') in Unknown on line 0`. I take it that this is because `.htaccess` instructs the `articles.php` file to prepend `init.php` from the same sub directory, which doesn’t exist — only in the parent root directory. The problem is not with the `init.php` file itself, but referencing it from the subdirectory. – Manngo Aug 12 '17 at 13:05
  • @Manngo Oh I see. Don't set the `auto_prepend_file` in `.htaccess` files. Do it one time in `php.ini` instead. Remember to restart the server. – BeetleJuice Aug 12 '17 at 13:09