Nov
12

Using GET data with PHP include()

Programming PHP

Earlier today I thought I’d edit the title-page wallpaper script, and make it print friendly error messages. Previously it would just die in a very unhelpful way when an error was encountered. It didn’t occur to me before-hand, but my custom error handling script was very antiquated. For one, it had to be called with GET information.

This works well for .htaccess redirects and error documents (which was why I designed it this way a year or so ago), but it is a bad way of calling the error functions from within a running script. For one, PHP won’t allow you to pass GET values in the filename:

include('error.php?code=404'); // will not work

Since I didn’t have the time to overhaul the entire script, I checked for an easy work around. PHP recommends you use URL wrapping instead.

include('http://www.lazymoon.org/error.php?code=404');

But I didn’t like this idea, because if my host disables URL wrapping I’ll be left with broken scripts. Not to mention it takes longer to process.

The easy solution, for those who are interested, is just to set the GET information manually before including the file.

$_GET['code'] = 404;
include('error.php);

This does the job nicely.

Monday, November 12th, 2007 @ 11:43 PM • 32 responces

Nov
8

GD Library Garbage Collection

Programming PHP

If you have visited the LazyMoon title page (with a recent browser and JavaScript active), you would have noticed the ability to dynamically create a wallpaper form the now symbolic silhouette design. One thing very few people notice is that the wallpapers are automatically sized to match your screen resolution. Meaning I haven’t just designed every possible combination, but programmed a script to create your wallpaper when you request it.

This consumes an unholy amount of memory. To the extent where I suspect my host may get a little upset if too many people request a wallpaper at once.

What is concerning is that PHP doesn’t automatically garbage collect GD resources stored in memory. If someone cancels their request or the script terminates early, that resource will not be destroyed. This only has to happen a handful of times before the memory is filled and I get a unhappy letter with some very large numbers.

If you use the GD library and don’t have a garbage collection function, you are playing dangerously. To get around this, we can write a function and use PHP’s register_shutdown_function to manually free un-destroyed resources.

The code snippet I use is:

function gd_garbage() {
    global $img;
    if (isset($img)) {
        foreach ($img as $i) {
            if (get_resource_type($i) == 'gd') {
                imagedestroy($i);
            }
        }
        unset($img);
    }
}

register_shutdown_function("gd_garbage");

For this to work, we have to store all image resources in an array named $img. However, we shouldn’t rely on garbage collection alone to clean up GD resources, for two very important reasons:

  1. The function may fail,
  2. Leaving GD images open until the script terminates is a considerable waste of memory.

It is always recommend that you destroy your images the moment you are done with them, and let the garbage collection handle any mishaps along the way.

Thursday, November 8th, 2007 @ 04:23 PM • No responces

Nov
3

Rounding To Grouping

Programming JavaProgramming JavaScriptProgramming PHP

There may come a time when you have to round a number up or down to certain multiples of another number. Since us Australians haven’t had one-cent pieces in quite some time, most purchases are rounded up or down to multiples of five cents.

Enough of my side-story. Here’s some simple code to get the job done:

Java:

import java.lang.Math;

public float roundToGrouping(float number, float grouping) {
    return Math.round(number/grouping)*grouping;
}

PHP:

function roundToGrouping($number, $grouping) {
    return round($number/$grouping)*$grouping;
}

JavaScript:

function roundToGrouping(number, grouping) {
    return Math.round(number/grouping)*grouping;
}

Saturday, November 3rd, 2007 @ 01:23 PM • No responces