A short example of programming

This is an attempt to capture the thinking that goes on behind a short bit of programming work I needed to do. It'll probably remain in rough-form forever.

The problem: people are clicking on links in emails or other web-pages to get to Dave's Picks. Some of the URLs have punctuation near them, most commonly a period after the URL or a greater-than following it. These URLs produce 404s, but are easy enough for me to fix that I ought to fix them up and send the folks to the correct page.

I've already got a redirection mechanism in place to handle pages that have moved, but that's not quite right though, since I implemented it using an array, and I need more flexibility in this case. I wouldn't want to have an array containing a new string for every single page on this website, since I'd have to update it every time I added a page.

So I'll strip off the last character of a URL if it's a period or a grater-than, since none of the real files on my server have filenames ending in period or > At some point, I might have such a file, and it'll cause me some confusion when it doesn't work, but I'll burn that bridge when I come to it.

So how to strip the last character from the requested url? Well, what's the PHP documentation have to say about the last character? Knowing that the php site has a cool search function, I type in the URL http://php.net/lastchar and see what comes up. I get taken to http://www.php.net/manual/en/function.strrev.php where there's a comment saying that $lastchar = substr($string, -1, 1); is a good way to find the last character of a string.

So I notice that I have the full filepath in a variable $fullpath, and write:

// is last character a . or > ?
$lastchar = substr($fullpath, -1, 1);
if (($lastchar == '.') || ($lastchar == '>')) {
	// strip the last character and try again
}

which leaves out the actual work (things after a // are comments in PHP, and are handy reminders to yourself what you're doing -- I almost always write my code as english-language comments first and then come back and fill in the actual code), but finds the cases I'm looking for. This is one of the first steps in most programming tasks. Figure out what you're looking for, find a way to answer a simple yes/no question and then code it up (I'm grossly oversimplifying here, since I'm not covering the syntax of the language or any of those issues, but I think that'll have to be another article).

Okay, so now I need to rip that last character off the string. Since I'm already using substr, and it looks like that might be a good bet to remove the last character, I check the documentation at http://www.php.net/manual/en/function.substr.php to see what it has to say. Turns out a negative value for the last parameter will shortend the string (example 3), so I write:

	$strippedpath = substr($fullpath, 0, -1);

after the comment in the above example. Now I've got the stripped-down path.

From my previous redirection code, I've already got an example of how to do the redirection (this is part of the larger script that serves up the pages at Dave's Picks). It is:

header("Location: $redirects[$fullpath]");
header("HTTP/1.0 301 Moved Permanently", true);
$data = "The page "$fullpath" " .
		"is now located at "$redirects[$fullpath]"";

Since I don't want to use the $redirects[$fullpath], but instead use $strippedpath, I do some copying and pasting and end up with the complete code:

$lastchar = substr($fullpath, -1, 1);
if (($lastchar == '.') || ($lastchar == '>')) {
	$strippedpath = substr($fullpath, 0, -1);
	header("Location: $strippedpath");
	header("HTTP/1.0 301 Moved Permanently", true);
	$data = "The page "$fullpath"" .
			" is now located at "$strippedpath"";
}

I save the modified script (and this web-page), and load this page as a test. It seems to work. Now for the acid-test. Slam a period onto the end of the URL, asking for http://davespicks.com/writing/programming/progexample1.html. and find that it doesn't work. D'Ohh! It's redirecting me to http://davespicks.com/writing/programming/writing/programming/progexample1.html which doesn't exist. But then I spot the fact that part of the path is doubled up, and remember that redirects need a full path (and apparently if not, they'll be treated as a relative URL, which I don't want), so I have to go back and modify things a bit. The result is:

// strip last character if it's a . or > and try again
$lastchar = substr($fullpath, -1, 1);
if (($lastchar == '.') || ($lastchar == '>')) {
	$strippedpath = substr($fullpath, 0, -1);
	header("Location: http://davespicks.com/$strippedpath");
	header("HTTP/1.0 301 Moved Permanently", true);
	$data = "The page "$fullpath" is now located at " .
		""http://davespicks.com/$strippedpath"";
}

I save the updated script (and this page again) and try tacking on a greater-than, which should force a page-reload, allowing me to look at the updated version of this page. I try the URL http://davespicks.com/writing/programming/progexample1.html> and see what happens. Success! Try the period again just to make sure I haven't broken that with the most recent change. Woo!

And that's it. Problem solved, and now a few more people coming to the website will get to the page they wanted with a little less hassle. A little cleanup on this web-page, and I've killed two birds with one stone, getting some more new content written, and making life easier for folks.

Copyright 2009, Dave Polaschek. Last updated on Mon, 15 Feb 2010 14:09:33.