PHP’s mysql_connect() Reuses Connections by Default

As I mentioned yesterday, I’m doing some work in WordPress right now, and a few minutes ago I tweeted that my custom code is messing with WP’s wp_get_archives() and wp_list_categories() functions, well, I found the problem.

I am including the 4RoadService.com header & utilities files in my WordPress theme, and I am using the same user here on my test server for both the main 4RoadService.com database and the WordPress database. It turns out that when the 4RoadService.com database connection was initialized, since it uses the same connection info as the WordPress database, the existing connection was just reused, (this behaviour, by the way, is well described in the PHP documentation), then when the 4RoadService.com connection was told to use the main 4RoadService database it did, thus switching our one and only connection away from the WordPress database, and making WordPress think that there were no posts on the blog.

Fortunately, there is a quick workaround, just add one more attribute to the mysql_connect() function so it looks like this:

$dblink = mysql_connect($host, $user, $pass, true);

This way a new connection is established, and the WordPress connection is left alone.

I am left wondering why, in the loop, WordPress was able to see my posts, perhaps it establishes a second database connection in there. However, I’m not going to spend the afternoon poking through the guts of WordPress.

Endpoints: A little secret for URL manipulation in WordPress

Today I’ve been setting up WordPress as the News section of a website which loads its pages via AJAX requests whenever possible, but falls back on normal HTTP requests when the AJAX loads are not possible.

The when the AJAX requests are initiated from Javascript, /outputxml/ is added to the end of the URL. This gets translated, with some mod_rewrite magic, to a $_GET parameter called output. /outputxhtml is also possible but since that’s the default it doesn’t get used very much.

After, (mostly), building the WordPress theme I started testing, and as I expected I ran into some problems when /outputxml/ was added to the end of the WordPress URLs. I got 404 errors, which makes total sense. I thought I could get around this by simply doing a little extra mod_rewrite magic, however, it seems there’s not way to simply replace /outputxml somewhere in a url with an empty string using mod_rewrite alone. After some time, I stumbled upon an underdocumented WordPress function: WP_Rewrite::add_endpoint and its friend, add_rewrite_endpoint. These functions make it so that WordPress recognizes /category/post-name/trackback, and /category/post-name/outputxml. Excellent!

I just had to create a plugin, make sure that WordPress wouldn’t kill my $_GET[‘output’] variable, add 1 line to my .htaccess and I was good to go.

References:

And this is what my plugin looks like, (for educational purposes only. I am not distributing it):

function fourRS_outputxml_activate() {
    global $wp_rewrite;
    add_rewrite_endpoint('outputxml',array(EP_PERMALINK, EP_PAGES));
    add_rewrite_endpoint('outputxml',EP_ALL);       

    $wp_rewrite->flush_rules();
}
register_activation_hook( __FILE__, 'fourRS_outputxml_activate');


function fourRS_outputxml_deactivate() {
    global $wp_rewrite;
    $wp_rewrite->flush_rules();
}

register_deactivation_hook( __FILE__, 'fourRS_outputxml_deactivate');


/* Makes it so WP doesn't eat my nice $_GET variable */
function fourRS_parameter_queryvars( $qvars )
{
    $qvars[] = 'output';
    return $qvars;
}
add_filter('query_vars', 'fourRS_parameter_queryvars' );

Edit (August 25, 2009): Changed the attrbutes in the add_rewrite_endpoint() function.

Switching to an External DNS Provider

The other day I discovered, and tweeted, that Bell Canada, my ISP, has started doing DNS Redirection.  That is, if I try to visit a domain that does not exist, they send me to a page of search results for whatever I typed.  This can be considered convenient, but it is kind of like connecting you to 411 if you misdial a telephone number – not exactly what I want.  Bell offers an “opt-out”, but despite the name it it’s not an opt-out.  They simply set a cookie in your browser that redirects you to yet another webpage that they attempted to make look like the error page from your browser, (and failed).

This wouldn’t really be a problem if I wasn’t developing a program for BonzoBox right now that checks to see if user-entered URLs really exist.  All of the sudden I can’t test my work because no matter what domain name a URL starts with, it resolves, and returns a 200 status code to boot!  This will not do.

I needed a new DNS provider, however, who do you trust?  I tried out OpenDNS, but they do the same thing as Bell just started doing, (except, they’ve been doing it for a while, maybe always, and they’re up front about it).  I read about how Level3 has great DNS servers, but if you look at level3.com it doesn’t mention it anywhere, (it’s not exactly what they’re known for).

I did run across some Level3 DNS Server addresses on DSLReports.com, but are they really Level3’s servers, or are they some hacker’s servers that has seeded the forum with some bogus info in order to capture my banking info?  Well, a whois lookup told me that they do belong to Level3, so my new DNS servers are now 4.2.2.1 – 4.2.2.4.  They’re even easy to type!

FYI:  This does not appear to be a paid service from Level3, but if it was, it is something that I would be willing to pay a few dollars a year for.

Bell, on the other hand, is barely hanging on to my business.  The only reasons I am still with them is 1) I am too lazy to research the alternatives, and 2) I have an old account with no monthly bandwidth cap.  If I can find another service that is reliable and has no bandwidth cap, I may very well consider switching.