Assuming you have shell access to the webserver (or at least some kind of file access) you can add a small output function at the top of the page (in the global scope):
<?php
function debug_out($msg) {
$f = fopen('/tmp/debug.log', 'a+');
if (!$f) {
trigger_error('Cannot open debug log.', E_USER_ERROR);
return;
}
fputs($f, "$msg\n");
fclose($f);
}
// rest of the PHP page below this
...
Then sprinkle the code with debug_out()
calls
...
debug_out($some_variable);
...
so you can see the value of $some_variable
in the output file. Add some unique identifiers to avoid confusion about which statement created the output line:
...
debug_out("\$some_variable, line 42: $some_variable");
...
Use JSON for displaying complex data structures:
...
$json = json_encode($some_variable, JSON_PARTIAL_OUTPUT_ON_ERROR);
debug_out('$some_variable, line 42: ' . ($json ? $json : json_last_error_msg()));
...
or, probably better, use print_r()
:
...
debug_out('$some_variable, line 42: ' . print_r($some_variable, true));
...
Note that for the latter the second parameter must be set to true
, otherwise the data will be printed (i.e. shown on the page) instead of being returned and logged.
If you have shell access, /tmp
is a good location for a temporary debug log file, since the directory is usually world-writable. If you don't have shell access you need to find a location where the web server can create files that you then can download for inspection.