Archive for the ‘Development’ Category

PHP needles and haystacks

Wednesday, July 21st, 2010

I was skim reading  ”PHP – The Good Parts” yesterday to see if there was anything new in it. The following lines caught my eye – never knew this!

The position of needle/haystack parameters in string functions is the reverse of the parameters in array functions.

strstr($haystack, $needle)
array_search($needle, $haystack)

I just assumed the developers were completely insane and chose parameter orders at random.

Dave on Usability

Monday, July 5th, 2010

A nice perk from working in the web development department at e2save.com: we’re allowed to buy books on the company credit card as long as we share the knowledge with the team afterwards.

I decided to try this out by ordering the “Rocket Surgery Made Easy“ by Steve Krug, the sequel to “Don’t Make Me Think“. I haven’t finished the sequel yet, but I thought it would benefit the team to do a 15 minute presentation on the original:

I’m quite pleased with my first ever presentation (not including primary school). I assure you, it makes more sense if I’m standing there talking you through it!

Oh, and I’d highly recommend “Don’t Make Me Think” if you’re thinking of purchasing a web usability book. It really is quite good, although a little out of date.

SQL Server XQuery and Namespaces

Tuesday, January 5th, 2010

I just had a frustrating little SQL Server problem earlier today and would like to share the handy solution I found via Experts Exchange.

SELECT ResponseXml.value('(//Reference)[1]', 'varchar(50)')
FROM Transaction

A fairly simple XQuery request of an XML column but annoyingly it didn’t work, and it turned out to to be because the ResponseXml had an unnamed XML namespace, which SQL Server doesn’t handle too nicely.

<Message xmlns="http://www.example.com/">
<Reference>123</Reference>
</Message>

It’s the xmlns attribute (XML namespace) that causes the problem apparently, and the simplest solution I found was this snippet:

SELECT ResponseXml.value('declare default element namespace "http://www.example.com/"; (//Reference)[1]', 'varchar(50)')
FROM Transaction

I just had to tell XQuery to use a different default namespace – a lot simpler than other suggested solutions such as removing the namespace from the XML.

Google Mobile Analytics and Opera Mini

Thursday, December 10th, 2009

Google Analytics is a popular web statistics site with lots of useful features, I’ve been a big fan since the start. Google recently added a bunch of new features, and an important one out of those for me is mobile web tracking.

Typically, Google’s way of tracking site usage is to ask you to put a small snippet of HTML onto your site which is essentially a javascript call to their servers. Great – except a lot of mobile phones don’t actually support javascript. So the only good solution is to run some code on the server. I don’t know if it’s related to Google’s purchase of AdMob, but they’ve done exactly that and provided server side code in various programming languages (C#, PHP, etc) to achieve this. See ga.php as a real example.

Great, now I’ve got beautiful graphs showing me how people use my site. But what’s that? 25% of my users live in the country (not set)? I’ve not heard of the place! It turns out those users more or less all used one browser: Opera Mini.

Oh Opera Mini, what a strange beast you are. While the Opera browser is a full normal browser, Opera Mini is a service that installs a thin client on your phone and Norwegian proxy servers visit the site for you. Norway? Well that’s a little better than living in (not set).

Luckily, this whole problem is fixed by a teensy change in their server side code. Rather than always fetching the browser’s current IP address, I tweaked Google’s PHP code so that it detects Opera Mini is in use and sends the real user’s IP address. To be honest, I’m a little confused why this isn’t in Google’s code in the first place.

So here it is. A minor fix to Google’s ga.php file (line 162) to improve Opera Mini handling:

Original line: "&utmip=" . getIP($_SERVER["REMOTE_ADDR"]);

and my version: "&utmip=" . getIP((stristr($_SERVER['HTTP_USER_AGENT'], 'opera mini') && array_key_exists('HTTP_X_FORWARDED_FOR', $_SERVER)) ? $_SERVER['HTTP_X_FORWARDED_FOR']: $_SERVER["REMOTE_ADDR"]);

That’s all. Now I get real country stats :)

Dabr 2.0?

Tuesday, July 28th, 2009

The other day I created a new Twitter account: @newdabr. I had a burst of creative thought and rushed some code to achieve this live demo. It really isn’t much to look at, but what the site is doing is a big improvement over the original @Dabr. (more…)