A few days ago I had the case that a friend had contacted me and told me about his website, which did not work anymore. I looked at the website and saw that it was a 500 error.
An HTTP 500 error means that the server has fainted without being able to testify. That’s why we have to slowly approach and analyze the code (which we did not write ourselves).
The HTTP 500 Internal Server Error status code indicates that an unexpected circumstance prevented the server from fulfilling the request sent to it.
MDN web docs – 500 Internal Server Error
Fumble
So that I could slowly approach, I first had to comment out the whole code. This means that nothing is done at the beginning.
PHP supports ‘C’, ‘C ++’ and Unix shell-like (Perl-like) comments.
php.net manual comments
Subsequently, more and more lines of code are gradually released and looked every time, whether the page has crashed or continues to run. If the page crashes, you can refine the search more and more.
In this case, I had to use the same technique in three files because they were loaded by require.
require is essentially include, but throws an E_COMPILE_ERROR error in case of an error. It thus terminates the program execution while include generates only a warning (E_WARNING) and thus allows further program execution.
php.net manual – require
Found a mistake
I kept going through this process until I found the first line of code that caused the 500 error:
mysql_connect(DB_HOST, DB_USER, DB_PASS) or die('Connection to database failed: '.mysql_error());
At first glance, everything looks fine, but when I looked at the php.net documentation, things became clearer to me. You can not be so overbearing and think that you do not need the php.net documentation. There are always news about different extensions and functions displayed. In this case, for example:
Warning
php.net manual mysql_connect
This extension has been deprecated since PHP 5.5.0 and has been removed in PHP 7.0.0 . Use the extensions MySQLi or PDO_MySQL instead.
What does that tell us?
Many hosters are switching their servers from php-5 to php-7 for security reasons. Lately, I have had at least 20 websites whose vendors have switched to php-7. From my own hoster, I have received at least 10 emails at regular intervals, which warned me before the change and have asked me to prepare my website accordingly.
What I want to get out of is that the warnings were ignored here. Here it is clear that the contact person of the provider has apparently ignored these mails and is now facing a server error.
The solution
In the end I just had to update the code and make sure that the website works with a php-7 server. For this, only MySQL queries had to be updated in 50 places.
Conclusion
If you also notice that a website displays such an error, you can do nothing but slowly grope your way and eliminate the possible sources of error.
In such cases, it does not help if you try to solve the problem quickly, because the chances of overlooking obvious mistakes are far too high.