Posts tagged: TIPS

You really should learn regular expressions

Here’s another advice post. Luckily, many of you can test out of it, like a college Gen Ed requirement. Here’s the test:

  1. What does the following regular expression do? ^http[s]?://([a-z]+\.)?example\.com/$ (Answer below.)

The target audience for this post is people who have heard of regular expressions, but don’t use them. Or who have used them a little, but have the feeling they really should know them better.

You’re right. You should.

Quick, but not dirty, PHP

Though I’m doing more and more work in Python, I still write a lot of PHP code, especially for quick one-off web automation tasks.

There is plenty of activity on the other end of the scale in the PHP world now: frameworks like Cake, WASP, Solar, TaniPHP, the forthcoming Zend Framework. All this action is very cool, but doesn’t address the one-page script – and the one-page script is still worth doing right.

Many people come into PHP helter-skelter, not realizing that “Wow, it works!” is not the highest level of achievement possible. I don’t offer myself as a PHP guru, but below are some of the conventions (I dare not call them “patterns”) I use that I think are worth passing along.

Beware of the "just"

This post on the 37signals blog validates something I have been saying for years, and have recently been telling my students to watch out for: client requests that hinge on the word “just.”

As in: “Can you just make this webcam grab into a 16x20 print?” Or, “Can you just make our shopping cart work like Amazon.com?”

“Just” means, “I have no idea how this is actually going to be accomplished, but I would like it to be instantaneous.”

Obscure "svn mv" problem solved

I banged my head against this one for a while before figuring it out, so I’m posting the solution – for my own future reference if nothing else.

I’ve been working on extending Textmate’s Markdown language bundle. The development versions of the bundles are stored in a repository managed by Subversion.

I noticed that the bundle’s name started with a lowercase letter, unlike the other bundles, so I did a quick svn mv to fix it:

Form hijacking

Does your website contain mail forms that aren’t sanitizing input as aggressively as they should? There seems to have been a recent surge in automated (or semi-automated, it’s hard to tell) probes and exploits of form mail scripts, all revolving around injecting headers into sent mail.

Here’s how it works: Let’s say you have a form that allows the user to enter their email address. The black hat’s exploit script submits a value for that field that includes a newline, followed by whatever email headers they want to insert: Bcc, for example, or even full-blown MIME-encoded parts.

Anatomy of a BoingBoinging

Anatomy of a BoingBoinging

spike Some domain names become active verbs: I googled it. Others become passive verbs: I got slashdotted.

BoingBoing, linked to by over 16,000 blogs, is a passive verb too, and two weeks ago my server got BoingBoinged.

Joe started it when he made a posting on the Well with a link to a series of (bloodless) photos from a huge motorcycle ride turned motorcycle pileup. Somebody suggested a slideshow; I took the opportunity to exercise my mass-image-resize script and to check out ImageReady’s ability to export animations as Flash. I put the resulting 2.6MB file on my neglected moto-blog, posted the link to the Well, and went on with my evening.

Ctrl-T considered harmful

Here’s an odd bug in OS X’s Mail.app: if the cursor is either at the beginning or the end of a message you’re composing, and you press the keyboard shortcut for “Transpose characters” (ctrl-T) the application spontaneously quits.

Bummer!

Caution – don’t idly test this out right now if you have an unsaved message open. I know it’s tempting.

MacFixit has a suggested workaround – use ~/Library/DefaultKeyBindings.dict to disable the key – but it’s not ideal since ctrl-T has valid uses in other applications, like invoking spellcheck in Pico or Nano. Unless you tend to hit ctrl-T accidentally, I’d just leave it alone and wait for the inevitable patch from Apple.