Jun 10 2008

Something incredibly stupid…

So I spent a few days last week helping a client locate the source of a server compromise. The point of attack turned out to be a very poorly written php script. Apparently miffed by the initiative to turn register globals off, and yet too lazy to directly call only the variables that were supposed to be passed legitimately, the author of this script used a method I’ve seen a few times before. Basically you loop thru either GET or POST and use PHP’s variable variables to assign anything there to a local variable. Anytime you’re using variable variables you need to be extremely careful, even more so when you are creating variables from user submitted values.

Read the rest of this entry »

May 9 2008

Calendar Class Bug Fix, version 2.6 available!

A couple of calendar class users noticed an issue with the feature for controlling the day that weeks start on. The class has been updated with a fix. The fix involves replacing the use of PHP’s data(“w”) with date(“N”). date(“N”) uses the ISO-8601 numeric representation of the day of the week. Days of the week are represented as 1 = Monday thru 7 = Sunday. This is different from the old behavior which was 0 = Sunday thru 6 = Saturday.

Since the new behavior could cause unexpected display if the user was unaware of the change, the old property for controlling this feature has been disabled. Use the new property $week_start to control this feature now! Using the disabled week_start_on property will cause an error message to be displayed!

The new version (v2.6) is available for download on the calendar.class.php download page

Mar 26 2008

how to determin if a specific year is a leap year in PHP

I don’t know if I’m just on a refactoring kick lately or what… Ok, how to determine if a year is a leap year. And the related question, how many days are in a given month. If you’re working with dates, there’s a good chance you will need a way to do both at some point.

Stumbled on a post over at the reusable code blog that makes a valiant attempt to provide a solution for those two needs. Again good solid code that definitely gets the right results… but something that can be accomplished in a much more efficient way with just a few native PHP functions. So let’s jump into it!

How to check if a specific year is a leap year in PHP? No need to start dividing by 4 except if when divisible by 100 unless simultaneously divisible by 400. Yeah, I can sense you scratching your head and going back to re-read that last sentence again. And that’s exactly what I’m talking about. Stop it now! Restrain that little voice in your head that’s saying, “oh a problem, I bet I can find a really slick way to solve it.” You don’t need to figure out how to build that equation. All you need is PHP’s date() function.

echo date("L");

That will print 1 if the current year is a leap year, 0 if it is not, as anti-climatic as that might be. Now let’s build that into a truly reusable function by adding in some strtotime() magic!

function is_leapyear($year){
	return date("L", strtotime($year . "-01-01"));

Which you then use like this:

echo ( is_leapyear(2007) )? "Leap Year" : "Not a Leap Year";

3 lines of code versus 11!

Next up, finding how many days are in a given month? The afore mentioned examples build a function which uses a switch statement to hard code the months with 30 days, the months with 31 days and then makes use of the the leap year detection function to decide if February has 28 or 29 days for the year in question. Wheeew, another long description for a solution which brings the total lines of code to accurately print out the number of days in this past February to 42! We’re gonna slim that down quite a bit!

Once again, PHP’s date() function…

echo date("t");

Yup, that will print the number of days for the current month. Let’s add in strtotime() for a nice tight reusable function.

function days_in_month($year, $month){
	return date("t", strtotime($year . "-" . $month . "-01"));

And equally simple usage:

echo days_in_month(2007, 02);

And what’s more, this doesn’t require the previous is_leapyear() to get the job done.

Again, not faulting anybody for trying to solve a problem. In fact, as I’ve said in previous posts, these over engineered solutions often display a great deal of ingenuity and problem solving ability. And of course, I can’t speak for the ASP code posted there. But, not every wheel needs to be invented, or re-invented for that matter. But when I see something like this I’m gonna post about it so hopefully others will find these posts as well as the other code that has been spread all of the internet. Seriously, google for “php is leap year“. People have created classes to do something that a built in PHP function can provide.

Mar 22 2008

ucwords() to convert a string to title case in PHP

I enjoy the challenge of solving a problem as much as the next guy, but sometimes it seems that PHP programmers have a tendency to apply great ingenuity in creating complex solutions for a fairly simple problem that actually already has a very simple solution. What’s more, these bits of code (we’ll call them “better mouse traps”) usually end up posted on a blog someplace to be picked up and spread into countless people’s applications.

have a look at this function I stumbled upon today…

Read the rest of this entry »

Feb 8 2008

calendar.class.php update (version 2.5) now available!

I have just released a new version of my PHP calendar generation class. This new version (version 2.5) implements some features users have requested in comments on this site.

You can now specify the starting day of the week. The calendar can be set to only link the days specified in the $highlighted_dates array and, you can now supply a formating string for constructing the links. The last feature allows you to integrate the calendar into sites and applications using Cruft-free URLs.

Read the rest of this entry »