On the string number comparison issue in PHP

As you may have heard, in PHP apparently the following returns true:

'9223372036854775807' == '9223372036854775808'

And after it was posted on reddit a few days ago, shit hit the fan, as expected.

I’m not going to write about whether or not this is bad language design or whether or not PHP sucks – there’s more than enough discussion about that on reddit and hacker news – but there is a really important lesson here:

Continue reading

WordPress Front Page vs Home Page and getting post ID outside or after the loop in every possible way

I was recently working on a site that needed to display the featured image of every page in its sidebar.

On regular, static pages or posts, everything works as intended – you can use is_page(), is_single() and get_the_ID() and not worry about anything.

// This works well on static pages or posts,
// displaying the featured image
if ( is_page() || is_single() ) {
    if ( has_post_thumbnail( get_the_ID() ) ) {
        the_post_thumbnail( 'full' );
    }
}

However, on pages that contain the Loop (blog posts page, custom loop pages), things can get a bit complicated. I’ll walk you through a bunch of different solutions you can use to get the post/page ID, depending on the situation.

Continue reading

Only twenty-two

I was only twenty-two, remember; not that I was so stuck on myself that I didn’t want to know just where I was at fault, but at that age nobody knows much of anything.

Edwin Lefevre, “Reminscences of a Stock Operator”

Fair enough.

Why you should be using a proper HTML sanitization library

The idea that it’s easy to protect your web app from XSS seems to be floating around the interwebz. I’ve seen a whole bunch of “tutorials” saying stuff like “just use htmlencode()” etc.

If you’re building web applications, even small ones, do yourself a favor and use a proper html sanitization library. I hear good things about HTMLPurifier (PHP) and Sanitize (Ruby); there probably is a library for most other languages as well. I don’t dare to recommend anything specific so you’ll need to do some research, but it will be time well spent. And if you’re using a framework or CMS, use the sanitization functions they provide.

Continue reading

Facebook is actually useful, sometimes

I worked at Mäksa parish administration for a couple of months this summer. I was in charge of managing the networks & computers of the administration, plus a small school and a library (’tis a really small parish, like most of them in Estonia are). The last IT guy quit overnight without saying goodbye, or telling anyone the current server and network passwords.

And so it came to be that on a sunny day in August, the server hard dive ran out of space, and I (still) didn’t have the root password. So I called the ex-IT-guy, but he didn’t pick up his phone.

I kept calling him for the whole morning. Nothing.

Oh well, I thought, he’s probably really busy with his new job. Maybe he’ll call back later.

But then, in a sudden flash of insight, I looked him up on Facebook and sent him a message.

Got a reply exactly 11 minutes later.