Tagged: php

Flash for mac is bad for your HTTP_HOST

I’ve been testing the super awesome swfupload code for some projects at work. I have nice progress bars working and you can queue up multiple files. It’s sweet. I tested out my code in Opera, Firefox, Safari 3, IE 7, etc on Windows and Linux. Working great.

Of course, an hour before an important meeting is when we discover it doesn’t work on any mac browser. The flash debug output shows our site initialization code is just throwing a fit. No DB access settings can be found. Doesn’t make any sense why an HTTP request from flash on a mac would break our app.

Here’s the basics of how our our setup switches dev/production settings:

if ($_SERVER['HTTP_HOST'] == 'example.mydesktop') {
    // LOCAL DEV SETTINGS
} else if ($_SERVER['HTTP_HOST'] == 'example.com') {
    // PRODUCTION SITE SETTINGS
}

It makes considerations for www. etc. The problem in this case is that flash is adding :80 to the HTTP Host header. Instead of

GET /path/to/file HTTP/1.1
Host: example.com

Flash for mac likes to add

GET /path/to/file HTTP/1.1
Host: example.com:80

This will cause ‘example.com:80’ to show up in the php $_SERVER[‘HTTP_HOST’] variable. You can see how this would break simple host detection.

Written by Comments Off on Flash for mac is bad for your HTTP_HOST Posted in General Tagged with ,

Extending PEAR’s DB class

This took me quite a while to figure out, so I probably should write it down. My basic goal was to have global db error handling by overloading the query method. Because of the inheritance heirarchy in the DB classes this was tough to nail down at first. Using the DB class in it’s vanila form looks like this:

require_once('DB.php');
$db =& DB::connect("mysql://user:pass@host/dbname");
$sql = "SELECT * FROM foo";
$query = $db->query($sql);
$result = $query->fetchRow();

Which is a slightly different API than the DB class I was using at ResTek for a long time:

require_once('DB.php');
$db = new db('database');
$sql = "SELECT * FROM foo";
$query = $db->query($sql);
$result = $db->fetch_assoc($query);

It was a little annoying to rewrite code / retrain my brain for PEAR’s API, but it makes things easier.

Now when you call DB::connect, you do not get a DB object back but a DB_mysql (or whatever DB you specified). I tried making my own MY_DB class to return the right objects. Disaster. I was successful with copying and renaming the classes DB, DB_common, and DB_mysql. Now I was able to insert my own code in the DB_mysql::query method. Sweet.

I eventually trimmed it down to just extending the DB_mysql class. Here’s a sniplet that works very well for me:

class DB_MIKE_mysql extends DB_mysql {

    function &query($sql) {

        $query =& DB_mysql::query($sql);

        if ($this->isError($query)) {

            /* custom error code here */

        }

        return $query;
    }
}

$db =& DB::connect("MIKE_mysql://user:pass@host/dbname");

The DB::connect function loads a class called DB_$WHATS_IN_THE_DSN, so getting the custom class name right was the tricky part. PEAR seems to like this on php4 and php5.

Written by Comments Off on Extending PEAR’s DB class Posted in Uncategorized Tagged with