Browsing articles in "Code"

Easy, Beautiful Charts with Flot

May 22, 2011   //   by Mike McMurray   //   Code, Web Apps  //  No Comments

I’ve been looking for a free, open source chart library for a while and was struggling to find something that was good enough (and handled time series and missing data points well). For a long time I’ve been using Fusion Charts (FC) and Open Flash Chart (OFC) in any in-house work and those products are very good. But they’re Flash, a little slow and not supported by some popular mobile devices. I’ve also used pChart for some testing but it was fairly stale in development and being images, had no interaction.

The Flot JS library was something I’d seen a while back, but before I became comfortable with JS and jQuery. It does almost exactly what I want, is farily light-weight, extensible and easy to use. In a few hours last week I swapped out the FC code I had written for Overview and put in something almost half the size with Flot that created (in my view) a better product.

So I thought I might let others know what a nice, easy option Flot is with a little example. Beware this is not a fully working, copy and paste sort of example, it’s really just a chunk of generic code I pulled out. Read more >>

Debug PHP Function Calls

Feb 11, 2011   //   by Mike McMurray   //   Code, Web Apps  //  No Comments

Logging errors can be very helpful as your code base becomes huge. But sometime it’s still difficult to find out what’s calling the function that’s giving the error. Wouldn’t it be nice if there was a way to see how you’d got to that function?

Well of course there’s a way in PHPdebug_backtrace. Just add something similar in a suitable place in your code and you’ll be able to find what functions were called.

$trace=debug_backtrace();
$caller = $trace[1]['function'];
// or just dump all the info
var_dump($trace);

Beware the leading zero in Javascript parseInt()!

Dec 9, 2010   //   by Mike McMurray   //   Code  //  No Comments

Exhibit A,

parseInt('01'); //equals 1
parseInt('02'); //equals 2
parseInt('03'); //equals 3
parseInt('04'); //equals 4
parseInt('05'); //equals 5
parseInt('06'); //equals 6
parseInt('07'); //equals 7
parseInt('08'); //equals 0 !!
parseInt('09'); //equals 0 !!

When Javascript encounters a leading zero it assumes an octal number. So when it sees 08 or 09 then there are problems.

The fix is to add the radix to the function call,

myInt = parseInt('08',10); //equals 8

Using PHP exec() with IIS6

Oct 12, 2010   //   by Mike McMurray   //   Code, Sys Admin, Web Apps  //  No Comments

There are a few work arounds I’ve found since having to use PHP under IIS6 and Windows 2003. Every now and again I come across something that just doesn’t work the same way with this variation of web server, OS and PHP.

I needed to double-check the DNS hostname being reported from users on an internal website. Our DNS is a little scratchy when it comes to scavenging and keeping itself tidy, so often a DNS lookup of the client IP will give an incorrect name. This happens especially for laptops that hop off and on wireless APs.

So to compare against DNS (and because it’s an internal site) I thought to query the machines NetBIOS name with,

exec("nbtstat.exe -A 123.123.123.123",$r);

and that quickly fails with an error about “failing to fork”. Essentially this means, PHP can’t do what you want.

It boils down to file permissions. you need to run cmd.exe and nbtstat.exe with that single command. Both those files are secured against non-system users – probably rightly so too.

To resolve, just allow your IUSR_SERVER user to have read and execute permissions on both those files. Your exec() command should now be working.

REMEMBER: You are responsible for the security of your server. If you really don’t need to let programs run on your web server, then don’t.

Running WordPress & PHP Behind ISA Proxy

May 14, 2010   //   by Mike McMurray   //   Blogging, Code, How-To, Sys Admin  //  No Comments

Some things work well on their own but when mixed make your life hard. Things like Linux and PHP work very well. Microsoft ISA proxy also does a good job in a corporate MS environment. But making the two work together in a controlled environment can be an exercise in frustration.

In this post I’ll pass on the methods I found to get PHP and your Linux boxes talking out through a corporate ISA proxy server. You can then bring in RSS feeds, updates and other things in WordPress and use apt-get to update Ubuntu. Read more >>

Outlook Hates Line Breaks

Jan 22, 2010   //   by Mike McMurray   //   Code, Web Apps  //  No Comments

If you’ve ever wondered why your plain text email message is randomly ignoring line breaks like \n or \r\n then you’re not alone. I regularly use PHPMailer to send off automated emails and usually in plain text to keep it simple.

What Outlook 2003 (and 2002 and 2007 versions apparently) likes to do is be super helpful and remove what it regards as extra line breaks. It won’t be consistent either within a single mail or across many but it will make the content difficult to read. What you thought would be new lines will now be joined up in places and it seems to happen more often the further through the content you go.

There don’t seem to be many fixes for this issue but there are a few work arounds to help out.

  1. Turn off this “feature” in Outlook in the Tools»Options menu. Honestly I’m not sure what use it is anyway. Unfortunately you’d have to do this on all the recipient’s computers.
  2. Use HTML in your email rather than plain text. Depending on your content and need for complete accuracy, perhaps more time than it’s worth.
  3. Add twice the number of line breaks where you currently have them. This seems to help but now your email is rather full of white space and may be more difficult to read.

Other than changing email clients, which is pretty unlikely, that’s about it. If you know of other options to try and get Outlook to not remove line breaks, please leave a comment below. This Microsoft KB article explains which versions of Outlook are affected http://support.microsoft.com/kb/287816.

New Version of Kisimi Wiki is Available

Oct 27, 2009   //   by Mike McMurray   //   Code, Web Apps  //  No Comments

I’m happy to announce that a new version of Weka Design’s free wiki software called Kisimi is available. There are a huge number of improvements in the 20091025 release. Here are some of the more important ones.

  • Page security allows you to dictate exactly who can view and edit your pages. Some people can edit your pages, some can view and everyone else can’t see a thing. Security is as finely grained as you want it to be.
  • Kisimi now comes with an upgrade option at installation time and things are a little smarter. Installation can take less than 2 minutes!
  • Longer pages with headings can automatically generate a table of contents section.
  • Users can specify a timezone so that the dates and times are relevant to their location.
  • Administrators can prevent new user sign ups. Handy for those sites publishing their documentation and support pages to the world.
  • Any user with an email address listed at www.gravatar.com has their avatar image appear in their preferences.
  • Tidier user interface with plenty of little shiny bits.
  • Plenty of bug fixes including those messages for PHP configs that show all warnings. It’s still recommnended that PHP is configured without warnings for production servers.

Just head to http://kisimi.wekadesign.co.nz to download the latest version and start managing your online wiki content better.

Adding up Rows with JQuery

Feb 26, 2009   //   by Mike McMurray   //   Code, Web Apps  //  No Comments

Simple and sometimes unnecessary function to add table cells together and populate another with the total. Using jQuery because its easy and it works and we all love it.

$(document).ready(function() {
    var x = 0;
    $("td.num").each(function() {
        x += parseInt($(this).html());
    })
    $("td#total").html(x);
});

Where your table cells to add up are <td class=”num”> and the table cell that has the total is <td id=”total”>.

A Closer Look at Wiki Authorship

Feb 5, 2009   //   by Mike McMurray   //   Code, Interesting Stuff  //  No Comments

Jeff Atwood takes an interesting look at the history of changes to wiki pages and the balance between opinion and fact. The larger wikis (e.g. Wikipedia) have a huge amount of data around page edits and Jeff’s article also highlights an IBM study on how the more popular Wikipedia content evolves over time.

There’s also a comment about one of my favourite subjects – reading too much into statistics. Apparently Jimmy Wales (Wikipedia co-founder) looked into who was responsible for most of the articles changes and found that 0.7% of users were responsible for over 50% of all edits. But an “edit” may be a spelling correction rather than adding content or altering the facts or meaning in an article. As it turns out, the data points to these hyper-active users doing just that – cleaning up after everyone else.

Kisimi uses a basic string comparison function called simple_text() to show the relative difference between two versions of a page. We could also use the Levenshtein function which gives the minimum changes to go from string A to sting B, but that doesn’t always make much sense for larger content changes. If someone sees that two versions are 96% the same then it’s obvious they’re much the same.

Why Aren’t You Using FireBug?

Mar 7, 2007   //   by Mike McMurray   //   Code, Web Apps, Web Design  //  No Comments

I’ve been using the Firefox browser addon called FireBug for a while now and am amazed at how helpful it is. If you’re a web developer, and especially if you use JavaScript and AJAX methods, you should be using it.

For example, while developing I like to add in timers to PHP based pages to show how long things are taking. This way if a SQL statement needs some fine-tuning or a change slows things down I can see it happen. FireBug extends this to the entire page and the HTTP traffic. Here’s what happens when I load a page that has a few JavaScript calls, small images and a single CSS link in it,

Straight away it’s obvious what’s taking up the bulk of the time – those two library calls. Once I take those out of the equation the load time drops to under a second. And through all this the PHP timer function only shows me how long the server-side work is taking.

With FireBug I know who (in a geeky code way) is doing what and with who and I can act on it. Now that’s helpful.

Pages:12»