PHP Best Practices

I’ve been coding in PHP for a new client lately, and while PHP doesn’t have all the glory associated with it that ASP.NET has, nor does it have the compiler or IDE that I love, it is still a fairly decent language to code a website in – but only if you do it right.

For some reason people LOVE to stick a ton of MySQL queries into the middle of their HTML templates, which are also embedded deeply among more PHP code.  Are you guys kidding me?  The people who do this have obviously never maintained their own code, it’s just ridiculous to look at or even contemplate maintaining.

OK, maybe this is sounding too angry of a rant, but this is the reason that PHP Programmers have as bad a reputation as VB Programmers.  I sure don’t want that kind of reputation.

Fortunately there ARE good PHP Programmers out there (no I admit, I’m not the only one!  😉 ).  Isn’t most of Yahoo’s website infrastructure based around PHP?  They must be writing maintainable, scalable code!  So it must be possible in PHP.

There are lots of good, lightweight frameworks like CodeIgniter or CakePHP available out there.  They’re easy to learn, easy to use and make your websites far more maintainable by you and people like me.  It should also give you a bit more developer cred, AND help you get your site out there and online faster to boot!

Sorry for the Code Rage, just trying to get the word out!  As with everything, there is a better, easier, faster way, you just have to look for it and use it!

CakePHP: My Session ID Keeps Changing

I had a problem when my CakePHP session wasn’t saving from page to page and it turned out the session ID (from session_id()) was changing from page to page also.

It turns out to be a simple problem: I had pointed Apache to my app folder not my app/webroot folder.

This also caused webroot to be inserted into all my URLs as well.

Quick and easy fix. Long frustrating time to figure out. 😛

CakePHP: AJAX Callbacks Order

In CakePHP there is an AJAX helper based on the Prototype library. During an AJAX event, such as clicking a link generated by the AJAX helper ($ajax->link()) you can add javascript call-backs to the $options parameter. These are the callbacks, in the order they are called:

  1. before
  2. loaded
  3. interactive
  4. complete
  5. after
  6. loading

I was expecting a different order, such as loading to come before loaded, but this is how it is!

Also, if you’re updating the content of a <div> it will be updated between the Interactive and Complete steps.

CakePHP: Error 500 Too Many Redirects

Another error I ran into was this error:

Request exceeded the limit of 20 internal redirects due to probable configuration error. Use 'Limit InternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.

It took a lot of looking, and I found out that it was an error in my .htaccess file. However, there are three major .htaccess files in a typical CakePHP application

  1. The Cake Folder .htaccess
  2. The App Folder .htaccess
  3. The Webroot Folder .htaccess

It’s hard to figure out which .htaccess file you have to edit, but my first guess was the webroot folder.

I have setup my directory structure using the alternative setup described later in the CakePHP manual:

  • webroot: This is in /var/www/testing (where testing is the virtual directory of my application (
  • app: This is in /home/eric/cake/testingapp
  • cake: This is in /home/eric/cake/cake_1.1.12.4205

The setup seemed to be working, and my normal front page appeared, but if I went to another controller (i.e. I would get the Error 500 message and the Apache error log file said I had done too many redirects in the application.

The solution is extremely easy, but it was very hard to find! But I found it on the IRC logs here: courtesy of poLK:

Edit the .htaccess file in webroot (for me that was in /var/html/testing/.htaccess) and add the line “RewriteBase /” to the file, so my .htaccess file now looks like this:

RewriteBase /
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]

It's a simple change, but took me about an hour or two of research, Google and Groups searching to figure out.

Case Sensitivity

I’ve been doing some work on a project based on the CakePHP ( Framework. CakePHP is awesome, and a great alternative to Ruby on Rails.

When I was moving my project from my development box (Windows) to my testing box (Linux) I started getting weird “Missing Controller” errors. It turns out that the error I was having was because I named my files Main_Controller.php instead of main_controller.php like CakePHP was expecting.

It seems like a simple problem, but I spent almost two nights trying to fix that bug!  I had to dig into the framework code to resolve it.  Probably one of the best parts of the CakePHP framework is that the framework code is pretty light, and while its pretty complicated stuff its still really easy to dig down into it.