如何获得有关错误500的详细信息 - 内部服务器错误?错误、详细信息、如何获得、服务器

2023-09-10 13:49:29 作者:此人危險、請勿靠近

我用我的PHP的网站都很多地方 $ AJAX 要求是正常使用,直到前几天我所有的 $ AJAX 请求开始给错误500 - 内部服务器错误。 我可以在控制台中看到的错误,也是我用错误处理程序,以获取有关错误的详细信息。 这是我的Ajax请求

I use $ajax requests in many parts of my PHP website everything was working perfectly until few days ago all my $ajax requests start giving error 500 - internal server error. I can see that error in the console and also I used error handler to get more information about the error. That is my Ajax request

window.setInterval(function(){
         $.ajax({
            type: "GET",
            url: "include-ajax/is_it_midnight.php",
            dataType: "json",
            success: function(data)
            {
                if(data == 1)
                {
                    //Reload website at midnight
                    location.reload();
                }   
            },
                    error : function(XMLHttpRequest, textStatus, errorThrown) {
                        alert(XMLHttpRequest);
                        alert(textStatus);
                        alert(errorThrown);
                        alert(XMLHttpRequest.responseText);
                    }
        }); 

    }, 10000);

这是我得到我的浏览器:

that is what i get on my browser:

is_it_midnight.php:

is_it_midnight.php:

<?php
$current_hour = date('H');
$current_minute = date('i');
$current_second = date('s');
//If the website was open between the 00:00:00 and 00:00:10 it will refresh automatically
//else it will not be open it will open after midnight so it will have aleardy the new day data 
if($current_hour == 0 && $current_minute == 0 && ($current_second > 0 && $current_second < 10))
{
    $is_it_midnight = 1;//This flag means it is midnight
}
else
{
    $is_it_midnight = 2;//This flag means it is not midnight
}
echo json_encode($is_it_midnight);
?>

一些注意事项:  1.它是不是给这个错误所有的时间,有时它工作正常,并把正确的反应(我看到网站上的反应和标题信息,当我检查网络选项卡中的铬控制台)。  2.不要紧,如果类型为GET或POST它不断给我同样的问题(我显示此阿贾克斯例如使用GET类型,但我也POST类型的请求在我的网站同样的问题)。  3.我添加ini_set('display_errors',1);``ini_set('display_startup_errors',1);``error_reporting(-1);到开始时的 is_it_midnight.php ,但没有任何错误显示,因为我相信没有语法或任何其他PHP错误(因为有时它工作正常和正确)。  4.我也检查服务器错误日志但并没有什么与此有关的文件或任何其他Ajax的文件都没有。  5.我试图检查,如果这是一个超时错误,但我没有得到任何暂停 textStatus 它只是警告错误

Some Notes: 1. It is not giving this error all the time sometimes it works fine and bring the response correctly (I see the response and header information on the website and when I check network tab in the chrome console). 2. it does not matter if the type is GET OR POST it keeps giving me the same problem (I show this ajax example with GET type but I have also POST type requests in my website with the same problem). 3. I add the ini_set('display_errors',1);``ini_set('display_startup_errors',1);``error_reporting(-1); to the start of the is_it_midnight.php but no errors showed because i believe there is no syntax or any other php errors (because sometimes it works fine and correctly). 4. I check also the server error log but there is nothing related to this files or any other ajax file at all. 5. I tried to check if this is a timeout error but I did not get any timeout from textStatus it just alert error.

更新: 我检查Apache日志,我发现这样的事情: [周六02月21日7点35分05秒2015年] [错误] [客户176.40.150.195]脚本头的premature结束:is_it_midnight.php,引用者:http://www.example.com/index.php

UPDATE : I checked apache log and I found something like this: [Sat Feb 21 07:35:05 2015] [error] [client 176.40.150.195] Premature end of script headers: is_it_midnight.php, referer: http://www.example.com/index.php

我需要任何有益的帮助或线索,了解为什么我得到这个错误有什么我没有尝试,以获取有关该错误的详细信息???

I need any useful help or clue to understand why do I get this error is there anything I did not try it to get more information about that error???

推荐答案

首先,你必须肯定的是,错误日志机制,它配置好。

First of all you must be sure that the error log mechanism it's well configured.

要做到这一点添加下面的code或类似的:

To do that add the following code or similar:

ini_set("log_errors", 1);
ini_set("error_log", "/tmp/php-error.log");
error_log( "Php Errors!" );

这是我注意到那就是你的PHP脚本,它没有返回100%有效的JSON,我也认为你能重构code返回,而不需要对结果产生任何变量中的其他问题(性能更好,速度更快,酒糟code)避免任何类型的内存分配问题。

An other issue that I notice it's that your php script it's not returning 100% valid JSON and I also think that you can refactor the code to return the result without the necessity to create any variable (better performance, faster, lees code) avoiding any kind of memory allocations problems.

在某些情况下非常负载共享服务器或不可扩展的VPS可以尝试非常低的内存,并且可以随机生成的错误时,试图分配内存变量。

In some cases very loaded shared servers or unscalable VPS can experiment very low memory and can randomly generate errors when try to allocate memory for variables.

我不知道,如果你使用某种框架或没有,但以不同的方式来写纯PHP脚本可以是这样的:

I don't know if you use some kind of Framework or not but a different way to write your script in pure php can be something like this:

header('Content-Type: application/json');
return (date('H') == 0 AND date('i') == 0 AND (date('s') > 0 AND date('s') < 10)) ? json_encode(['res' => 1]) : json_encode(['res' => 2]);

这code总是返回有效的JSON格式如 {资源:1} {资源:2} 键,您可以轻松地访问该槽JS这样的水库= data.res;执行console.log(RES); ,你会评估此类似这样:(data.res === 1)

This code will always return valid JSON format like {"res":1} or {"res":2} and you can easily access this trough JS like this res = data.res; console.log(res); and you'll evaluate this like this: (data.res === 1).

在你的情况,你正在使用数据类型:JSON,这在几乎所有情况下,它不是沃金好,如果你的php它不返回有效的JSON。也许你认为删除它,这不是很重要的位置,如果你不enferced期望唯一有效的JSON。

In your case, you are using dataType: "json" and this in almost all cases it's not woking well if your php it's not returning valid JSON. Maybe you consider to remove it, it's not very important here if you are not enferced to expect only valid JSON.

您可以尝试一下,看看发生了什么。 如果错误仍然存​​在,你可以看到它的 /tmp/php-error.log 文件中。并与您的托管公司评论它。

You can give it a try and see what is happening. If the error persist you can see it inside the /tmp/php-error.log file. and comment it with your hosting company.

作为一个括号,我想补充一点,你的脚本应该返回1或0,真的还是假的,而不是1或2。这只是一个更加务实的方式来DOIT。

As a parentheses, I would like to add that your script should return 1 or 0, true or false instead of 1 or 2. It's just a more pragmatic way to doit.

让我知道,如果我的回答是有帮助的,或者如果你不能修复这个问题,我会尽力帮助你。

Let me know if my answer was helpful or if you can't fix the problem and I will try to help you.

再见! Suerte!

Bye Bye! Suerte!