I want to load the file variables.php (that simply contains variables) at the run of any pages of my project so I can use the variables from any place.
I created a file called .user.ini (and placed it in the root folder):
; Automatically add files before PHP document.
; http://php.net/auto-prepend-file
auto_prepend_file = /Volumes/www/project_name/admin/libs/variables.php
It doesn't work. It seems that PHP doesn't read the .user.ini file.
php.ini is right configured by default:
user_ini.cache_ttl 300 300
user_ini.filename .user.ini .user.ini
Where am I wrong?
Well, the manual says it all:
Since PHP 5.3.0, PHP includes support for configuration INI files on a
per-directory basis. These files are processed only by the CGI/FastCGI
SAPI. This functionality obsoletes the PECL htscanner extension.
And:
If you are using Apache, use .htaccess files for the same effect.
... though it actually refers to the Apache module (mod_php).
If you need SAPI-independent custom settings I'm afraid you'll have to use both. You can minimise the maintenance burden if you keep those settings to the minimum (most PHP directives can be provided in PHP code itself). And you need to ensure that .htaccess settings don't crash when mod_php is not available:
<IfModule mod_php5.c>
php_value auto_prepend_file /Volumes/www/project_name/admin/libs/variables.php
</IfModule>
I think .user.ini should be located in project root folder, for example, /Volumes/www/project_name/.user.ini
In addition to the main php.ini file, PHP scans for INI files in each
directory, starting with the directory of the requested PHP file, and
working its way up to the current document root (as set in
$_SERVER['DOCUMENT_ROOT']). In case the PHP file is outside the
document root, only its directory is scanned.
.user.ini documentation
Related
On a LAMP stack I was able to use:
> php_value auto_prepend_file bootstrap.php
in .htaccess to bootstrap a bunch of commands that I wanted to execute before any page was ever processed.
Is it possible to do the same in IIS 6.0 ?
UPDATE:
Not the prettiest solution, but since I have one site on this server I just added the auto_prepend_file setting in php.ini and restarted IIS
You can either use the registry to set a per-directory configuration. Or .user.ini files (since php 5.3) :
Since PHP 5.3.0, PHP includes support for .htaccess-style INI files on a per-directory basis. These files are processed only by the CGI/FastCGI SAPI. This functionality obsoletes the PECL htscanner extension. If you are using Apache, use .htaccess files for the same effect.
When I pull up phpinfo() from my webroot it details local and master php config settings. I'm familiar with the principal -- master settings are handled in the main php.ini file and can be overridden in site directories with php.ini files, ini_set() calls, or .htaccess files (depending on how the server is configured).
For a few settings I'm seeing local values that differ from the master settings, and for the life of me, can't locate where they are coming from. The loaded/parsed php.ini section of phpinfo() doesn't shed any light, and I've grep'd the heck out of the server trying to find where specific settings are found in files, without success.
Is there any way to locate the source of a setting?
If you've already checked "loaded ini files" section, there are a few more places where the settings can be defined:
if you're running Apache module, check for php_value in its config files
check for php_value in .htaccess
if you're using fastcgi (with nginx, apache or anything else), extra parameters can be specified in the command line (although who would do this?)
there are also default PHP setting
ini_set directive in the code above or in auto-prepended file defined in auto_prepend_file directive
this page http://php.net/manual/en/configuration.changes.modes.php also mentions something about Windows registry and .user.ini (oh gosh, never thought about this!)
I am new to the world of web and want to know that when server default php.ini file, custom php.ini(that we created manually and added into per directory) file, .user.ini file is loaded/read by the server ?
According to my concept these files are loaded/read on each request or loaded/read on each process at the cgi.
Can anyone tell in easy words ?
Referring to the documentation, the default php.ini is loaded, when php is started. You can place your custom user.ini in the same location. Php is looking in default directories for all *.ini-files for including. You have to restart php whenever you apply changes to the configuration.
FYI, manual - the configuration file
For My PDF Generation I am using MPDF. But its not working in client's server. It shows no problem in my local system and my server. So I tried to change the max_execution_time, max_input_time, memory_limit in the ini file. But its not changing. When I used .htaccess it showed " For security reasons, mod_php is not used on this server. Use a php.ini file for php directives"..
I tried ini_set(), .user.ini file, php.ini file options. Is there any other way I can change the ini settings on the server?
Adding this specific line to .htaccess file reads the newly created php.ini file in that folder.
AddHandler application/x-httpd-php5 .php
We have PHP 5.2.6 deployed to c:\php and in that folder there is the php.ini file. On Windows, can a website override these settings similar to the way that apache has .htaccess? e.g.
DirectoryIndex index.php index.html
<IfModule mod_php5.c>
php_flag magic_quotes_gpc off
php_flag register_globals off
</IfModule>
<IfModule mod_php4.c>
php_flag magic_quotes_gpc off
php_flag register_globals off
</IfModule>
Update:
I was aware of ini_set() but wondered if there was a declarative way to do this in a configuration file in the website rather than in script.
I would recommend doing all you can to avoid changing register_globals to on as it's a major security hole.
But you can try using init_set() to change the settings within your PHP code, although some settings cannot be changed once PHP has started running. (These are somewhat server dependent I believe.)
You can override the directives in the php.ini file several ways, but not all directives can be changed by each method. See the php.ini directives page in the manual for a list of the directives and the methods that will work on each one.
The last column in the table lists the methods that will work on that particular method. In increasing level of access:
PHP_INI_USER - Can be set in user
scripts with ini_set() (or any higher method)
PHP_INI_PERDIR - Can be set using
the .htacess file with php_value
for string values or php_flag for
binary values (or any higher method)
PHP_INI_SYSTEM - Can
be set using php.ini or httpd.conf
only (both require access to the server's configuration files)
PHP_INI_ALL - Can be set using
any of the above methods
ini_set should do what you're after -
$option = 'magic_quotes_gpc';
echo "Value of $option => ", ini_get($option);
ini_set($option,0);
echo "New value of $option => ", ini_get($option);
A caveat here is that just because you can set the value at run-time doesn't mean it will work as expected, e.g. setting register_globals at runtime will be of little use as that setting has already done it's job by the time your script starts.
From http://us.php.net/configuration.changes:
Changing PHP configuration via the Windows registry
When running PHP on Windows, the configuration values can be modified on a per-directory basis using the Windows registry. The configuration values are stored in the registry key HKLM\SOFTWARE\PHP\Per Directory Values, in the sub-keys corresponding to the path names. For example, configuration values for the directory c:\inetpub\wwwroot would be stored in the key HKLM\SOFTWARE\PHP\Per Directory Values\c\inetpub\wwwroot. The settings for the directory would be active for any script running from this directory or any subdirectory of it. The values under the key should have the name of the PHP configuration directive and the string value. PHP constants in the values are not parsed. However, only configuration values changeable in PHP_INI_USER can be set this way, PHP_INI_PERDIR values can not.
...Haven't actually tried this yet, so your mileage may vary.
I just found a new way of doing this.
First of all, I used phpinfo() to find the PHP.ini being used by my Hosting provider.
Thereafter, I uploaded a file containing the following code to my Hosting space:
$fsrc = fopen($pathToIni,'r');
$fdest = fopen($myHostingDir,'w+');
$len = stream_copy_to_stream($fsrc,$fdest);
fclose($fsrc);
fclose($fdest);
echo $len;
This effectively copied the php.ini to my Hosting space. Thereafter, I downloaded that php.ini, changed the register_globals to off (for which I did all this), and uploaded it to the root of my Hosting space. Bingo, there you go.
I have relied on the fact that IIS uses the complete php.ini if available in a directory. You cannot override only specific settings like that using .htaccess on Apache.
For cgi environments, there is a module called htscanner. It basically fakes .htaccess behavior and allows per directory configurations. Unfortunately I have no experience with this on Windows, let alone with IIS6.