Archive for the 'PHP' Category

“Minify” your external JavaScript and CSS with PHP

First off, I have to say that most of the work that went into this project was already done for me by someone named rgrove. You can find the original (PHP 5) version of Minify here. All I’ve done is convert the code to run on servers still using PHP 4 (download here). This article presents a justification for using Minify over other techniques, and the results of converting www.appelrouthtutoring.com to use the library.

Most modern web sites today are seperated into the three main parts: semantic HTML, presentational CSS and functional JavaScript. Any HTML page you visit can be supported by multiple CSS and JavaScript files, each one requiring it’s own HTTP connection and download. When a website starts using multiple style sheets and the latest fancy JavaScript libraries (I’m looking at you jQuery, Prototype, MochiKit, YUI, Ext, Dojo Toolkit…) mixed with their own personal scripts, things can start getting pretty slow even on the fastest connections.

One solution is caching these files, but this still requires an HTTP connection every time the browser has to check for the latest versions. You could use an expiration time far off in the future, but you better make sure your code works perfectly the first time a user downloads it. Another problem with this technique is that pages served over HTTPS will never be cached; obviously for security reasons.

A second solution used by many major sites like Google and by libraries like jQuery, is to pre-compress any JavaScript so that file downloads are quicker. Dean Edward’s packer is an excellent tool in this regard. While this is a big help to anyone stuck with dial-up, users with fast connections will not notice a remarkable difference due to the identical latency for downloading an uncompressed or compressed JavaScript file. This also adds another step to the building, testing and deployment process because you definitely don’t want to be developing a site while debugging obfuscated code.

Minify is a PHP library that automates the process of concatenating and compressing multiple CSS and JavaScript files. This is a “free” way to improve the experience for anyone visiting your site; everything just loads faster. How much faster? Before I deployed the minified version of www.appelrouthtutoring.com, I tested the average download time of the homepage with Fasterfox, a Firefox extension. I reloaded the homepage 10 times, clearing the cache every time, and found the average load time to be 3.343 seconds.

That would be a good average time for a content heavy site like www.cnn.com, but not for the super simple mostly text web site I tested. After uploading the site using my modified Minify library, the average load time dropped to 1.228 seconds. Again, that’s an average after 10 reloads with no caching. That’s a speedup of 272%(!), with absolutely no effect on the development process for the site.

I found a similar speedup on the intranet part of the site. Load time fell from an average of 4.994 seconds to 2.329 seconds (214% speedup!). While this doesn’t exactly double the productivity of employees using the intranet, it sure makes life a lot easier.

It’s code time (the exact code I use on the homepage):

[do this before writing any HTML]

require_once('minify_php4/minify.php');

// Create new Minify objects.
$minifyCSS = new Minify(TYPE_CSS);
$minifyJS = new Minify(TYPE_JS);

// Specify the files to be minified.
// Full URLs are allowed as long as they point
// to the same server running Minify.
$minifyCSS->addFile(array(
'/styles/fonts.css',
'/styles/nifty.css',
'/styles/public.css',
'/styles/calendar.css',
'/styles/navigation.css'
));

$minifyJS->addFile(array(
'/scripts/jquery.js',
'/scripts/nifty.js',
'/scripts/public.js'
));

[do this in <head>]

<style type="text/css"><?php
echo $minifyCSS->combine();
?></style>
<script type="text/javascript"><?php
echo $minifyJS->combine();
?></script>

It’s so easy, it should be a crime to load multiple external files the old-fashioned way. Let’s all make the web a better place and start cutting down those load times!

Get the source here.

UPDATE: Send email with PHP and GMail hosted for your domain

Due to the amount of requests emailed to me for working source code I have now posted the modified PHPMailer I use in my own projects. Click here to download the library. Here’s an example of how to use the code:

require_once('/phpgmailer/class.phpgmailer.php');
$mail = new PHPGMailer();
$mail->Username = 'user@domain.com';
$mail->Password = 'password';
$mail->From = 'user@domain.com';
$mail->FromName = 'User Name';
$mail->Subject = 'Subject';
$mail->AddAddress('myfriend@domain.com');
$mail->Body = 'Hey buddy, here's an email!';
$mail->Send();

This will work for any GMail or Google Hosted email account. Just make sure to include the @domain.com part for the username, even if it’s for standard GMail. Hope this saves people some hastle.

[tags]gmail, php, google, phpmailer[/tags]

Handling multiple web development projects using VMware

This morning a client that I’m currently developing for called me to inform me that they needed to use their web application immediately. I had to host it for them myself as they didn’t have their own server, but I was already working on another project that I was already hosting.

Normally this involves editting my httpd.conf file, adding a virtual server, messing with the firewall and restarting Apache. But today I decided to try something new: setup a VMware Workstation session running Ubuntu server and use the Shared Folders feature to link my project to the document root. I’ve been really pleased with the results and now, if I need to setup another server for a client, I can just clone my current session, share another project folder and it’s up in minutes.

For me, the biggest upside to doing it this way is that each application can assume they are running with default settings including standard HTTP/S ports. This means no fancy setup of name servers when you are trying to host multiple sites, each with SSL. Another nice thing is the added security between clients who are now partitioned into their own virtual servers and not having to restart Apache or MySQL for all your other projects. It’s real easy, here’s how I did it:

  1. Install VMware and get it running
    Go to www.vmware.com, create a profile and get a VMware Workstation key, download the program and follow the instructions. Setting it up has never been a problem and is kind of beyond the scope of this article, just don’t forget to run vmware-config-tools.pl after installing it.
  2. Download Ubuntu Server and install it
    Head over to http://www.ubuntu.com/server and follow the links to download the SERVER version of Ubuntu, the standard version has too much stuff we don’t need. Create a new VMware session (make sure to choose Linux, or specifically Ubuntu as the OS) and remove the sound and floppy hardware in the session settings. In the cdrom settings, make sure the device is pointed to the ISO you just downloaded. Boot up the machine and keep hitting enter until you’re done (you may to choose ‘yes’ instead of ‘no’ once during the partitioning sequence). I chose not to install the LAMP version because I’m going to download xampp to host the web site, but do whatever you’re more comfortable with. The whole installation process took about 10 minutes on my 2.6 GHz P4 dual-core with 2 gigs of RAM, but VMware’s cloning feature means you only have to go through this once. I suggest you update the system with a sudo apt-get upgrade upon logging on for the first time.
  3. Setup XAMPP (or roll your own LAMP)
    Over time I’ve found that downloading xampp, untarring it, and running sudo /opt/lampp/lampp start is a lot easier than setting up Apache, PHP and MySQL seperately. The easiest way to download xampp to the Ubuntu server session is to find a mirror from sourceforge and use wget. For example, here’s how I installed it from the command prompt:

    wget http://superb-west.dl.sourceforge.net/sourceforge/xampp/xampp-linux-1.5.4.tar.gz
    sudo tar xvfz xampp-linux-1.5.4.tar.gz -C /opt
    sudo /opt/lampp/lampp start

  4. Setup the shared folder for the document root
    Before you can install VMware tools and allow folders to be shared, you have to download the right tools. Enter this at the command prompt:

    sudo apt-get install make gcc linux-headers-`uname -r` build-essential xinetd

    Now, to install VMware tools, on the menu bar click ‘VM’ and then ‘Install VMware Tools…’. This will load the tools on a CD on your session. To actually instally the tools run these commands:

    sudo mount /dev/cdrom
    cd /tmp
    tar zxf /media/cdrom0/vmware-linux-tools.tar.gz
    umount /dev/cdrom
    cd vmware-tools-distrib
    sudo ./vmware-install.pl

    To share the folder find the menu bar, click on ‘VM’, click on ‘Settings…’, click on ‘Options’, click on ‘Shared folders’ and finally click ‘Add…’. Enter a name for the folder, find your project directory and hit ‘OK’. Your project directory will now be located in the Ubuntu session at: /mnt/hgfs/[your project directory]. If you are using xampp, you can point your htdocs directory here by typing the following:

    sudo rm -rf /opt/lampp/htdocs
    sudo ln -s /mnt/hgfs/[your project directory] /opt/lampp/htdocs

That’s it! If you don’t want to go through all the hastle again, I suggest you clone your VMware session now and just keep cloning it everytime you want to make a new server. To view the web site, just point your browser to the server’s IP address. Let me know what you think, I’m off to create a new session to host my own Wordpress blog.

Send email with PHP and GMail hosted for your domain

I’ve been using Linux ever since I became a CS major at Georgia Tech five years ago. I’ve compiled my own kernels, maintained Apache and MySQL, and written custom IPTable’s; I’ve even setup printing AND scanning with modern hardware. But not once have I ever come close to setting up a working mail server.

On second though, I think I may have come close once. I sent out an email to myself and then sat there at my GMail inbox hitting refresh, waiting to receive the message. About five minutes later I found it in the spam folder and started reading about reverse IP pointers and DomainKeys. My brain started to hurt and in the end, I was sending messages through godaddy.com’s email servers and hoping most of my customers got their emails.

Now that GMail for your domain has arrived (if you’re a lucky beta winner, that is) it’s like hiring a world-class email server engineer and paying them nothing. It takes a little work because of GMail’s security requirements, but you end up with a rock solid email server that won’t send messages straight into spam folders.

First, download the standard PHPMailer libraries (version 1.73). The only thing you have to modify is the class.smtp.php file. Open it and go down to about line 683, in the Quit() member function. Comment out everything between the fputs() that sends the “quit” command and the if statement surrounding $this->Close() except for $rval = true;. The reason this works is unknown to me, but every time I tried to read from the socket after sending the “quit” command I got a lot of garbage.

Now just use the following settings:

$mail->From = '***@&&&.com';
$mail->Mailer = 'smtp';
$mail->Host = 'ssl://smtp.gmail.com';
$mail->Port = 465;
$mail->SMTPAuth = true;
$mail->Username = '***@&&&.com';
$mail->Password = '***';

Replace the *’s and &’s for real values and you’re all set.