【问题标题】:PHP doesn't receive POST if string is too long如果字符串太长,PHP 不会收到 POST
【发布时间】:2016-09-09 05:45:53
【问题描述】:

所以我试图通过 ajax 将字符串发送到 php 脚本,这里是 Ajax

function gVerify(){
    var response = $('#g-recaptcha-response').val();
    console.log(response);
    $.ajax({
        type: 'POST',
        url: 'recaptcha.php',
        data: { response: response},
        success: function(data) {
            if(data == 'true'){
                console.log("Success");
            } else{
                console.log(data);
            }
        }
    });
};

和php

<?php
$ip = $_SERVER['REMOTE_ADDR'];
$response = $_POST['response'];

$url='https://www.google.com/recaptcha/api/siteverify';
$secret = '6LeIxAcTAAAAAGG-vFI1TnRWxMZNFuojJ4WifJWe';

$verifyCaptcha = file_get_contents($url."?secret=".$secret."&response=".$response."&remoteip=".$ip);
$captchaReply = json_decode($verifyCaptcha);

if(isset($captchaReply->success) AND $captchaReply->success == true){
    //Captcha successful
    return 1;
} else {
    //captcha failed
    echo json_encode($response);
}
?>

问题是 php 变量 $response 如果值太长,则不会接收到 post 值。我尝试手动发送字母数字字符串,如果我发送 1000 个字符,它不会接收,如果我发送 500 个字符,变量会接收数据,我会通过console.log(data); 在控制台中返回结果,所以我知道其他一切正常.那么这个地方有大小限制吗?

【问题讨论】:

标签: php ajax


【解决方案1】:

在 php.ini 文件中设置了 post_max_size

您需要在此文件中增加该限制。

请看这里:http://php.net/manual/en/ini.core.php

【讨论】:

  • 对其他人也一样:发送数据的默认限制是8M或8000万字符,我只是发送900左右,我不可能达到限制
【解决方案2】:

最大尺寸在服务器中配置。如果您使用.htaccess 文件,您可以通过以下编码来修改值:

#set max post size
php_value post_max_size 20M

瓦拉

【讨论】:

    【解决方案3】:

    我通过在发送的数据中添加JSON.stringify() 解决了这个问题。所以代码现在看起来像这样:

    function gVerify(){
        var response = $('#g-recaptcha-response').val();
        $.ajax({
            type: 'POST',
            url: 'recaptcha.php',
            data: { response: JSON.stringify(response) },
            success: function(data) {
                if(data == 1){
                    console.log("Success");
                } else{
                    console.log(data);
                }
            }
        });
    };
    

    这在我在 php 中删除的令牌周围添加了引号,但至少我得到了完整的令牌。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-04
      • 1970-01-01
      • 2016-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-21
      相关资源
      最近更新 更多