Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get more information about error 500 - internal server error?

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:

enter image description here

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);
?>

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.

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???

like image 664
Basel Avatar asked Feb 17 '15 21:02

Basel


People also ask

Why do I keep getting 500 internal server error?

The HTTP status code 500 is a generic error response. It means that the server encountered an unexpected condition that prevented it from fulfilling the request. This error is usually returned by the server when no other error code is suitable.

Is a 500 error my fault?

If you try to visit a website and see a “500 Internal Server Error” message, it means something has gone wrong with the website. This isn't a problem with your browser, your computer, or your internet connection. It's a problem with the site you're trying to visit.


2 Answers

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

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!" );

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.

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.

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]);

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).

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.

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.

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.

Bye Bye! Suerte!

like image 162
vitaminasweb Avatar answered Sep 20 '22 01:09

vitaminasweb


My guess is that the PHP process is killed by the server due to some (mis)configuration on your host (I suspect that mod_fastcgi/mod_fcgid is used; you can check this with phpinfo()). Also, you execute the call on each 10 seconds which could hit some limit. What I would do in your case:

  • Check if PHP works with mod_fastcgi/mod_fcgid (Server API field in phpinfo()).
  • Check the Apache access_log and see if a pattern exists for 500 status code for script is_it_midnight.php.
  • Try to increase the setInterval refresh time to 15s, 20s, 25s, 30s and etc. to see if there is any improvement.
  • Place error_log (memory_get_usage()); before echo json_encode($is_it_midnight); to see what is the memory usage allocated to the script (although it is very unlikely that the memory usage is a problem given the small script).
like image 24
VolenD Avatar answered Sep 19 '22 01:09

VolenD