include for all files - php

I have a 'template.php' file which I manually include in most of my php files - is there a way to somehow automatically include a php file into all of my other files?
I could not find any examples that worked for me.

You can add an .htaccess file to your directory and use the following
php_value auto_prepend_file template.php
You can also read about PHP's auto_prepend_file directive Here

Related

php.ini auto_prepend_file not affecting child directories

I've always placed
php_value auto_prepend_file [path to config.php]
in the .htaccess file to include my config.php file in every page. However, I just switched to a new host (namecheap), and this host gives me the error "Invalid command 'php_value', perhaps misspelled or defined by a module not included in the server configuration".
According to this answer, the host is forcing me to set the auto_prepend file directly in the php.ini. I did this (placed a php.ini into public_html and set the auto_prepend_file value), and it worked, but only for the home page. In every page besides the home page, the config.php is not included. It seems like the auto prepend only works on every page if I paste the php.ini into every directory, which definitely isn't proper. I'm at a loss how to include the config.php into every php file... I really don't want to paste a require_once command into every file.
Found the answer here.
I had to add suPHP_ConfigPath /home/username/public_html to make the php.ini file recursive instead of setting php_value.

Using apache configuration to specify PHP include directory for each site

I recently switched to fastcgi and now I have a problem with setting the php include path which had previously been set in .htaccess as:
php_value include_path "[INCLUDE PATH]"
Since the switch, this gives the error:
Invalid command 'php_value', perhaps misspelled or defined by a module
not included in the server configuration
And to my understanding 'php_value' cannot be set via htaccess with fastcgi.
Are there any ways to get around this or to globally specify the php include path for a whole site with subdirectories, such as through the apache configuration?
I know that I can manually set the include path on each script in php or I can specify the include path in a php.ini in the directory, but the websites are already established with many files and directories and it would be cumbersome to go through each file and directory to copy the new include paths or php.ini files.
You're pretty much out of luck as only the PHP module supports the php_value and php_flag directives.
What you can do is create a php.ini file for each website. From memory, you will need this to be a full php.ini file as a new file will completely override the system one.
One simple way to do this would be copy the system file and then add your custom property, eg
cp /usr/share/php5/php.ini /path/to/site/php.d/php.ini \
&& echo 'include_path = "[INCLUDE PATH]"' >> /path/to/site/php.d/php.ini
Then, in the site's .htaccess file, set the PHPRC environment variable. This is to avoid having to add a php.ini file into every directory.
SetEnv PHPRC /path/to/site/php.d
Ideally though, each site should take care of its own include_path in code. This will make your applications much more portable.
set_include_path(implode(PATH_SEPARATOR, [
'[INCLUDE_PATH'],
get_include_path()
]));

Force include in php.ini?

Is there anything I can change in php.ini that includes a php file before running any other file? The equivalent of adding "require('somefile.php');" before every file?
You can use the auto_prepend_file directive.
Search for auto_prepend_file = [path/to/file] in php.ini. Replace [path/to/file] with your file

PHP auto include

I want to auto include a PHP script onto every exection into the server, I was hopping to do it via the PHP ini, via a setting or being able to write an extention in php that was simple and included my php script.
You can set the auto_prepend_file directive in your php.ini file:
http://php.net/manual/en/ini.core.php#ini.auto-prepend-file
If running Apache and can access .htaccess you can do the following otherwise look at #Lock's answer
prepend.php:
<?php
echo "<p>this is the prepended file</p>\n";
main.php:
<?php
echo "<p>this is the main file</p>\n";
append.php:
<?php
echo "<p>this is the appended file</p>\n";
And prepend prepend.php and append append.php using the instructions below, when main.php is called the following would be outputted from the script:
<p>this is the prepended file</p>
<p>this is the main file</p>
<p>this is the appended file</p>
And prepend prepend.php and append append.php using the instructions below, when main.php is called the following would be outputted from the script:
<p>this is the prepended file</p>
<p>this is the main file</p>
<p>this is the appended file</p>
Prepending a script
To prepend a file so it is parsed before the main script, add the following setting to the .htaccess file, php.ini (which of course would affect all websites), or the config:
php_value auto_prepend_file prepend.php
A path does not need to be included but if it isn't then it will use the include path to find it. This can result in an error like "Warning: Unknown: failed to open stream: No such file or directory in Unknown on line 0" if there is no copy of this file in the include path or directory the script is in. So it's best to have the full path to the prepended file.
Appending a script
This is almost the same as prepending a script and the same notes apply. The way to append a file is as follows:
php_value auto_append_file append.php
Overriding a setting so nothing is prepended or appended
If you need to override an existing auto_append_file or auto_prepend_file setting you can do this by setting the value to "none" like so:
php_value auto_prepend_file none
php_value auto_append_file none
This can be useful if you want to have .htaccess set the append/prepend file at the root level of a website but then want a particular subdirectory to not do the append or prepend. You would create a new .htaccess file in that subdirectory which sets them to none as above.
Source: http://www.electrictoolbox.com/php-automatically-append-prepend/
Another solution, if you're on Apache and this is available to you, is to use .htaccess. I add a line:
php_value include_path "/var/www/mysite.com/config"
and then in my PHP files, I can include
include_once('someconfig.php');
which looks in /var/www/mysite.com/config/. Admittedly, I've done this without knowing the auto-prepend solution--which looks much cleaner and more efficient.

Execute php script before every php script?

How would I run this before every php script besides putting it in all of them?
if ($_SERVER['REMOTE_ADDR'] == '123.123.123.123')
{
$_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_X_REAL_IP'];
}
I basically want the same affect as putting that at the top of every script without actually doing that.
Put it in its own file and set the auto_prepend_file configuration in the php.ini / .htaccess file to point to it.
Update: Since you mentioned lighttpd in a comment, note that you can configure it like this in the global INI file with PHP 5.3:
[PATH=/vhost/domain.com]
auto_prepend_file = /vhost/domain.com/foo.php
[HOST=domain.com]
auto_prepend_file = /vhost/domain.com/foo.php
Or you can create the file /vhost/domain.com/.user.ini and do the same:
auto_prepend_file = /vhost/domain.com/foo.php
If you have the necessary rights to change your PHP configuration, auto_prepend_file is exactly what you're looking for.
auto_prepend_file
Specifies the name of a file that is automatically parsed before the main file. The file is included as if it was called with the require() function, so include_path is used.
The special value none disables auto-prepending.

Categories