如何在服务器端调试 php 脚本?在某种情况下客户端页面不显示响应
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3448655/
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
How to debug php scripts at server side? In a situation client page doesn't show response
提问by Edward
I'm currently testing a script on my host. This script is to accept PayPal IPN data, do a couple of things (ie. data verification), send necessary data to a specific email address, and more.
我目前正在我的主机上测试一个脚本。此脚本用于接受 PayPal IPN 数据、执行一些操作(即数据验证)、将必要数据发送到特定电子邮件地址等。
I'm using a test tool on the PayPal site to post test data to my script. My script doesn't work: there should be a code issued but I can't see the error. The test tool in PayPal just does the post and doesn't seem to accept any response. I want to see what happened in my script so that I can fix it. How can I get error messages my script outputs?
我正在使用 PayPal 站点上的测试工具将测试数据发布到我的脚本中。我的脚本不起作用:应该发出代码,但我看不到错误。PayPal 中的测试工具只是发帖,似乎不接受任何回复。我想看看我的脚本中发生了什么,以便我可以修复它。如何获取脚本输出的错误消息?
Any ideas?
有任何想法吗?
采纳答案by David Goodwin
A better approach would be to log the errors to a file, and not the 'screen'. If you make a server wide change, it's probable that other sites on the server will be affected.
更好的方法是将错误记录到文件中,而不是“屏幕”。如果您在服务器范围内进行更改,则服务器上的其他站点可能会受到影响。
- log_errors = On
- display_errors = Off
- error_log = /tmp/php.log (or a better path of your choosing; file probably needs to be writable by Apache/webserver).
- log_errors = 开
- display_errors = 关闭
- error_log = /tmp/php.log(或您选择的更好的路径;文件可能需要由 Apache/webserver 写入)。
Failing all of the above, install xdebugand try debugging the code - you can step through line by line and inspect variables at will - perhaps this will give you a better idea.
如果上述所有操作均失败,请安装xdebug并尝试调试代码 - 您可以逐行逐步执行并随意检查变量 - 也许这会给您一个更好的主意。
Failing that, resort to var_dump() and friends.
如果失败,请求助于 var_dump() 和朋友。
回答by AlexV
Not sure if you tested this already (put at the very beginning of your PHP script):
不确定您是否已经对此进行了测试(放在 PHP 脚本的最开头):
error_reporting(-1);
ini_set('display_errors', 'On');
if it's not showing, try it with an .htaccess (if on Apache):
如果它没有显示,请尝试使用 .htaccess(如果在 Apache 上):
php_flag display_errors on
php_value error_reporting -1
with this you should get your error. If not, try your server logs or if it's still don't help, try to echo
some control values to find where it crash...
有了这个,你应该得到你的错误。如果没有,请尝试您的服务器日志,或者如果它仍然没有帮助,请尝试echo
一些控制值以查找崩溃的位置...
Of course this is only for debugging purpose and you should turn that OFF when in production.
当然,这仅用于调试目的,您应该在生产时将其关闭。
回答by Knowledge Craving
In these sort of cases, you should see what the $_POST
array is printing in the console. Try using the following code at the very beginning of your page, after initializing with all the required configurations:-
在这种情况下,您应该$_POST
在控制台中看到数组正在打印的内容。在使用所有必需的配置进行初始化后,尝试在页面的开头使用以下代码:-
<?php
// include all required configurations
// if needed, include the header file also
echo '<pre>';
print_r($_POST); // for viewing it as an array
var_dump($_POST); // for viewing all info of the array
echo '</pre>';
die();
// other HTML or PHP Logic follows
?>
If this does not work, then you will need to put some breakpoints, by putting some "exit
" statements after some code every now & then, and slowly execute as much code as possible in a step-by-step manner.
如果这不起作用,那么您将需要放置一些断点,通过不时地exit
在一些代码之后放置一些“ ”语句,并以逐步的方式慢慢执行尽可能多的代码。
Hope it helps.
希望能帮助到你。
回答by Michael Chourdakis
You can use a (posibly local) web server which includes, say, PHP along with xdebug. Some pre-installations like UwAmpor USBWebServerwill do, if debugging in Windows.
您可以使用(可能是本地的)Web 服务器,其中包括 PHP 和xdebug。如果在 Windows 中调试,一些预安装(如UwAmp或USBWebServer)就可以了。
回答by Ramuns Usovs
use log files and then read them.
使用日志文件,然后读取它们。
Basically at the start of the script open a file then at various points in code dump some variables to that file, then you can go through that file and examine the output and in this way you can see what is wrong with your script
基本上在脚本开始时打开一个文件,然后在代码中的各个点将一些变量转储到该文件中,然后您可以浏览该文件并检查输出,这样您就可以看到您的脚本有什么问题