How to Set Individual PHP Configuration Settings

PHP



In this article, we show how to set individual PHP configuration settings.

One way to set individual PHP configuration settings is manually by opening up the php.ini file and changing the values of whatever configuration settings that you want. This allows you to change the PHP configuration settings permanently and systemically throughout your whole website, so that it affects all pages of your website.

The other way of setting the PHP configuration settings is through PHP code in a script. This is a temporary change to the PHP configuration settings that is only valid for the script at hand. In other words, it doesn't affect the website systemically (all the pages on a site). It only changes configurations for the specific script that has the changes in code. This is for times when you don't want to change a configuration for the whole website but for simply one page on a website. There are many reasons why you would want to do this. One example is you want to increase the size of file uploads for a movie upload script but not other other pages where users can upload files (since you want those pages limited).

PHP has a function for setting PHP configuration settings. This is the ini_set() function.

Inside of this function, you pass in the specific configuration setting that you want to set and the value that you want to set that configuration setting to. So the first parameter is the configuration setting that you want to change and the second parameter is the value that you want to change it to.

So to set the value of the max_execution_time configuration setting, the following code below is used.



So the code above sets the configuration setting, max_execution_time, to 60. By default, most servers have this setting set to a value of 30. The maximum execution time is the maximum amount of time given to run a PHP file. If the file takes longer than the time in the max_execution_time setting, then the file times out and doesn't complete processing. So it's the maximum amount of time given to a PHP script to completely run. If it's not done completion within that time, then the server clocks out. The server clocks the file out.

The beauty about using PHP code to set a configuration file is that whatever you set the value to in a PHP script is just set for that script. So, above, we set the maximum execution to 60. It's only for this PHP script that it becomes 60. After, it reverts back to its regular php.ini value. So it's not a permanent setting change. It's just for the script on hand. It's really amazing diversity that you have. In that, writing a change in a PHP script that doesn't affect the whole website. It's just for that script. There's many reasons why you may need to change a configuration setting just for a certain script. For example, say that you have a PHP script where a user uploads a very big file such as gigahertz of memory. By default, the maximum upload size for a file is about 32MB, which definitely would not suffice for large files such as video movies. So you would want to change the maximum upload size to a much larger value such as 4000MB or whatever. And the amazing thing is it's just for this script. You may not want to increase the file upload size throughout the rest of the website. You may want to keep those file upload sizes, as they are. This is the beauty of using PHP code to set PHP configuration settings. You can keep it just to one script.

And there's multiple PHP configuration settings you can set in this manner. All you have to is use the PHP ini_set() function. Pass in the configuration you want to change as the first parameter and the value you want to set it to as the second parameter. And that's all there is to it.


Related Resources

How to Read Individual PHP Configuration Settings




HTML Comment Box is loading comments...