Archive for the ‘ Web Development ’ Category

Database Exports From phpMyAdmin Add Garbage Text After Every Link

This is an issue that has plagued me for months and months, and I finally found a way around this VERY strange bug.

For some reason, exporting a database from phpMyAdmin inserts garbage after EVERY LINK in the database content. So if you have a database row with in the data, it will end up looking something like after export. Obviously, this causes all kinds of issues.

The only way I’ve found to stop this behavior is to NOT check “Save as file” when exporting your database. Just click “Go” and let the dump print to screen. Then copy/paste it into a text file (save as .sql) and import that into your database. If the export is too large to print to screen, you’ll have to manually do a search and replace on the resulting .sql file. The hash that comes after ?phpMyAdmin= is different on every export, but luckily it’s the same in each dump file – meaning it’s relatively easy to find it and replace it.

Set media=print Using The CakePHP CSS HtmlHelper

It seemed so simple, yet it took forever to figure this one out. I’ve been using CakePHP for several projects lately, and I’m really enjoying using it. But much of the documentation is lacking. Luckily there are plenty of blogs detailing the progress of this framework and what can be done with it. But I searched in vain for quite a while on how to create a link to a css file and designate it for “print” only.

Here’s how to create a link to a stylesheet and set the media type to “print”:

$html->css(array('filename'), 'stylesheet', array('media' => 'print'));

That will output the following code:

Hope that helps the next person trying to figure this one out.

Local Web Development With OS X 10.5 (Leopard)

With the release of Leopard last Friday, it appears the bulk of my previous tutorials are now taken care of out of the box. mod_rewrite is enabled by default, and a decent (but not robust) installation of PHP 5.2.4 is ready to go – it only needs to be uncommented in the Apache configuration file.

The PHP installation is missing some key extensions, like PostgreSQL support and the GD library to name a few. But the majority of commonly used extensions are ready to go. Marc Liyanage is already on the case and working out the kinks for his own installer including those additions. The only thing missing at this point is MySQL support for Leopard. As of this writing, there is no installer available yet.

The rest of this post assumes you are working from a clean install or an “archive and install” of 10.5. I’m not sure what the Apache configuration files may look like if you simply upgraded from 10.4 or 10.3 – if they are clean this should work just fine.

Open the Apache configuration file (I’m using TextMate, which is where the ‘mate’ command comes from):
$ mate /etc/apache2/httpd.conf
and scroll to line 114 – it should look like this:
#LoadModule php5_module libexec/apache2/
Just remove the comment:
LoadModule php5_module libexec/apache2/

To enable Virtual Hosts, uncomment line 461:
Include /private/etc/apache2/extra/httpd-vhosts.conf
and add your own directories in your /etc/apache2/users/you.conf file. Similar to the 10.4 configuration, change the first two lines (inside ) like so:
Options All
AllowOverride All

and add your Virtual Host directives after that:

DocumentRoot /Users/yourname/Sites
ServerName localhost

DocumentRoot /Users/yourname/Sites/devsite
ServerName devsite

Restart Apache:
$ sudo apachectl restart
and you’re all set.

The only thing left to do is update your hosts file:
$ mate /etc/hosts
and add whatever you used for your ServerName after the other entries: devsite


Now I need to figure out if those rumors of Ruby on Rails out of the box are true…

Save Time (and code) Using Virtual Hosts For Local OS X Web Development With mod_rewrite

UPDATE: This tutorial was written for OS X 10.4 and parts of it are no longer necessary with OS X 10.5. See this post for details.

Using a few built-in tools in OS X, I’ve found a great way to develop sites as Virtual Hosts on Apache. If you are a web developer working on a Mac and you need to develop sites using root relative paths without adding a base href tag to every page on your site, this tutorial should help tremendously. Here’s how it works:

First, make sure you’ve got PHP and MySQL installed – those are the only two things you should need to install outside of the stock OS X configuration.

Next, follow this tutorial on enabling mod_rewrite on OS X that I’ve written previously.

The rest will involve some basic command line use, but it’s pretty painless. As mentioned in the mod_rewrite tutorial, I highly recommend TextMate for editing the configuration files. If you’re a UNIX purist, there’s always vi.

For this example, create a new folder in your /Users/yourname/Sites folder called devsite and place some files there (a simple html or php file with something to output in a browser will do).

  1. Open your httpd.conf file and enable Virtual Hosts:
    $ mate /etc/httpd/httpd.conf
    Around line 1063 (almost at the bottom of the file), you should find and uncomment this line:
    NameVirtualHost *:80
  2. Open your users.conf file and add a Virtual Host directive:
    $ mate /etc/httpd/users/yourname.conf
    After the ... portion, add the following:

    DocumentRoot /Users/yourname/Sites
    ServerName localhost

    DocumentRoot /Users/yourname/Sites/devsite
    ServerName devsite

    Keeping the localhost portion at the top will still allow you to view the root of the server and anything in it’s directory at http://localhost.

  3. Open your hosts file and add your new site name to bypass DNS:
    $ mate /etc/hosts
    Make sure these three lines stay at the top: localhost broadcasthost
    ::1 localhost

    And add this line after it: devsite
  4. Restart Apache
    $ sudo apachectl restart

You can now view your local files in your browser at http://devsite.

Let’s step back and look at what we’ve done and why this is so useful.

Most likely, until now, you were viewing local development sites by going to something like http://localhost/~yourname/devsite/fancy/url (or instead of localhost) and doing anything with mod_rewrite meant lots of base href tags and RewriteBase configurations, which in turn meant lots of ../../ paths before your images or included scripts to make things work. Now, viewing the same files in your browser is as easy as http://devsite/fancy/url and all of your paths can begin with a single slash ( / ) meaning it will still work the same when you migrate your site to the live server. Root relative paths are a life saver – and this setup makes it possible.

It’s also useful for quick access to commonly used tools, like phpMyAdmin. What I’ve done is install phpMyAdmin in /Users/me/Sites/phpMyAdmin and set up a Virtual Host like this:

DocumentRoot /Users/me/Sites/phpMyAdmin
ServerName admin

In the /etc/hosts file: admin

And I can always get to my phpMyAdmin install by simply typing http://admin into the browser.

The reason http://admin works is because most operating systems (even Windows!) will look at the local hosts file first before requesting information from DNS servers. Apache is told to handle requests to http://admin through the Virtual Host we set up. And all of this is done without www’s or .com’s.

By now you should realize that you can add as many Virtual Hosts as you like for as many sites as you need. All you need is these three commands:
$ mate /etc/httpd/users/yourname.conf
$ mate /etc/hosts
$ sudo apachectl restart

This can also be done on Windows, but it takes a little more work, and would of course work the same as described above on Linux/UNIX. If anyone would find it useful, let me know and I’ll post a new tutorial on the same method for Windows developers.

Change Your Domain Name and Keep Your Incoming Links With .htaccess And mod_rewrite

When moving our site from to, this handy little bit helped move our entire website with four lines of code:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*) [NC]
RewriteRule ^(.*)$$1 [R=301,L]

This reference was extremely helpful.

This RewriteRule lives in the .htaccess file at and tells any request coming in to that domain to swap out and replace it with This includes ANYTHING after the trailing slash, like a direct link to a previous post. So gets sent properly to

The important part is the [R=301] which sends a 301 (Permanent) Redirect header. That tells search engines that the page has moved permanently.

Just imagine what you’d have to go through setting up individual forwarding links…

Creating a MySQL Dump File From an External Database

I needed to export a MySQL database that was on a different server than the web server and was bound and determined to do it without going through the hassle of installing phpMyAdmin (port 3306 was blocked as well, so I couldn’t use any GUI tools either). The trick was adding the -h option. Here’s the command line that made some magic:

mysqldump -u username -p -h --compatible=mysql40 dbname > filetosave.sql

The --compatible command was added because the database server was running MySQL 4.1 and we needed the export for MySQL 4.0.

PHP mail() And Gmail – A Warning on Headers

Today, out of nowhere, we started receiving reports that HTML email the site was sending were showing up as just that: HTML code and not much else. Running a few tests confirmed these reports… but only in my gmail account. We went back and checked the reports and sure enough – they were all coming from gmail users.

After some tinkering and futzing around we realized that the Windows line breaks that we had after the headers (rn) were the problem. Here’s an example:

This will show up as two line breaks in gmail and trash your HTML formatting:
$headers = "MIME-Version: 1.0rn";

This, however, will fix the problem and show up in gmail just fine:
$headers = "MIME-Version: 1.0n";

I’m sure if you were sending mail from a Windows server this may not be the case, but for those on a Unix box, just stick with n. Google will thank you by displaying your HTML email the way it was meant to be displayed – without raw code!

Enable mod_rewrite on OS X 10.4 (Tiger)

Tiger has introduced a new super-confusion level to the stock configuration of Apache. In addition to the httpd.conf file in the /etc/httpd directory, there’s now a new users directory as well. That directory holds unique config files for each user of the machine. So, if you were to enable mod_rewrite or AllowOverrides in httpd.conf, you may find that it doesn’t quite cut the mustard in your personal Sites directory. Let’s take a look:

To enable mod_rewrite:

  1. Open /etc/httpd/httpd.conf
    (I highly recommend TextMate – from the command line you can simply type this:
    $ mate /etc/httpd/httpd.conf
    or use the old standards: vi, vim, whathaveyou)
  2. Go to line 223 (if your config file just so happens to jive with mine) and uncomment the following line:
    LoadModule rewrite_module libexec/httpd/
    (mind the wrap)
  3. Go to line 267 and uncomment the following line:
    AddModule mod_rewrite.c
  4. Scroll down to line 408 and change the line to read:
    AllowOverride All
    (Some server admins will tell you this may not be the best idea for hosting a live site, but I’m assuming you’re using this for local development only, right?)
  5. Uncomment line 454:
    AccessFileName .htaccess
  6. Restart Apache:
    $ sudo apachectl restart

At this point you should have mod_rewrite happily fixing your ugly URL’s in the /Library/WebServer/Documents directory, but it’s not working in your /Users/you/Sites directory. What gives? Here’s the trick:

  1. Open the yourname.conf file in the /etc/httpd/users folder.
  2. Change the first two lines to this:
    Options All
    AllowOverride All
  3. Give Apache another bounce:
    $ sudo apachectl restart

You should now be seeing friendly url’s in your very own Sites directory.