Tag Archives: PHP

PHP debugging

php-debug.jpgManaging PHP debugging can be tricky as, by default, errors and test ‘echo’ statements can be lost along with all the other output. This problem is compounded when using AJAX, and errors can turn up in XML streams, causing all sorts of problems.

A better solution is to redirect all of that output into an external file, and use a tool like tail to monitor the log.

For printing debug statements, a debug function that shows where each statement came from (file and line number), and is capable of printing complex objects is useful too.

The following is the debugging code that’s included in SiteComponents.

if ( defined("DEBUG_ENABLED") )
   ini_set("error_log", $_SERVER["DOCUMENT_ROOT"]."/debug.log");
   ini_set("error_reporting", E_ALL);
   ini_set("display_errors", false);
   ini_set("log_errors", true);

/** * Define debugging routine */ function SCDEBUG($content="") { if ( !defined("DEBUG_ENABLED") ) return;

if ( !array_key_exists("__SCDEBUG_fd", $GLOBALS) )
   $GLOBALS["__SCDEBUG_fd"] = fopen($_SERVER["DOCUMENT_ROOT"]."/debug.log", "a");
    if ( !$GLOBALS["__SCDEBUG_fd"] ) return;

$bt = debug_backtrace();

if ( is_array($content) || is_object($content) )
   $content = print_r($content, true);

      ($bt[0]["file"] ? (substr($bt[0]["file"], 
            strlen($_SERVER["DOCUMENT_ROOT"])+1).":".$bt[0]["line"]." (") : "").
        ": ".$content."\n");


Also tagged | Comments closed

URLs, the last great ‘what the’

Happily surfing the Microsoft website today (don’t ask), I was amused to note the ridiculous URLs in use – Here are some examples:

  • downloads/details.aspx? FamilyId=4C254E3F-79D5-4012- 8793-D2D180A42DFA &displaylang=en
  • downloads/Browse.aspx? displaylang=en&productID= 4289AE77-4CBA-4A75- 86F3-9FF96F68E491
  • downloads/info.aspx?na=63&p=& SrcDisplayLang=en& SrcCategoryId=&SrcFamilyId= 9996B314-0364-4623-9EDE- 0B5FBB133652&u=%2f genuine%2fdownloads%2f WhyValidate.aspx%3ffamilyid %3d9996B314-0364-4623- 9EDE-0B5FBB133652 %26displaylang%3den

Whatever happened to friendly URLs? If I was going to point someone to an article or a download from Microsoft’s website, I’d need several weeks just to recite it! (I will admit that Apple is no better – take their link to the MacBook Pro on their store site: 80505/ wo/3s3D4l85iljb2mPXrPH2pCTDMcy /0.SLID? nclm=MacBookPro&mco=7C576790)

Crazy long URLs force site users to work through the navigation instead of being able to point each other to pages: Imagine reciting such a URL over the phone – it would never happen – "slash, 3, lowercase s, 3, capital D, 4, lowercase l, 8, 5…". Instead, one would tend to point to, and give directions from there. There is also no way anyone could work out what each URL points to by looking at it. It’s crazy!

In the case of the four URLs above, these really should be something like:

  • Worldwide_English/ActiveSync_4.1

These days, with ‘404 handlers’ and such things in common use (this site uses one!), it really is very easy to make user-friendly URLs. Having a decent URL for a site’s users means they’re more likely to be able to point each other to a site (Keep It Simple, Stupid), and thus more likely to bring more visitors to the site.

URL handlers are very easy to write – using Apache, one just needs a .htaccess file sitting in the webroot, which directs all URLs to a handler page:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /index.php

Then, have a page (in this case, index.php) which processes the URL and provides an appropriate page. SiteComponents has:

// Get URL request
$s = substr($_SERVER["REQUEST_URI"],
strrpos($_SERVER["SCRIPT_NAME"], "/")+1);
// Strip off GET parameters and anchors
if ( strpos($s, "?") !== false )
$s = substr($s, 0, strpos($s, "?"));
if ( strpos($s, "#") !== false )
$s = substr($s, 0, strpos($s, "#"));
// Run site

The ‘Run’ method within $site will then handle the URL, and return an appropriate page (or suggest one if no exact match is found).

Also tagged | Comments closed