SiteCrafting Blah Blah Blog

Mar. 31, 2008 at 11:40am

Got API?

An API reference does a method's body good...

gotAPI.com is one of the most useful online resources I've come across, primarily because it places resources spread all over the internet into one simple site. I've been using this for quite some time, and have for the most part I have taken its usefulness for granted. Then it occurred to me that I might not be the only one that could find this tool useful (I know, it was a big 'DUH!' moment). So now I will share this gem with others...

Read more

Comments (1) | Posted in ASP.NET 2.0, CSS, From the Workbench, Javascript, MS SQL Server, MySQL, PHP, Review, Software Engineering, XHTML by Nick Williams


Mar. 26, 2008 at 12:41pm

Make PHP Growl

. . . but in a good way!

I stumbled upon a fantastic PHP class today and felt compelled to comment on it. Originally written by Tyler Hall, this class allows you to send notifications to any system running Growl from a PHP script. For those of you that don't know, Growl is an application written for Mac OS X that is intended to act as a universal notification tool (much like the taskbar notification bubbles we've all come to know and love in Windows XP/Vista).


Read more

Comments (8) | Posted in PHP, Review, Software Engineering by Nick Williams


Mar. 12, 2008 at 4:34pm

PHP 5.3 - A Heavy Hitter in Training

PHP's next major release is just that, major!

The evolution of PHP into an object-oriented programming language has been a very exciting process to watch. I'll admit its advancement seems to move at a glacial pace at times, but looking back we certainly have come a long way. With the release of 5.0 we saw PHP leave its childhood and enter its awkward adolescent stage. At this point there is evidence of a mature and complex object-oriented language while stilll exhibiting many of its younger habits.

So what's in store for us next? Find out after the jump . . .

Read more

Comments (0) | Posted in PHP, Software Engineering by Nick Williams


Jan. 29, 2008 at 3:15pm

PHP Patterns, Part III

The TO Pattern

The Template Object (or TO) is a design pattern of my own that I developed to fill the role of the View layer in the MVC model. As you have probably figured out, the purpose of the TO is to handle everything related to the user interface. The idea here is to separate the interface as much as possible from the rest of the application, so that we could do a complete rewrite of an application without ever touching (or accidentally "breaking") the view portion. Read more

Comments (0) | Posted in Coding Techniques, From the Workbench, PHP, Software Engineering by Nick Williams


Jan. 9, 2008 at 3:51pm

PHP Passes Homeland Security Test

Use open-source with more confidence

When meeting with prospective new clients, we tell them that SiteCrafting uses PHP and MySQL as the development platform. Invariably this leads some of them to ask us what PHP and MySQL are and if they are safe and fast. Sometimes, this can lead to interesting conversations, where we explain to them why we think PHP and MySQL are safe and fast.

Occasionally, there's a client who remembers reading an article 4 or 5 years ago about PHP 3 having some security issues. We refer them to current articles on PHP and mention our own experiences, but the latter argument can come across as "Because we say so," which isn't a good way to get the point across.
Read more

Comments (0) | Posted in Odds 'n Ends, PHP, Security by Ken Foubert


Nov. 30, 2007 at 1:05pm

Why is AOL denying email?

Try adding a parameter to the mail() function

Email debugging is often frustrating and time consuming. There could be numerous reasons why the email you attempted to send out through your code fails: routing issues, firewalls, company servers blacklisting or blocking incoming emails, spam filters, bad DNS records, and email header requirements. The last one can definetely lead to infinite frustration, as I have experienced in the past, and was recently the cause of why AOL was denying emails generated by the php mail() function.

Read more

Comments (1) | Posted in From the Workbench, PHP by Ken Foubert


Nov. 14, 2007 at 3:48pm

PHP Patterns, Part II

The DAO and VO Patterns

In this installment, we will be looking at two patterns that have been 'borrrowed' from Java. If you've had any development experience with J2EE, you are probably well aware of how handy Data Access Objects and Value Objects can be. If you haven't, don't fret! This article was written especially for you!

If you've never heard these terms before, you may be wondering why I have chosen to group them together within one article. The simple explanation is ... well you'll see. For now just accept that they go hand-in-hand, much like salt and pepper or peanut butter and jelly or <insert clever combination here>.

Excited? Let's dig deeper...

Read more

Comments (4) | Posted in Coding Techniques, From the Workbench, MySQL, PHP, Software Engineering by Nick Williams


Oct. 26, 2007 at 2:05pm

Apache, MySQL, and PHP on Leopard (Mac OS 10.5)

I just finished installing Leopard on my computer, and my first impressions are that it's very slick and well thought out. One of my necessities as a developer is that I must have a webserver running on my personal computer, so I was dismayed when it wasn't functional after the upgrade. The main reason is that Leopard uses a different version of Apache than 10.4 did, and so some things get wonky. But it's easy to fix. Apache and PHP are included in Leopard, so the only thing missing is MySQL. To install that, go to MySQL's site, and download the latest copy for OS X. It's incredibly simple to install.

After that comes setting up Apache. Open up a terminal window, and type in "sudo pico /etc/apache2/httpd.conf". (Note that you must be a computer administrator to access the files in etc/) OS X 10.4 had Apache in /etc/httpd/, and that's part of why it didn't work after the upgrade. Find the line in httpd.conf that looks like "#LoadModule php5_module        libexec/apache2/libphp5.so", and remove the # sign at the beginning. Then search for AddType, and put the following somewhere around it.

AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps

Save the file, and exit. Then open up System Preferences in Finder, and click on Sharing. Then turn on Web Sharing. That's all you need do to to setup a webserver on Leopard, or upgrade from OS 10.4. You should be able to open up a browser, and enter http://localhost/YOUR_USERNAME/ and see that the server is running.

Comments (30) | Posted in MySQL, PHP by Dave Poole


Sep. 27, 2007 at 3:17pm

PHP Patterns, Part I

The Registry Pattern

This is the first in a planned series of articles discussing the application of various PHP patterns. If you're still getting your feet wet developing in PHP (or programming languages in general), you may be wondering what a "pattern" is in the first place. The best place to find a quick explanation is over in Wikipedia's article (which does a much better job than I could hope to do within the scope of this article).

Now, let's move on to the fun part . . .



Read more

Comments (7) | Posted in Coding Techniques, PHP, Software Engineering by Nick Williams


Aug. 9, 2007 at 4:14pm

Getting the Client’s IP Address

There are a number of reasons why you may want to capture a web user's IP address. You may want to only allow certain IP addresses to view your website, to allow only one vote per IP address, track unique clicks on links and buttons, a location of web users on Google Maps using MASHUP, and whatever else you can think of.

Below are a couple of examples to get the client's IP address in PHP and ASP.net, along with a couple of things to watch for.

Read more

Comments (0) | Posted in ASP.NET 2.0, Coding Techniques, From the Workbench, PHP by Ken Foubert


Jul. 27, 2007 at 2:56pm

PHP Code in The Background Part 2

Passing Arguments To Command Line

The blog entry How To Run PHP Code In The Background discussed how to run a php file through the command line using php's exec() command.  Recently, I needed to pass a querystring to the php file, but this is not possible through the command line. If you try to pass a querystring, the exec() command will fail.

Fortunately, after some googling, I came across a forum post that mentioned how the global array $_SERVER["argv"] can be used to get arguments being passed through php. I then followed the link on that page to Chapter 43. Using PHP from the command line on  the php.net website. Under the user contributed notes, I found a nice little function for getting the arguments and placing them into an array, similar to $_GET, $_POST, and $_REQUEST.

Read more

Comments (0) | Posted in From the Workbench, PHP by Ken Foubert


Apr. 18, 2007 at 1:08pm

PHP PEAR and include_path

When attempting to use the SMTP library from PEAR for a project, I was having problems with including the file. I kept getting a file stream error for this code:

require 'Net/SMTP.php';

I figured the problem had to be related to the include_path setting in the configuration file.

Read more

Comments (0) | Posted in Coding Techniques, From the Workbench, PHP by Ken Foubert


Apr. 13, 2007 at 2:19pm

How To Run PHP Code In The Background

PHP Asynchronous Programming

If you have php code that would take longer than 5 minutes to run, what would you do?

You could update the configuration file for apache, or whatever web server you're running, to increase the amount of time a script page is allowed to run  and then use php's ini_set("max_execution_time", seconds) to increase the amount of time php will allow a script to run.

Or you could use a session variable to mark the last position of the script and then use meta-refresh to start the script at the last position.

Or you could try an asynchronous call by using php's exec command.
Read more

Comments (9) | Posted in From the Workbench, Odds 'n Ends, PHP by Ken Foubert


Apr. 9, 2007 at 4:36pm

Printing without the Dialog

One day, one of our clients came to us with a very unusual request - they wanted to be able to print something directly from the browser, but without displaying the usual print dialog box. I don't have much time in the webdev business, but I've never heard of this kind of request, and neither had anyone else in the office.
Read more

Comments (0) | Posted in From the Workbench, Javascript, Odds 'n Ends, PHP by Dave Poole


Apr. 2, 2007 at 2:36pm

PHP cURL and SSL Connection Timeout

I've been working on a web site that uses cURL for request/response transactions and I was encountering a number of problems with empty responses from the server. This caused some frustrations, since these empty responses caused entire sections of a web page to be empty, especially on searches. Well, I couldn't have that, since people using the website would be confused on why a search for an item only worked occasionally. Trust in the web application would drop dramatically. Read more

Comments (2) | Posted in From the Workbench, PHP by Ken Foubert


Mar. 20, 2007 at 4:55pm

Character Set Troubleshooting

Over the past few weeks, Reena has been building a new website off and on for a local Jesuit parish.  Everything was going along smoothly until we hit a bump in the road when dealing with a particular page with some Vietnamese text.  After some small (but not obvious) changes in configuration and more research than I cared to do on the subject of character sets, it's all working properly.

Read more

Comments (1) | Posted in PHP by Mike Ash


Feb. 28, 2007 at 1:25pm

PHP Strings - Gotcha!

Yesterday I was working with a script that saves an uploaded image to a database. If you've ever seen a binary file as a string, there's a ton of gobbelty-gook that is unreadable to people. For some very odd and inexplicible reason, my script only saved the first four character's worth of data to the database, and that's not very helpful. I did some digging, and revealed a weird gotcha in the way PHP treats strings.
Read more

Comments (0) | Posted in Coding Techniques, PHP by Dave Poole


Feb. 20, 2007 at 2:53pm

Optimizing GROUP BY with Multiple LEFT JOINS

Creative use of temporary tables

At SiteCrafting, I enjoy working with a large number of different projects, each with their own requirements, technology, and problems to be solved, unfortunately, I sometimes forget about past solutions, until after I have finished writing a piece of code. Such is the case with a query that was eating up some serious processing time.

The problem was with a GROUP BY query with LEFT JOINs to several other tables and summing up totals from those joined tables. This query was taking about 4 minutes 45 seconds to run, and worse yet, it was affecting searches which had nothing to do with that query, and probably also eating up precious memory and cpu resources.

Read more

Comments (0) | Posted in From the Workbench, MySQL, PHP by Ken Foubert


Feb. 2, 2007 at 10:53am

Query conservationist

The fewer the queries the better

I always enjoy trying to do my work creating web applications using the fewest number of database queries possible. It's kind of a pride thing, I guess. That and I suppose it makes to improve an app's performance and reduce the load on a server. If you're into that kind of thing. ;)

A technique I use subtitutes the temptation of using queries whose results call queries and instead uses only two.

Read more

Comments (3) | Posted in Coding Techniques, MySQL, PHP by Kevin Freitas


Feb. 2, 2007 at 10:11am

Script Timing, v2

Making the timer look better

Last time, I outlined a way to create a simple PHP script timer. I use this timer in many of the projects that I work on, and it's really helpful to nail down trouble spots in scripts. The thing that it lacks, however, is a decent display. When you call the elapsed() function, it only gives you a number. Creating a decent display for each timer call is tedious and boring. So I simplified it. Now, I'm going to extend the existing Timer class and add some new functionality - make it look better and make it easier to call and display the information.

Read more

Comments (2) | Posted in CSS, PHP, Software Engineering by Dave Poole


Jan. 26, 2007 at 4:14pm

Making a PHP Script Timer

I've been ranting about a lot of things lately, but not many of them are related to my job description. I want to take a bit of time, and go back to good old PHP, CSS, and Javascript. Today, I'm going to go over how to create a execution timer that will let you know how much time has passed since it started. There are a number of benefits to using one of these things, for example to track down why a script would take 77 seconds to complete rather than 3 or less. (It was a bloated SQL query)
Read more

Comments (0) | Posted in PHP, Software Engineering by Dave Poole


Jan. 19, 2007 at 4:52pm

Emulating Form POST in PHP

There's small choice in rotten apples.

Recently, I had a need to POST a form, using PHP script, to a remote url.  As I started to research a solution, I soon realized there were very few limited options.

Read more

Comments (4) | Posted in Coding Techniques, From the Workbench, PHP by Ken Foubert


Dec. 13, 2006 at 4:12pm

The <select> Tag and innerHTML

While I was working on a recent project, I ran into a weird bug with IE 6. I was using some AJAX to dynamically fill a <select> tag with options depending on what element was selected from the tag's parent. It worked fine, and quite seamlessly in Firefox, but broke for inexplicable reasons in IE. Read more

Comments (6) | Posted in Browser Bugs, Javascript, PHP by Dave Poole


Dec. 1, 2006 at 8:41am

Use Functions for Readable Code

Code Soup versus Short Stories

On my way to meeting with a client, I got into a conversation about using functions in your code. One of the guys learned to write assembly first, and so his stance was that creating functions was only feasible when you used the same code more than (about) three times. That view makes a certain sense - don't deal with the overhead of creating tons of functions for stuff you'll seldom use.

I argue that you should use functions where ever possible, first because it makes code easier to generalize (and thus re-use), but also because it makes code much more readable.

Read more

Comments (1) | Posted in Coding Techniques, PHP, Software Engineering by Dave Poole


Nov. 8, 2006 at 1:36pm

The Joys of Object Oriented PHP

It's pretty common for PHP developers to make complex and difficult to maintain scripts, and I am no exception. I write my code in two distinctly different ways: scripts that do a whole bunch of things depending on input, and classes that do a bunch of things depending on how they are called, but from lots of different scripts. But which is better?

It's a nice ego boost for any prrogrammer to know they created a script/function/class that is really useful, and well designed, but at some point the code stops being well designed, and becomes gross. I think that point is when you have one script that displays an entire (SQL) table of data, views a particular row, edits that row, can delete that row, or can add new row to the table.

If my Software Engineering professor sees me doing that, I imagine that he'll reevalute my grade in his class.
Read more

Comments (2) | Posted in Coding Techniques, Critiques, PHP by Dave Poole


Oct. 27, 2006 at 11:34am

Cross Table Content Search

One of the more interesting adventures working with data storage is trying to aggregate information meaningfully from multiple very different data structures. Imagine you've got a website filled with content (say, a few hundred pages). All the content is stored and output dynamically - who wants to create and maintain 500 static html pages, anyway? And of course, you need a bit of variety, so all this content is spread across five different page designs, each requiring its own template and data structure.

Now you say you want to search your site? All the content? And you want the results all together in one big happy sorted-by-relevancy list? How on earth am I going to do that?

Well... like this.
Read more

Comments (0) | Posted in MySQL, PHP by Joe Izenman


Oct. 16, 2006 at 12:57pm

File Extensions

I've been using a fairly complicated function to grab the extension of an uploaded file. I have run across many instances where I have wanted to split up the file name from the file extension, or just find the file type. Since MIME types are not reliable enough for grabbing a true extension, I have been using this aforementioned function.
Read more

Comments (5) | Posted in Coding Techniques, PHP by Mark Neidlinger


Oct. 13, 2006 at 10:59am

A Piece of Query Cake

Making Inserts Sucks

If you've ever created SQL queries with PHP, you probably know what a pain it can be to create insert and update statements. I really, really (really) don't like it. As I was working on my personal site, and exploring possible frameworks to use, I came across CodeIgniter. They have a great database interaction library, especially the function for creating the insert queries.

Today, armed with only the descriptions of CodeIgniter's query helper functions, I spent 20 minutes trying to duplicate some the effect of the insert and update functions. I've never seen the code, or even used it, but I didn't have to see the code to write a similar function. Both functions take a table name and an associative array of column names and values. The update function also requires a WHERE statement, and it can't be blank. This is different from CodeIgniter, and that's so you don't accidentally reset all of the passwords in the mysql users table, or any table for that matter. And then, *poof*, the function gives you a nice sql statement.

I'll never have to write another "INSERT blah blah blah" again. Yay!
Read more

Comments (2) | Posted in Coding Techniques, MySQL, PHP by Dave Poole


Oct. 11, 2006 at 7:29pm

The Three 'M'-igos

Why butter fingers break apps

Happens all the time, right? One minute you're cranking out line after line of quality code then the next it all comes to a screeching halt for no apparent reason. I immediately go into troubleshooting mode. A quick and simple redirect using a header("Location: ...") function should pose no danger of messing up my app -- but today it did.
Read more

Comments (3) | Posted in Coding Techniques, PHP by Kevin Freitas