ini_set not working in the same script - php

I am using a prestashop and I need to change the max_input_vars so translation changes are applied from the Translation section.
I cannot use php_value in .htaccess because of my hosting provider policy, so I need to use the php.ini file.
I've created it into the prestashop administration folder with the content:
max_input_vars = 3000
But when I update the translations, I get the error of max_input_vars, so it is not being changed with the php.ini. I've also tried without blanks, with a trailing ";" and no effect.
To check it, I've created a file with just phpinfo() and it tells me that it is set to 1000 (the default value).
I supposed that using ini_set just above the phpinfo call would change its value (only for that script), but in fact, it does not change anything.
So, I put:
ini_set("max_input_vars","3000");
phpinfo();
and the value I am getting for max_input_vars is 1000.
Any idea on how to change the value, or why it is not working?

This configuration setting is not settable at runtime from code. If you look at the list of configuration settings you will see that its "changeable" value is PHP_INI_PERDIR. This page explains what that means.
ini_set can only affect settings that are marked changeable as PHP_INI_USER.
The logical explanation for this is that the setting affects PHP's behavior before your code has had a chance to run.

The setting applies while PHP is trying to parse the data sent to the script. This happens before your script is even executed. So you simply cannot set it at runtime because the setting affects something pre-runtime.

Related

Increase PHP max_input_vars via web.config in IIS

I have a simple wordpress web site under a windows hosting, I know that a Linux environment is better for wp but I need windows for another ASP.NET application. Anyway wordpress show me an error message about max_input_vars set to 1000 and this value should be 4000, I can't edit the php.ini then I would like to know if is it possible to set this value in the web.config, I know that in Apache it can be set in the .htaccess so I hope that can be done in the web.config as well.
Thanks in advice!
EDIT
I have found a solution: I create a php.ini file with max_input_vars=4000 and I added it to wp-admin folder after that the error message disappear and checking the environment setting by PHP Settings wordpress plugin the max_input_vars value is 4000, I don't know if this is a best practice, but works, if someone has a better solution please explain it.
Thanks!
I know it's been a long time but i thought to post a solution for anyone who search about this issue.
Go to IIS and then from the right side there is an option to stop the server.. click on it.
Go to you default programs folder. Probably C: drive (C:\Program Files (x86)) then search for PHP folder.. probably you will find more than a version. choose the one you're using in wordpress.
Inside that folder you will find a file (php.ini) edit that file using notepad (you must start notepad using admin privilege).
It is a big file.. so search for (max_input_vars = 1000) and it is commented. so you have to remove the semicolon before the word max and then increase it to 4000. You may also search for memory_limit and increase it to 128m or more.
Save the file and make sure it is saved properly. try to open it again and make sure your changes are saved.
Return back to IIS and start the server.
Close the browser and open it again. Check your wordpress if the issue is cleared.
Good luck.
There are four modes in which the PHP directives can be set:
PHP_INI_USER
PHP_INI_PERDIR
PHP_INI_SYSTEM
PHP_INI_ALL
Please have a look at how these are managed: http://www.php.net/manual/en/configuration.changes.modes.php
Each directive has one of these changeability modes defined and for that you could visit: http://www.php.net/manual/en/ini.list.php
As you'd see, the changeability attribute for directive max_input_vars is PHP_INI_PERDIR, meaning it can be overridden by adding a custom value in either of php.ini, .htaccess, httpd.conf (Apache) or .user.ini. Therefore, defining a new php.ini file to increase the limit of max_input_vars is just as fine as the other three workarounds.
Please remember though that, at times, some of the extensions that PHP would use might need to be re-enabled in custom ini files. For instance, I've at times run into problems with pdo and mysqli being considered as disabled, unless explicitly set in the custom file too, when using an override ini file.

php ini settings if done from inside php code and other from php.ini file , which one will override the other?

If some configuration parameters of php are set from both the modes i.e from inside the php code and from an php.ini file .
I wanna know which one will be overriding the other to take effect. and also please tell me if the php.ini configuration is applied in the sub folders or not.
If you mean ini_set then ini_set will take precedence over the configuration file.
See ini_set.
Note that some values can't be set in this way due to ini_set restrictions or server configuration. ini_set will return FALSE if it did not set the value (or, presumably, if the value was previously false).

Is it possible for a host to lock a php ini settings?

I'm working on a website which requires the use of sessions. However the default value for the life of a session is 1440 seconds or 24 mins. I have tried to change this using ini_set() making sure to put the ini_set() before the session_start() and then checking it with ini_get(). The ini_get() returns the value I have set but the session still seems to follow the default rules set.
So is it possible for a host to lock out editing of the ini settings?
Short answer: possibly.
It could be that they've disabled use of ini_set via the disable_functions directive in the global php.ini, however that doesn't explain the behavior you're seeing; the documentation isn't clear, but I'd expect it to throw an error if you called a disabled function, nor does it explain why ini_get returns what you've previously set. It's also entirely possible they're running their own patched version of PHP that alters this behavior. Not unheard of, but unlikely.
Things to try:
setting it with session_set_cookie_params instead (needs to be before session_start)
double checking that you're setting the correct param (session.cookie_lifetime) and that your ini_set is definitely placed before session_start, or that you don't have another session_start somewhere else
The awser is yes.
The php agent has 3 scopes.
1 System
This one effects all applications. Its the global.ini file. They can
also disabling overwrites.
2 Perdir
This is your php.ini file. It only effects your directories. Here can
you disable overwrites for the script scope and make overwrites in the
system scope when allowed
3 Script
This is the ini_set scope. It can overwrite the perdir & system
scope when allowed
So is it possible for a host to lock out editing of the ini settings?
Yes they can disable overwrites in there system scope.

ini_set doesn't overwrite php settings

I am trying upload larger files so I set this in php code
ini_set('upload_max_filesize', '1000M');
ini_set('post_max_size', '1100M');
ini_set('memory_limit', '1200M');
but $_POST and $_FILES are empty. But when I change php.ini settings it works. Why it doesn't change setting on the fly?
It's too late to have those settings changed after PHP has started.
upload_max_filesize - uploads happen before the PHP script runs and your ini_set() is executed
post_max_size - likewise
And even if you could change memory_limit (depends on server configuration), it would be too late again if you expect that big of an upload.
See ini_set() and http://www.php.net/manual/en/ini.list.php for more info.
Have a look at these :
http://www.php.net/manual/en/ini.list.php
http://www.php.net/manual/en/configuration.changes.modes.php
After reading, it seems that the only option that you can modify with ini_set is memory_limit.
The others 2 being configurable in the php.ini file, in httpd.conf, .htaccess, or in a per directory .user.ini file.
Meaning, if you want to change these values, you must have access to your server's configuration ! You won't be able to afford that from within the script !
The first two settings are not allowed to be changed per script, but even if they were it still would not be effective since the files/POST data must be handled before the script is even run, which means that the settings wouldn't have a chance to take effect.

cannot change php max_memory in php.ini

I am trying to change the max_memory parameter so I went into my php.ini and set max_memory = 500M For some reason, when I use phpinfo(), that setting still displays 128M (the default setting). I read that some hosts restrict max_memory. Is there a way to change this value manually, or override this option in a config file, or do I have to recompile php?
If you can recompile PHP, then you, obviously, have full control of the web server.
I would guess, as written in the comments, you:
Did not restart the Apache (or not the right Apache)
(as you found) You are editing the wrong ini file.
Your server simply don't have 500MB free memory
a few other things to check:
the report from phpinfo states that it is using the php.ini file that you're editing, not some more "specific" .ini file somewhere else
you haven't overridden the memory_limit variable through an apache .htaccess file or using ini_set in a PHP script somewhere
if you're in a shared environment, as Itay suggests, you may not have more than 128M available to your account, and you probably don't have access to reset apache either.
if that's the case, try making the value smaller to see if the changes you're making take effect. If they are, and you can't increase it above 128M, time to upgrade your hosting account.. otherwise, try making the change through .htaccess or ini_set in your script because those methods don't require an apache restart.
if you still can't get it to work, contact the host and ask them to make you a local copy of the global php.ini file for your account.

Categories