The Fifty Words of Green Eggs and Ham
|
|
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Numbers indicate how many times each word appears in the text.
This comes from a posting I originally made to Everything2, two years ago next week.
|
|
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Numbers indicate how many times each word appears in the text.
This comes from a posting I originally made to Everything2, two years ago next week.
Lila was completely entralled by the Ultrasaucer (and another just like it) while visiting friends; hers should be here next week. Considering how long it kept her entertained, my productivity will likely benefit as well. I would feel guilty about that, but there isn’t really any place in my office to put her and she was really, really happy playing in it. And we’ve still gotta pay for it somehow…
After reading this page about Secure Programming in PHP, I realized that my automated PHP archive solution had something of a security hole. It’s been fixed and the original posting was updated.
That page will most likely be gone by tomorrow. There’s a discussion about this on MetaFilter with some interesting comments about the cost differences between US (Region 1) DVDs and those provided to other markets. Yet another bad idea from the MPAA.
I yanked the PayPal and Amazon donation links from the navigation sidebar. It’s been six months and no one has ever clicked them. Experiment over.
Until a few minutes ago, a new month meant broken permalinks, at least until I remembered to redo the Blogger template and make sure a template page was saved with the correct name.
Not any more. Using PHP, I now have a single, multi-use archive template page, which should work to display my archives for as long as I use Blogger for updates.
Here’s a brief explanation of how I did it:
Blogger’s archive file names look like this: 2002_07_01_blog_archive.html where “blog_archive” is the file name specified in each user’s Blogger settings. The rest date is simply glommed onto the front and uploaded.
For my solution, I decided that I would send the archive file name in the URL query string, an example URL would be:
notes_archive.php?a=2002_07_01_blog_archive.shtml#12345678
August 2nd update: I noticed that Google wasn’t indexing those URLs so I decided to use Apache’s mod_rewrite tool to mask the query. Here’s how I did it.
PHP makes processing query strings a breeze, automatically stripping out unneeded data and placing the relevant information into the variable $a.
The simplest thing to do then is to pass the filename to a PHP include command which will embed the file:
if ($a) include "$a";
Using an if statement to check for the existence of $a prevents PHP from trying to embed an empty filename, which causes an error.
July 11th Update: The above code turned out to be a potential security hole. Never include a file in a PHP file without checking the filename first. Instead of checking for and including $a, I modified the code to first make sure the value of $a was a valid file, without any directory information or links to other sites:
if (ereg('^2[^/]+l$', $a)){
The if statement checks the value of $a against a simple regular expression. The pattern simply checks that $a starts with a “2” (^2, not y3k compliant), ends with an “l” (l$) and has a bunch of characters inbetween which are not “/” ([^/]+l). If the value matches the pattern, the filename can be included, otherwise a new value is generated for $a:
$date = strtotime("now");
$a = (date('Y_m', $date) . "_blog_archive.shtml");
I also wanted to include date information in the body of the HTML page, so the title, meta tags and page information would show the month and year depending on the embedded archive. Since the filename already contains a date, a little bit of text munging did what I needed.
The first step is to generate a UNIX Timestamp based on the existing date information. Once the timestamp is stored, PHP can use it to generate just about any conceivable combination of date and time elements for use anywhere on the page. PHP’s strtotime function takes most common date formats and outputs a UNIX Timestamp. I used a regular expression to extract the date from the filename and store it’s component pieces:
ereg('([^_]+)_([^_]+)_([^_]+)', $a, $dateParts);
([^_]+) is a pattern which matches more than one non-underscore characters in a row. Putting the pattern in parentheses tells the matching engine to store the match in a variable for later. In PHP, the match is stored in the array specified at the end of the function, in this case
Using the PHP function print_r shows the contents of $dateParts to be:
[0] => 2002_06_01
[1] => 2002
[2] => 06
[3] => 01
I rearranged those results and used strtotime, placing the resulting timestamp into variable $date:
$date = strtotime($dateParts[1]."-".$dateParts[2]."-".$dateParts[3]);
PHP’s date function can now be used to output the month and year anywhere in the script by inserting the following line of code:
<? echo date('F Y', $date) ?>
I’ve been actively fiddling around with PHP in my spare time for a little over a week, but I’m amazed at how much is possible in a few lines of code and how much I’ve already been able to build.
Hey wait, that’s not me! (but this is/was)