« SMTP May Mot Be Necessary | Main | RSS Feed »
July 22, 2005
PHP Debugging
When something strange happens to a third-party PHP script, I often have no idea where to start to fix the problems. I find PHP code debugging is an effective way even the error message may suggest a system configuration problem.
How to find the problem
Turn on register_globals and display_errors in the php_ini file, these two variables are not recommended for production system due to the security concern, though. If this doesn't give enough information, try debugging into the code with echo() or logging function. Some scripts use templates and aren't easy to print the output directly, logging to a file may be necessary in this case.
A simple logging function
function logInfo($msg)
{
$filename = 'log.txt';
$handle = fopen($filename, 'a');
$msg = date("H:i:s")." ".$msg."\r\n";
fwrite($handle, $msg);
fclose($handle);
}
A weird example
A phpBB forum which has been running well suddenly popped up an error message whenever I tried to login: "Connection was refused". I tried to set all file attributes to 777, re-upload all files, but neither worked. I also posted the problem on phpBB's support forum, but didn't get any response. Finally I started to debug into the code with the above logging function to see what really happened. By tracing the script step by step, I found the problem was that I accidentally set the cookie using secure path in the admin control panel, phpBB redirected to https with header() function after successful login, the problem was fixed by modifying the Database record.
Category : PHP
Posted by FreeBSD Newbie at July 22, 2005 03:15 PM
