change PHP configuration settings through ini_set function

PHP configuration settings through ini_set function

In this article we will discuss about changing  PHP configuration settings through ini_set function


ini_set('max_execution_time', '300');//300 seconds = 5 minutes
ini_set('max_input_time', '300');//300 seconds = 5 minutes
ini_set('memory_limit', '128M');
ini_set('post_max_size', '12M');
ini_set('mbstring.internal_encoding', 'utf-8');
ini_set('upload_max_filesize', '12M');
ini_set('register_globals', 0);
ini_set('display_errors', '1');
ini_set('error_reporting', E_ALL);


change PHP configuration settings through php.ini file

PHP configuration settings through php.ini.

In this article we will discuss about changing  PHP configuration settings through php.ini file.

Finding the correct php.ini file :

Locate the php.ini file that is been used by the server. By Using phpinfo() PHP function we can locate the exact php.ini file used. Path of the php.ini file can be found by checking the value assigned to  “Loaded Configuration File” when we run the code that displays output of phpinfo() .

In php.ini file you can change the required settings like below.

Enabling Extensions :

Extensions can be enabled or disabled through php.ini file by added or removing “;” in front of the required extension.

Example : enabling mysql extension

;extension=php_mysql.dll  ( Disabled)

extension=php_mysql.dll ( Enabled )

In this way we can Enable or Disable the Extension as per the requirements.

Changing Settings :

php.ini file also provide the option to change the default setting .

Examples :

max_execution_time = 300; // 5mins
memory_limit = 128M; Maximum amount of memory a script may consume (32MB)
upload_max_filesize = 12M;
short_open_tag = Off
safe_mode = Off
error_reporting = E_ALL & ~E_NOTICE
register_globals = Off
magic_quotes_gpc = On
file_uploads = on

change PHP configuration settings

In this article we will discuss about possible ways of changing PHP configuration settings.

Changing PHP Configuring settings are easy. Mainly there are 3 ways of changing PHP configurations settings. They are
1) Through php.ini file
2) Through .htaccess file
3) Through ini_set() function.

Through php.ini file :

The php.ini file is the main PHP configuration file, and most immediate way to affect PHP’s functionality. The php.ini file is read each time PHP script gets executed. This file consists of all the PHP configuration settings with detailed documentation/commented of each setting.

Please check this article for detailed information about php.ini file

Through .htaccess file :

The majority of ISPs provide Apache web server hosting on Linux or Unix platforms. Hopefully, they’ve also granted “AllowOverride Options” or “AllowOverride All” privileges in Apache’s httpd.conf configuration. This allows you to create an .htaccess file within your application’s root folder overrides the default Apache and PHP configuration.

In .htaccess  usage only two PHP directives are allowed

1) php_flag  ( Used to set on/off values)

Example : php_flag register_globals off

2) php_value  ( Used to set the values)

Example : php_value upload_max_filesize 20M

Don’t use php_value to set boolean values. use php_flag instead.

Please check this article  for detailed information about php.ini file

Through ini_set() function :

Some of the hosting server ( shared hosts ) do not allow the use of php.ini file for modifications of default PHP configuration. In order to modify the global PHP settings, we need to use the ini_set() PHP function.

ini_set PHP function requires two string arguments. First argument always represent setting name and the second argument is the value for the setting.

For example to change  the of Maximum Execution Time of a script through ini_set function, Use the below code.

ini_set(‘max_execution_time’, ‘600’); //600 seconds = 10 minutes

Always use ini_set function at the top of the script.

Settings that are through ini_set() function are applicable only to current script. There after it continues to use the settings from php.ini file.

Please check this article for detailed information about ini_set() function.

Usefull Information :

php_ini modes will plays a key role to suggest the best way of editing the global configuration setting .

There are 4 different types of php_ini_ modes and they are given below :

PHP_INI_USER        (Entry can be set in user scripts (like with ini_set()) or in the Windows registry. Since PHP 5.3, entry can be set in .user.ini)
PHP_INI_PERDIR    (Entry can be set in php.ini, .htaccess, httpd.conf or .user.ini (since PHP 5.3) )
PHP_INI_SYSTEM    (Entry can be set in php.ini or httpd.conf)
PHP_INI_ALL             (Entry can be set anywhere)

To find the php_ini mode of each setting click here