Help Desk Software

Debug 500 Internal Server Error in PHP scripts line-by-line

Sometimes your PHP script will have a mind of its own and show a generic "500 Internal Server Error" message in your browser instead of a more descriptive error message.

500 Internal Server Errors can sometimes be tricky to debug because they don't show any detailed information about the error and it can be a hundred things causing it.

Here are the steps we take at Hesk when we try to debug 500 errors in PHP:

  1. turn "Debug mode" on; if the script you are using (and debugging) has a "Debug mode" available, turn it on and try again. In Hesk, you can turn Debug mode on in Admin > Settings > Help desk > under "Features" turn "Debug mode" on and save changes.
     
  2. check PHP error logs; next, we connect to the server and look for any error_log files created in the script's directory. These may contain helpful debug information, such as what exactly went wrong in which file and which line.
     
  3. check server error logs; since "500" is a server-side error, the underlying cause may be found in server error logs (server being the thing that runs your web server, for example, Apache, Nginx, Microsoft IIS ...). Where exactly those are located depends on your setup. If you are using third-party web hosting look into your hosting control panel or contact your host for help. If you run a server yourself, you should know where to look (or Google it).

 

If still no clue...

Sometimes the above steps will give you some pointers, but sometimes you will still be completely in the dark.

If everything fails, here is a simple trick you can use to pinpoint the exact line where the problem occurs:

  1. make a backup of your PHP file (don't skip this, trust me...),
     
  2. download your PHP file to your computer and open it in Notepad++ or some other powerful text editor (do not use Microsoft Notepad),
     
  3. find the very first line starting with:
     
    <?php
     
  4. just after <?php add a space then this code:
     
    die('OK ' . time());
     
    Your code should look like this now:
     
    <?php die('OK ' . time());
     
  5. save the file and upload it to the server
     
  6. open the file again in your browser. You will either see:

    OK (some number here) = continue with the next step below
     
    - OR -
     
    500 Internal Server Error = something is wrong in the server-side configuration. Again, check your server logs, check Mod_security logs (if you have it), and ask your hosting company for help.
     
  7. now it's time for some trial and error.

    Move the die('OK ' . time()); code from the previous place down the file, a couple of lines at a time, testing the ticket page in between.

    For example (in a Hesk file):

    Move
    die('OK ' . time());

    after this line:
    require(HESK_PATH . 'hesk_settings.inc.php');

    Your file will look something similar to:
     
     <?php

    define('IN_SCRIPT',1);
    define('HESK_PATH','./');

    // Get all the required files and functions
    require(HESK_PATH . 'hesk_settings.inc.php');
    die('OK ' . time());

    Then refresh the PHP page in your browser.
     
    Again, you will either see "OK (new number here)" or the 500 Internal Server Error message.

     
  8. Repeat step 7 over and over (each time moving the die('OK ' . time()); code further down the file) until you hit the 500 error, then try to figure out which exact line causes the 500 error.

    For example, you may find out that if you place

    die('OK ' . time());

    just ABOVE line

    $my_email = hesk_getCustomerEmail(1, 't_email', 1);

    you get an OK, but if you place the die( ) code BELOW this line you get the 500 error.

    That will tell you which exact line is causing the 500 error.

  9. Now you have something to work with; you know the exact line number (code) causing the 500 Internal Server Error in your PHP script.

    You can try to figure out the problem yourself (Google it) or find someone (a developer, a support forum, ...) who can give some insight into why that particular line could be causing errors.

    Good luck!

Help Desk Software