Set local php ini

Setting php.ini Location In Apache

Note: This post is over two years old and so the information contained here might be out of date. If you do spot something please leave a comment and we will endeavour to correct.

After installing PHP on Apache you can use the php.ini file to set various different options to do with PHP. When Apache starts it uses what is contained in this file to set up and run PHP.

On both Windows, Unix and Linux systems Apache will look in a number of default locations for the php.ini file before giving up. You can explicitly tell Apache 2.x where to look for the file by using the PHPIniDir directive in the http.conf file.

For Apache 1.3.x this can be set using the SetEnv PHPRC directive.

This not only speeds up the time taken for Apache to start, but will also allow you to make sure that the php.ini file you are using is the one you are editing.

You can find the current location of the php.ini file by using the phpinfo() function.

Phil Norton

Phil is the founder and administrator of #! code and is an IT professional working in the North West of the UK. Graduating in 2003 from Aberystwyth University with an MSc in Computer Science Phil has previously worked as a database administrator, on an IT help desk, systems trainer, web architect, usability consultant, blogger and SEO specialist. Phil has lots of experience building and maintaining PHP websites as well as working with associated technologies like JavaScript, HTML, CSS, XML, Flex, Apache, MySQL and Linux.

Want to know more? Need some help?

Let us help! Hire us to provide training, advice, troubleshooting and more.

Support Us!

Please support us and allow us to continue writing articles.

Источник

ini_set

Устанавливает значение заданной настройки конфигурации. Настройка будет хранить установленное значение пока выполняется скрипт. После завершения работы скрипта значение настройки вернётся к исходному.

Список параметров

Не все доступные настройки можно изменять функцией ini_set() . Список доступных настроек приведён в приложении.

Возвращаемые значения

В случае успешного выполнения возвращает старое значение или false в случае возникновения ошибки.

Список изменений

Версия Описание
8.1.0 Параметр value теперь принимает любой скалярный тип (включая null ). Ранее допускались только строковые ( string ) значения.

Примеры

Пример #1 Установка значения ini-настройки

if (! ini_get ( ‘display_errors’ )) ini_set ( ‘display_errors’ , ‘1’ );
>

echo ini_get ( ‘display_errors’ );
?>

Смотрите также

  • get_cfg_var() — Извлекает значение настройки конфигурации PHP
  • ini_get() — Получает значение настройки конфигурации
  • ini_get_all() — Получает все настройки конфигурации
  • ini_restore() — Восстанавливает значение настройки конфигурации
  • Как изменить настройки конфигурации
Читайте также:  Разработка телеграм бота python

User Contributed Notes 20 notes

I have experienced on some systems that ini_set() will fail and return a false, when trying to set a setting that was set inside php.ini inside a per-host setting. Beware of this.

[[[Editors note: Yes, this is very true. Same with
register_globals, magic_quotes_gpc and others.
]]]

Many settings, although they do get set, have no influence in your script. like upload_max_filesize will get set but uploaded files are already passed to your PHP script before the settings are changed.

Also other settings, set by ini_set(), may be to late because of this (post_max_size etc.).
beware, try settings thru php.ini or .htaccess.

When checking for the success of ini_set(), keep in mind that it will return the OLD value upon success — which may have been «0». Therefore you cannot just compare the return value — you have to check for «identity»:

// This will NOT determine the success of ini_set(), instead
// it only tests if the old value had been equivalent to false
if ( ! ini_set ( ‘display_errors’ , ‘1’ ) )
throw new \ Exception ( ‘Unable to set display_errors.’ );

// This is the CORRECT way to determine success
if ( ini_set ( ‘display_errors’ , ‘1’ ) === false )
throw new \ Exception ( ‘Unable to set display_errors.’ );

?>

This explains reported situations where ini_set() «always» seems to fail!

set PHP_INI_PERDIR settings in a .htaccess file with ‘php_flag’ like this:

php_flag register_globals off
php_flag magic_quotes_gpc on

When your ISP does not allow you to add the default include directories — it might be useful to extend the ‘include_path’ variable:

If it’s not your server and therefore you want to hide the data in your session variables from other users, it�s very useful to set the session.save_handler in your scripts to shared memory with:

Remember: You have to set it in every script that uses the session variables BEFORE «session_start()» or php won’t find them.

Be careful with setting an output_handler, as you can’t use ini_set() to change it. *sigh*

In my php.ini I have this for my web pages (and I want it):

But this causes my command line scripts to not show output until the very end.

#!/usr/bin/php -q
ini_set ( ‘output_handler’ , ‘mb_output_handler’ );
echo «\noutput_handler => » . ini_get ( ‘output_handler’ ) . «\n» ;
?>

root@# ./myscript.php
output_handler => ob_gzhandler

Apparently (acording to Richard Lynch):

> TOO LATE!
> The ob_start() has already kicked in by this point.
> ob_flush() until there are no more buffers.

While this doesn’t belong in the manual, it should be useful for people looking on this page for zend_optimizer.* ini options, which are commonly installed:

Information on the «zend_optimizer.optimization_level» and «zend_optimizer.enable_loader» options is available at:

Careful — in some cases, when setting zlib.output_compression to «On» via ini_set, PHP won’t send the Content-type header and browsers will garble the output. Set it to the desired buffer size instead, which sends the correct header:

Читайте также:  Javascript задачи с ответами

ini_set ( «zlib.output_compression» , 4096 );
?>

[[[Editors note: Just because you’re able to set something
doesn’t mean it will work as expected. Depends on the
setting. For example. setting register_globals at
runtime will be of little use as its job has already
been completed by the time it reaches your script.
]]]

When a setting can not be changed in a user script, the return value of ini_set is «empty», not «false» as you may expect.
If you check in your script for return value is «false» the script will continue processing, although the setting has not been set.
The boolean return value is used for settings that can be changed in a script. Otherwise the empty value is returned.
To test for both error conditions use:

if (empty( $blnResult ) or (! $blnResult )) <
echo «setting cannot be set» ;
exit;
>
?>

if u receive an Error with generic like :
Maximum execution time of 30 seconds exceeded

if u set ini_set(max_execution_time, 300);
your problem will be solved
pls note that the 300 , is 300 seconds , which means 5 minute you can set another value !

Источник

Local php.ini Settings aren’t Working or Loading in Apache

DISCLOSURE: This article may contain affiliate links and any sales made through such links will reward us a small commission, at no extra cost for you. Read more about Affiliate Disclosure here.

Recently while working for a client’s web host, I had the trouble that local php.ini changes weren’t taking effect. I had already created a custom php.ini file in the user’s home directory and set rules. But these local php.ini settings were not working at all.

Apache server wasn’t loading the php.ini configuration file for the user. As a result, the settings were completely ignored. I’ve managed to fix that. If you have the same trouble then here I will tell you how to rectify this.

Local php.ini Settings aren't Working

The Order of Loading php.ini Settings

First of all, you have to identify the working php.ini settings including the local one. In this step by step tutorial, we will first identify which PHP configuration files are loading. By default, PHP directives take effects in the following order.

  • Global or root’s php.ini file that comes with the installed PHP version
  • A file set up by the server control panel software
  • The custom configuration in the user’s home directory
  • Local file in the domain’s document root
  • In the same directory where the PHP file resides
  • Using the ini_set() function at runtime

So here you see that any directive defined in the later file can override the same from the former one. However, there might be multiple PHP versions installed in your hosting account. Further, you might have the capability to specify PHP executable version per domain or website. Refer to this article to know the two ways to perform the same.

Читайте также:  Html span red font

Thus it’s better to know which local as well as master PHP settings files are taking effect. It will also help us to create correct and working configuration files.

Identifying the Effective PHP Configuration Files

It’s time to write and run a line of code. Write phpinfo(); in a PHP file and access this file in the browser. You will come up with the information about Apache and PHP in your system.

The Local Value column displays values from the user’s ini file while Master Values contains values form the global file. Further, the local value displays one from the master if it doesn’t exist in the custom one.

Since Apache is ignoring our PHP ini settings, let us first look at configuration path and loaded files. Search for rows “Configuration File (php.ini) Path” as well as “Loaded Configuration File”. The first one is the file that came with Apache and PHP. While your control panel software (cPanel, Plesk) loads its own php.ini file as shown in the second term.

So you have gotten the file. You can change the necessary directives in the loaded configuration file if have access to it. Otherwise, we should try to load the local php.ini file. Also, not sure but the loaded configuration file might change in the future with updates as well.

Making Local php.ini Settings Working in Apache

Well! It’s not always the php.ini file that loads. PHP has the setting to rename the user-defined configuration file. Look for the directive “user_ini.filename” on the PHP information page in the browser.

Local PHP ini not Loading

Instead of “php.ini”, there might be “.user.ini” provided. it means PHP will look for custom configuration in files named as “.user.ini”. So either you rename local php.ini to .user.ini or set “user_ini.filename” directive to “php.ini”.

Now Apache will load user-specific local ini file for PHP. You can verify the same by reloading PHP information on the browser. Just remember to delete phpinfo(); function or the file from your hosting account afterward.

Additionally, give attention to “user_ini.cache_ttl” directive as well. PHP caches the configuration and avoids re-reading until the cache expires. The value is measured in seconds. 300 seconds means 5 minutes. So either wait or reboot Apache if immediate changes don’t work.

Changing Local or User php.ini to Take Effect

So You have come to know why were PHP settings ignored. Now the local php.ini settings started working after changing the file name. PHP information and configuration are quite helpful utility. I have almost modified more than 25 directives to let Apache and PHP effectively work.

For example, “extension_dir” is one such useful directive. I have set up the PHP extension for Redis using this. Finally, hope you have fixed local php.ini settings aren’t working issue in Apache. If this article is helpful then don’t forget sharing and commenting.

Источник

Оцените статью