PHP警告删除JSON响应 [英] PHP Warning demolishes JSON response

查看:81
本文介绍了PHP警告删除JSON响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的php应用程序实现ajax文件上传(使用CodeIgniter).

根据post_max_size) ="noreferrer"> http://andrewcurioso.com/2010/06/detecting-file-size-overflow-in-php/并尝试发送适当的JSON编码的错误响应.

但是输出中包含的相应php警告完全破坏了我的JSON响应!

<br />
<b>Warning</b>:  POST Content-Length of 105906405 bytes exceeds the limit of 8388608 bytes in <b>Unknown</b> on line <b>0</b><br />
[{"error":"Posted data is too large. 105906405 bytes exceeds the maximum size of 8388608 bytes."}]

我不想在客户端解析和过滤警告,这看起来很丑. 并且全局禁用所有php警告似乎是不合适的.

我可以在php函数的上下文中禁用特定的PHP警告吗?还是将其包装在有效的json响应中?

解决方案

在生产服务器上:始终禁用php警告.

在开发计算机上: 出于测试目的,请禁用该页面上的所有警告,并在测试完成并确认功能正常后重新启用.

提示:我更喜欢全局设置一个变量,并根据该变量启用或禁用所有php和数据库警告通知.例如,如果使用apache,则可以在.htaccess中进行设置. 我在.htaccess中有这个:

  • 生产:SetEnv DEVELOPMENT Off
  • 发展:SetEnv DEVELOPMENT On

在php代码中:

if( strtolower(getenv('DEVELOPMENT')) == "on" ) {
        ini_set("display_errors",2);
        ini_set('error_reporting', E_ALL | E_STRICT);
}

要禁用.htaccess中的所有错误,请使用

php_flag display_startup_errors off
php_flag display_errors off
php_flag html_errors off
php_value docref_root 0
php_value docref_ext 0

如果您想变得非常棘手,可以添加以下支票:

if(error_get_last() !== null)

并设置一个http标头,然后从javascript中检查json输出和加载页面时是否显示错误日志,或者根据需要查看错误日志或在屏幕上显示错误,我仍然强烈建议您禁用该错误在生产环境中显示代码.

要对此进行更深入的讨论,请参阅本文. /p>

I'm implementing an ajax fileupload for my php application (using CodeIgniter).

I detect if the uploaded POST data is to large (>post_max_size) according to http://andrewcurioso.com/2010/06/detecting-file-size-overflow-in-php/ and try to send an appropriate JSON-encoded error response.

But the corresponding php warning included in the output completely destroys my JSON response !

<br />
<b>Warning</b>:  POST Content-Length of 105906405 bytes exceeds the limit of 8388608 bytes in <b>Unknown</b> on line <b>0</b><br />
[{"error":"Posted data is too large. 105906405 bytes exceeds the maximum size of 8388608 bytes."}]

I don't want to parse and filter the warning out on client side, that seems ugly. And disabling all php warnings globally seems inappropriate.

Can I disable specific PHP warnings in the context of a php function? Or wrap it inside of a valid json response?

解决方案

On production server: Always disable php warnings.

On development machine: Disable all warnings for that page for the purpose of testing and re-enable after testing is complete and functionality confirmed as working.

Hint: I prefer to have a variable globally set and depending on that enable or disable all php and database warning notifications. You could set that in .htaccess for example if you use apache. I have this in .htaccess:

  • production: SetEnv DEVELOPMENT Off
  • development: SetEnv DEVELOPMENT On

And in the php code:

if( strtolower(getenv('DEVELOPMENT')) == "on" ) {
        ini_set("display_errors",2);
        ini_set('error_reporting', E_ALL | E_STRICT);
}

To disable all errors from .htaccess use

php_flag display_startup_errors off
php_flag display_errors off
php_flag html_errors off
php_value docref_root 0
php_value docref_ext 0

If you want to get really tricky you could add a check like:

if(error_get_last() !== null)

and set a http header and check that from javascript both for json output and when loading your page and take a look at the error log or display the error on screen if you prefer that, still I strongly recommend that you disable the error showing code in the production environment.

For more in-depth talk about this take a look at this article.

这篇关于PHP警告删除JSON响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆