php.ini auto_prepend_file not affecting child directories - php

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.

Related

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()
]));

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.

PHP display_errors=Off in php.ini settings issue

I have a custom php.ini file I created in my public_html directory. It works as I have checked the settings set are correct using phpinfo().
But the strange problem is the php.ini settings do not seem to apply to all sub_folders in the public_html directory?
Any ideas perhaps theres a setting needed in php.ini to apply to all subdirectories in public_html?
I found the answer myself you have to add the line into .htaccess file so it uses the custom php.ini file you created in public_html instead of having to copy php.ini file in every subdirectory you create.
Add this to .htaccess does the trick obviously replacing username
SetEnv PHPRC /home/USERNAME/public_html/php.ini
Not really a solution, but you can disable overriding the display_error setting by using php_admin_flag in your sites configuration. If it still happens then, you have set an error handler which misbehaves.

Define php.ini path with .htaccess inside a Wordpress theme to change upload_file_size?

I have a wordpress blog. I made a custom page-template that allows users to upload files to my server. Just a simple php script which connects to my server with a form to select a file, blabla.. you know!
However the upload-feature doesn't make any sense with a max_upload_filesize of 20mb.
Now i want to change that. According to my hosting-provider it's REALLY EASY, but i think for normal people without experience in that field it's not. Anyway...
they told me i should create a .htaccess file where my upload-script is housed. So that's actually in my wordpess theme folder.
in the .htaccess file i should place "suPHP_ConfigPath" and the path to this directory.
so in my case this would be: (at least i think that's the path)
suPHP_ConfigPath /clientdata/n5200-2-dynamic/a/l/mydomain.com/www/wp-content/themes/mytheme/
Moreover i should create a php.ini file in the same directory that says:
upload_max_filesize = 500M ;
post_max_size = 500M ;
I did all of that and even created a info.php file in the same directory to check if the thing worked. However the php_info tells me still that the upload-size is set to 20mb.
Did I get anything wrong here? Do you guys have an idea what I'm doing wrong?
Does the php_info in that case even tell me the truth and the script should normally work? or should the php_info tell me that the size is set to the value I changed it to?
First of all, create a test.php file in the theme folder and put this into it;
<?php
phpinfo();
?>
Run http://mydomain.com/.../test.php in a browser and find 'Configuration File Path' on the page. If it does not match the one you mentioned in .htaccess, then the .htaccess is not working. Verify the name of .htaccess file and first make sure .htaccess is actually working first. If not, call your hosting support to help you out.
If the configuration path is showing as the one you specified in .htaccess, then
try putting these lines in the .htaccess file also and see if it works.
php_value upload_max_filesize "500000"
php_value post_max_size "500000"
If htaccess is working, also trying changing the value of the numbers in php.ini to 500000, instead of 500M.
I Hope your problem gets solved soon.
php_value upload_max_filesize 500000000
php_value post_max_size 500000000
instead of that very strange thing you put there
However, it would work only with Apache mod_php

How to turn off php safe_mode off for a particular directory in a shared hosting environment?

Hi I'm used shared hosting. I want to turn off php safe_mode off for my site. My provider gave me a php.ini file and asked me to put it with my settings in my public_html folder to override the settings, but it didn't work.
You can also try to create a file called php.ini in the root (public_html or other) folder, and putting the following in it:
safe_mode = Off
Depending on server settings, this may or may not work.
Your service provider might have forgot to tell you that you need to enable your user defined php.ini configuration by adding this line in the .htaccess file that you find in your public_html folder:
#Activates php.ini config located in main folder to work also recursively for all subfolders
suPHP_ConfigPath /home/YOUR_CPANEL_USER_NAME/public_html
Obviously replace YOUR_CPANEL_USER_NAME with your cPanel user name.
I'm supposing your server has got suPHP module(which is quite common in nowdays).
BTW: the php.ini file need to be in /home/YOUR_CPANEL_USER_NAME/public_html too and inside you should write this:
safe_mode = Off
And remember that Safe Mode is deprecated in PHP 5.3.0 and is removed in PHP 6.0.0.
TRY:
You can turn it off by adding the following line to a .htaccess file in your root (public html) folder.
If the .htaccess file doesn't exist, you can create it using a simple text editing program.
php_flag safe_mode Off

Categories