Stackify is now BMC. Read theBlog

How to Troubleshoot PHP Web Application Problems

By: jmendez
  |  March 14, 2024
How to Troubleshoot PHP Web Application Problems

Not all problems or issues in web development can be detected during development or testing. There are even web application errors that are hard to catch like runtime errors. Most PHP developers or server administrators will just look at the web server or database logs once an issue arises. They would just grep the logs for errors or timestamps in which the error occurred. This method of troubleshooting PHP problems requires a high technical skill and would take a long time to find the root cause.

PHP version compatibility issues

One of the most common problems a developer will encounter is PHP code that doesn’t work on a different server other than his or her local machine. Deployment servers usually don’t display errors and warnings, which makes it more dumbfounding why certain parts of the application display nothing or incorrect data.

class foo {
    function foo() {
        echo 'I am the constructor';
    }
}

In PHP 4 and 5, a constructor is a method that shares the same name with the class, but starting with PHP 7 this code will throw an E_DEPRECATED message unless a method named __construct() is declared in the same class. PHP 7 and newer versions use the method named __construct() for declaring constructors. Imagine a developer that uses a coding style of PHP 5 and the project is deployed to a PHP 7 environment and on top of that, the errors, notices, warning, etc. are hidden.

In PHP7 there is a compatibility checker tool that is designed to make an easier migration from PHP 5x to PHP 7, which gives you reports on errors and warnings on your code. This is the best way to troubleshoot PHP compatibility issues.

Another solution to this compatibility issue in PHP is by using PhpStorm developed by JetBrains, which is used as an IDE that has the ability to inspect the code.

apt install python-software-properties
add-apt-repository ppa:ondrej/php
apt update
apt show php -a | grep version # list all recent versions of PHP
apt install php7.2 # or whatever php version is the latest
a2dismod php5 # disable the old php version
a2enmod php7.2 # enable the new php version
service apache2 restart

Page loads indefinitely

There are many reasons why a web page loads indefinitely. One reason is a poorly written loop where the loop doesn’t reach the terminating condition.

One way to troubleshoot these types of PHP problems is to trace these is to log the counter of the loop that the terminating condition is based upon. In this way, the developers will be able to see how the counter and terminating condition converge or don’t converge at all.

$x = 100;
while ($x >= 100) {
    $x++;
}

A recursive function can also cause the web application to run forever. You can set a condition, also known as base case, which signifies the recursive call to stop or exit in the program.  The base case must be provided with some return value so that it will not keep on calling itself, which makes the script timeout or exhaust memory.

?php 
function recursive_func (args) { 
    if (base_case) { 
        return value; 
    } 
    else 
    { 
        recursive_func(argument); 
    } 
}

The best way to troubleshoot this is to log some of the crucial variables of the recursive function and log it every time the function is being executed. The logs must include the values used in the terminating condition. Same with the loops: one of the easiest ways to debug these issues is to log some values used in the condition for every step.

I would also suggest passing a number through the recursive functions to track how many times deep the recursion has been called. You can use the number as a failsafe check to know if you should not call the recursion function anymore to prevent it looping.


How to enable errors in PHP

As soon as an issue or problem is reported by another developer, or even worse by a user, then the developer would directly look at the latest web server or database logs. To view the errors in your PHP application, you will need to set the following settings in your PHP page so you can troubleshoot the problems. Knowing how to configure PHP display errors is really important.

ini_set('display_errors', 1); 
ini_set('display_startup_errors', 1); 
error_reporting(E_ALL);

This is usually at the top of a PHP web developer’s code (if not using any framework) during development or during bug fixing. If the web server or PHP is not set to display errors, warnings, notices, etc., then if the web application causes an error, all the user will see is a blank page or a 500 internal server error.

But once the application is already deployed, putting these lines of code might scare the users if they encounter notices, warnings, or even runtime errors.

On production servers, it is better automatically collect all of your PHP application errors via a logging framework or APM solution, like Retrace. This way your users will never see the errors and your developers can be alerted instantly if an exception occurs. Retrace can automatically collect all unhandled PHP exceptions that occur within your PHP applications.

Retrace’s error reporting and tracking solution allows you to track all unique errors so you know if they are new or regressed from a previous fix. This helps you quickly identify new exceptions found after a deployment and view the history of a specific exception.

Example screenshot of Retrace’ error reporting dashboard:

Retrace screenshot of errors

Viewing your PHP Logs

The most common way to troubleshoot PHP problems is by viewing your logs. Implementing good PHP logging best practices is important to ensure that you have the right data being logged and that you can easily access your logging data.

Troubleshooting problems across multiple web applications, hosted on multiple web servers, and written by multiple developers can make this task difficult.

Here are some examples of using the Grep command-line utility to search and filter text. You can even use regular expressions.

grep "string" ~/threads.txt

If you want to search multiple files:

grep -r "string" ~/thread/

But as mentioned, this technique needs an expert in tracing issues, but in case you forget all the commands in grep you can request command help.

 grep --help

Or use this to get more detailed command documentation:

man grep

If your PHP logs are only in text files on your server, accessing them is difficult. Tools like grep make it easier to search for them. The problem is usually even getting access to the server. If you have multiple servers than logging in to each one manually to troubleshoot PHP log files is very time-consuming .

A better solution is to use a PHP logging framework, like Monolog, and send all of your application logs to a centralized log management solution. Let’s discuss that next!


PHP frameworks: Error and logging

PHP frameworks usually have built-in logging and error tracking features. For example, the famous PHP framework Laravel uses the Monolog logging library. Laravel made its logging configuration flexible by letting web developers choose whether the application logs daily or puts everything in a single log file.

'log' => 'single' 
'log' => 'daily' 
'log' => 'syslog' 
'log' => 'errorlog'

In Laravel, logs are stored in the storage/logs directory. Using the Monolog library for logging, developers will have an easy way of tracing their code.

use IlluminateSupportFacadesLog; 
Log::info('To log some information.'); 
Log::warning('To log some warning.'); 
Log::error('Something error.');

Laravel has several logging levels aside from info, warning, and error, and these include debug, notice, critical, and alert.

Log::listen(function($level, $message, $context) { 
    // do something here 
});

Depending on which PHP framework you are using, many of them offer logging capabilities. You can also always use Monolog.


View all your logs in one place

Regardless of which PHP logging framework you are using, one of the benefits of them is being able to send all of your logs to a centralized logging solution like Retrace.

With Retrace, you can view and search all of your logs across every environment, application, server, and docker container. This makes it easy to quickly troubleshoot PHP application problems.

Below is the sample screen capture of Retrace’s log viewer:

image

In Retrace, errors can easily be tracked and monitored. Crucial information such as the time the error occurred and the URL that caused it is logged. The stack trace that causes the issue is also recorded. Based on the number of occurrences of an error, developers and project managers can decide which issue to prioritize.

error detail

Troubleshooting slow requests

In PHP applications the most common problem is slow web requests. Retrace can tell you which web requests are the slowest and get used the most often. Using Retrace’s code profiling and web request tracing, you can identify why your PHP requests are slow and how to optimize them.

requested action

One way to troubleshoot your PHP applications is by investigating SQL query performance. Retrace can identify and determine the most used and slowest SQL queries.

sql query

Summary

Troubleshooting PHP application problems can be very time-consuming. In this article, we covered a couple problems and tips on how to troubleshoot PHP problems with tools like Retrace.

Developers tend to rely on application logging to troubleshoot PHP problems. Good logging best practices are essential to making sure you are logging the appropriate amount of data.

Troubleshooting PHP code has come a long way starting from developers looking at log files manually, to the present day with centralized logging and APM tools like Retrace. Tools like Retrace make it easier to troubleshoot problems quickly with the combination of error reporting, centralized logging, and code level performance insights.

More information

These articles provide additional information about How to troubleshoot PHP Web Application that might be useful for your PHP applications.

Improve Your Code with Retrace APM

Stackify's APM tools are used by thousands of .NET, Java, PHP, Node.js, Python, & Ruby developers all over the world.
Explore Retrace's product features to learn more.

Learn More

Want to contribute to the Stackify blog?

If you would like to be a guest contributor to the Stackify blog please reach out to [email protected]