php 警告:输入变量超过 1000
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16470527/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Warning: Input variables exceeded 1000
提问by Fredrik Erlandsson
I'm building a RESTS like service in PHP that should accept a large JSON post as main data (I send and read the data much like discussed here: http://forums.laravel.io/viewtopic.php?id=900)
我正在用 PHP 构建一个类似于 RESTS 的服务,它应该接受一个大的 JSON 帖子作为主要数据(我发送和读取的数据很像这里讨论的:http://forums.laravel.io/viewtopic.php?id=900 )
The problem is that PHP gives me the following Warning:
问题是 PHP 给了我以下警告:
<b>Warning</b>: Unknown: Input variables exceeded 1000. To increase the limit change max_input_vars in php.ini. in <b>Unknown</b> on line <b>0</b><br />
Is there any way to get PHP not count input variables (or should I just surpress startup warnings)?
有没有办法让 PHP 不计算输入变量(或者我应该抑制启动警告)?
采纳答案by Fredrik Erlandsson
I found out that the right way to handle json data directly in PHP (via file_get_contents('php://input')
) is to make sure the request sets the right content-type i.e. Content-type: application/json
in the HTTP request header.
我发现直接在 PHP 中处理 json 数据的正确方法(通过file_get_contents('php://input')
)是确保请求设置正确的内容类型,即Content-type: application/json
在 HTTP 请求标头中。
In my case I'm requesting pages from php using curl with to this code:
在我的情况下,我使用 curl 和以下代码从 php 请求页面:
function curl_post($url, array $post = NULL, array $options = array()) {
$defaults = array(
CURLOPT_POST => 1,
CURLOPT_HEADER => 0,
CURLOPT_URL => $url,
CURLOPT_FRESH_CONNECT => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FORBID_REUSE => 1,
CURLOPT_TIMEOUT => 600
);
if(!is_null($post))
$defaults['CURLOPT_POSTFIELDS'] = http_build_query($post);
$ch = curl_init();
curl_setopt_array($ch, ($options + $defaults));
if(($result = curl_exec($ch)) === false) {
throw new Exception(curl_error($ch) . "\n $url");
}
if(curl_getinfo($ch, CURLINFO_HTTP_CODE) != 200) {
throw new Exception("Curl error: ".
curl_getinfo($ch, CURLINFO_HTTP_CODE) ."\n".$result . "\n");
}
curl_close($ch);
return $result;
}
$curl_result = curl_post(URL, NULL,
array(CURLOPT_HTTPHEADER => array('Content-Type: application/json'),
CURLOPT_POSTFIELDS => json_encode($out))
);
Do note the CURLOPT_HTTPHEADER => array('Content-Type: application/json')
part.
请注意该CURLOPT_HTTPHEADER => array('Content-Type: application/json')
部分。
On the receiving side I'm using the following code:
在接收方,我使用以下代码:
$rawData = file_get_contents('php://input');
$postedJson = json_decode($rawData,true);
if(json_last_error() != JSON_ERROR_NONE) {
error_log('Last JSON error: '. json_last_error().
json_last_error_msg() . PHP_EOL. PHP_EOL,0);
}
Do not change the max_input_vars
variable.Since changing the request to set right headers my issue with max_input_vars
went away. Apparently does not PHP evaluate the post variables with certain Content-type
is set.
不要更改max_input_vars
变量。由于更改请求以设置正确的标头,我的问题max_input_vars
就消失了。显然,PHP 没有评估Content-type
设置了某些帖子的变量。
回答by chugadie
Something is wrong, you do not need 1000 variables. Recode your program to accept 1 variable array with 1000 keys. This is error is there to warn you that you are not doing things the recommended way. Do not disable it, extend it, or hide it in any way.
出了点问题,您不需要 1000 个变量。重新编码您的程序以接受 1 个具有 1000 个键的变量数组。这是一个错误,警告您没有按照推荐的方式做事。不要以任何方式禁用、扩展或隐藏它。
回答by Faridul Khan
Change this
改变这个
; max_input_vars = 1000
To this
对此
max_input_vars = 3000
Comment line in php.inifile is
php.ini文件中的注释行是
;
回答by Mateusz
According to php manualmax_input_vars
in php.ini
role is:
根据php手册max_input_vars
中的php.ini
作用是:
How many input variables may be accepted (limit is applied to
$_GET
,$_POST
and$_COOKIE
superglobal separately). Use of this directive mitigates the possibility of denial of service attacks which use hash collisions. If there are more input variables than specified by this directive, anE_WARNING
is issued, and further input variables are truncated from the request. This limit applies only to each nesting level of a multi-dimensional input array.
多少输入变量可以被接受(限制被施加到
$_GET
,$_POST
和$_COOKIE
分别超全局)。使用此指令可以减少使用哈希冲突的拒绝服务攻击的可能性。如果输入变量多于该指令指定的数量,E_WARNING
则发出an ,并从请求中截断更多输入变量。此限制仅适用于多维输入数组的每个嵌套级别。
You just have to attribute greater number to max_input_vars
in your php.ini
.
你只需要max_input_vars
在你的php.ini
.
回答by Marco
Realy, change max_input_vars
using .htaccess
file is working but you need to restart Apache service.
确实,更改max_input_vars
使用.htaccess
文件是有效的,但您需要重新启动 Apache 服务。
Follow the complete process:
遵循完整的流程:
- Go to
C:\xampp\apache\conf\httpd.conf
- Open file
httpd.conf
- In file look for
xampp/htdocs
- A bit lower, you may see a line like this:
AllowOverride
- If this line show
#
beforeAllowOverride
delete the#
- Next line after
AllowOverride
insertphp_value max_input_vars 10000
- Save the file and close
- Finally STOP the Apache and Restart (It will works just after restart apache)
- 去
C:\xampp\apache\conf\httpd.conf
- 打开文件
httpd.conf
- 在文件中寻找
xampp/htdocs
- 再低一点,你可能会看到这样的一行:
AllowOverride
- 如果此行在删除
#
之前显示AllowOverride
#
AllowOverride
插入后的下一行php_value max_input_vars 10000
- 保存文件并关闭
- 最后停止 Apache 并重新启动(它会在重新启动 apache 后工作)
回答by Haritsinh Gohil
I think most of the time it is not necessary to increase the max_input_vars
size,
我认为大多数时候没有必要增加max_input_vars
大小,
but to optimize your code,
但要优化您的代码,
i had faced this problem when getting all results from one ajax request and sending that results to another ajax request.
从一个ajax请求中获取所有结果并将该结果发送到另一个ajax请求时,我遇到了这个问题。
So what i have done is stringified the arrayfrom the db results,
所以我所做的是从数据库结果中字符串化数组,
JSON.stringify(totalResults);
in javascript JSON.stringify
convert array to stringso after converting i have send that string to another request and decoded that string to array again using json_decode
in php,
在javascript中将JSON.stringify
数组转换为字符串,因此在转换后我将该字符串发送到另一个请求并再次使用json_decode
php将该字符串解码为数组,
<?php $totalResults = json_decode($_POST['totalResults']); ?>
so i got that original array again,
所以我又得到了那个原始数组,
i hope this can help someone so i have shared it, thank you.
我希望这可以帮助某人,所以我分享了它,谢谢。
回答by Dasun Lahiru
Changing max_input_vars in php.ini didn't worked for me
在 php.ini 中更改 max_input_vars 对我不起作用
Change max_input_vars using .htaccess file is working.
使用 .htaccess 文件更改 max_input_vars 正在工作。
If you want to change the “max_input_vars” using .htaccess file then go to C:\xampp\apache\conf\httpd.conf file (in xampp) add the following code in you .htaccess file.
如果您想使用 .htaccess 文件更改“max_input_vars”,请转到 C:\xampp\apache\conf\httpd.conf 文件(在 xampp 中)在您的 .htaccess 文件中添加以下代码。
php_value max_input_vars 10000
php_value max_input_vars 10000