Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto prepend PHP file using htaccess relative to htaccess file

Tags:

php

.htaccess

I used:

php_value auto_prepend_file "file.php"

in my .htaccess that is in public_html folder.

Now when I run public_html/sub/index.php i get this error :

Fatal error: Unknown: Failed opening required 'file.php'

How to use auto_prepend_file flag to include a file relative to .htaccess file ?

like image 984
Omid Avatar asked Jan 28 '12 13:01

Omid


2 Answers

The file must be inside PHP's include_path. So you must either set the file's directory to be in the include_path inside php.ini, or do it in the .htaccess with a php_value statement.

php_value include_path ".:/path/to/file_directory"
php_value auto_prepend_file "file.php"

If you use the above method in .htaccess, be sure to copy the include_path from php.ini in and add the :/path_to/file_directory so you don't lose any already needed includes.

Alternatively, just add :/path/to/file_directory to include_path directly in the php.ini

Update

If you cannot modify the include_path, you might try specifying a relative path to the auto_prepend_file. This should work since the file path sent is processed identically as if it was called with require():

php_value auto_prepend_file "./file.php"
like image 121
Michael Berkowski Avatar answered Oct 18 '22 09:10

Michael Berkowski


In your .htaccess

php_value auto_prepend_file /auto_prepend_file.php
php_value auto_append_file /auto_append_file.php

Next create 2 files in root

1) /auto_append_file.php

$appendFile = $_SERVER['DOCUMENT_ROOT'] . '/append.php';
require_once($appendFile);

2) /auto_prepend_file.php

$prependFile = $_SERVER['DOCUMENT_ROOT'] . '/prepend.php';
require_once($prependFile);

Now it should work on local and live servers irrespective of physical path or website providing each of your websites use the same filenames append.php and prepend.php.

like image 40
Frankos Avatar answered Oct 18 '22 10:10

Frankos