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 »

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 »

Jan 16 2008

Easy PHP GeoCoding with Yahoo Maps API

I just recently began a project for a client that will require a proximity search feature. Basically the visitor inputs their location, either city/state combo or zip code along with a proximity value (within 10 miles, 25 miles, 50 miles etc) and the search returns a list of businesses (in this case distributors of the clients product line) within that area. I’ve seen these types of searches many times, but this is the first time I’ve had occasion to build one.

To even begin a project like this you would generally need to get your hands on some kind of database mapping city/states and zip codes to latitude and longitude values before you could even start wondering how to calculate a radial distance area from those locations. Traditionally when it comes to these databases, you can either buy the data which is usually a little pricey and may carry a monthly subscription fee as well. Or you can dig around various federal government websites looking for the data only to find it 5 or 6 years out of date and compiled in way that isn’t quite what you wanted. Frankly, either option kinda sucks. Thankfully we now have very sophisticated mapping services from Google, Yahoo and Mapquest that can be leveraged for the task of geocoding locations.

Read the rest of this entry »

Oct 17 2007

Convert 24 hour (military) time to 12 hour (am/pm) time in php

The need to convert time from 24 hour format to 12 hour format is a pretty common task in php applications involving a database. MySQL (and most databases for that matter) require time in 24 hour format while humans (or more precisely, a subset of humans known as “Americans” that are also prone to, on occasion, take for granted that their cultural habits are common to all humans… as pointed out by John in the comments below) prefer to read their time in 12 hour format. I recently ran across a blog post demonstrating numerous custom functions to do the trick, all of which are overly complex. A quick google search confirmed that this is another one of those common programming problems that many PHP programmers have been over engineering a solution for when PHP’s core functions provide a simple and elegant solution. In other words, there’s a much better solution…

Read the rest of this entry »