Headers to Send to Force a Download in PHP

It can be difficult sometimes to force a web browser to popup a dialog to let someone download a file. A lot of times with things like a pdf or Microsoft Office files (Word, Excel, etc), browsers like Internet Explorer will just open them with a sub program in the browser. This can be annoying for both the developer and user. However to be honest with you, I am not sure if this is as big of an issue as it was a few years ago (with the way browsers are changing to enhance security).

Anyway, if you want to force a browser to accept a file as a download instead of opening it, I have found it is best to just be as vague as you can in your headers so the browser wont know what to do with it besides ask the user. Below is what I have used for years and it always seems to work no matter what browser or computer you are on.

header('Content-Type: application/force-download');
header('Content-Disposition: attachment; filename="myfile.abc"');
header('Content-Length: ' . filesize('myfile.abc'));

readfile('myfile.abc');

This is something I just want to log for posterity out of an old email of mine.

HyperCard Making a Comeback

HyperCard was a development platform on the Mac that featured some of the ideas that were used in the foundations of the web (things like the hyperlink and element scripting). It also was the first platform that I really got into developing for. The programs you write for it work kinda like a slide show that can be improved by scripting elements you place on each slide (things like buttons or fields to switch slides or do lookups and calculations).

Anyway, this website is implementing a web based version of it. It looks pretty interesting and they are making the scripting for it (called Speak) to be almost fully backwards compatible to HyperTalk (HyperCard's built-in language). I signed up for the beta but haven't got my invitation yet to try it out. Looks like it could be fun.

http://www.tilestack.com

Five Worthwhile Applications to Track Your Website

There are literally hundreds of applications and third-party services on the Internet that a business can use to track and analyze their website traffic. However many of them suffer from one of the following problems:

  • offers too little or too much information
  • overly complex statistics interface
  • poorly illustrates data
  • difficulty to install and configure
  • costly

Instead of bombarding you with a huge spread of software no business would have the time to wade through, here is a succinct list of free or low cost analytic options that are considered to be the cream of the crop.

Google Analytics
http://www.google.com/analytics

Google Analytics is a third-party service that can run on any web server without requiring special software. It is easy to install (only a few lines of javascript). One of its best features is that it is free to use. This has pretty much become the industry standard anymore when it comes to modern web analytics. It provides a good amount of basic information and it has a great interface.

CrazyEgg
http://crazyegg.com

CrazyEgg is an interesting third-party service that is also easy to install and bears no server requirements. What makes it unique is that it offers a number of overlays that will track what areas are popular to “click on” in website pages. In other words, it can give a good idea which areas within the site people are giving their attention. They offer a limited free package but it is rather inexpensive to upgrade.

103bees
http://103bees.com

This one is a third-party service as well and just as easy to install as the others. It is free to try out and very inexpensive to upgrade. It does, however, have a different focus than the others in that it is built around organic traffic (traffic from search engine or links from other websites). While the others mentioned do provide information in this regard, 103bees is built around it. This gives a refreshing perspective that is similar to a visitor’s journey to and through the site instead of just a report of what, when and how many.

Clicky
http://www.getclicky.com

Clicky is also a easy to install, third-party service. However this service offers something that none of these others do (and I think it is one of the most valuable) - it will show the path each individual user took on your site. You can view what page a visitor entered on and where they went from there. This can give really valuable insight into how visitors look for and digest information on the site. Also the higher level, paid versions offer ways to tweak the service to specific needs. It takes a programmer to set it up but it could be a good way to enhance traffic analysis.

There is one major drawback with this service however; the free package puts ads on the website it is tracking and for business this may not be good. You can upgrade to an ad free, paid version.

Mint
http://www.haveamint.com

This last one is for all of those businesses who either cannot or do not want to share their web traffic data with a third-party service. It is an inexpensive solution that can be installed on almost any web server out there (requires Apache, MySQL and PHP) and it will give you a good baseline of information.

The statistics interface is very pleasing to look at, however the data could be organized a little better and it offers little to no explanation as to what the statistics mean. It seems like a good solution for those who already have their feet wet with web statistics.

- - - - -

This is an article I originally wrote for COSE Mindspring. I have posted it here for personal archival purposes. You can find the original article here.

Firefox 3 Download Day 2008

The new version of Firefox (version 3) is coming out on Tuesday, June 17 and they are trying to set a world record for the most downloads within a twenty four hour period.

From using the betas and release candidates, this version of Firefox is the best yet. It fixes a lot of issues with stability and memory usage; and it also passes the Acid2 test and scores a 71 out 100 on the Acid3 test (using RC3). They also made some good improvements in integrating the UI into each platform (Windows, Mac and Linux).

So if you are a Firefox fan please support them by making a point to download the new Firefox on Tuesday. You can get more details about the world record and Firefox by clicking on the image below.

Download Day 2008

 

Parsing the Query String in Javascript

Here is a couple of query string parsing functions I put together in Javascript today. This first one will parse the current query string and return an object with properties for each of the arguments in the query string.

function parse_args() {
    var args = new Object();
    var query = window.location.search.substr(1).split('&');
    var tmp = null;

    for(var i = 0; i < query.length; i++) {
        tmp = query.split('=');
        args[tmp[0]] = tmp[1];
    }

    return args;
}

This second function will find an argument in the query string by name and then return the value, if it exists. If the argument is not found it will return false.

function find_arg_value(name) {
    var query = window.location.search.substr(1).split('&');
    var tmp = null;

    for(var i = 0; i < query.length; i++) {
        tmp = query[i].split('=');
        if(tmp[0] == name) {
            return tmp[1];
        }
    }

    return false;
}

Multiple CSS Classes on an Element

One of the lesser known features of Cascading Style Sheets is the ability to apply more than one class to a single element. Browser support for this is pretty good and goes back at least as far as Internet Explorer 6. I needed to put this feature to use, so I did a few experiments on how it works and thought I would share my notes here.

To give an element more than one class you just separate the class names with spaces. Below is an example of a div with three classes.

A couple of things I found about using multiple classes are:

  1. The order of the classes makes no difference. class="one two" is the same as class="two one". Standard inheritance applies and if the classes are at the same level of inheritance (i.e. both classes are defined in style tags) the class that is defined last will have its rules take priority.
  2. In Javascript, the className property returns all class names in a string (getAttribute('class') returns the same, doesn't work in IE6).
  3. Also in Javascript if you are looking for a particular class it can be handy to use split to divide the className property into an array.
    String(document.getElementById('test').className).split(' ')

You can see the example code in action by clicking here. Take a look at the source to see how the multiple classes interact and override each other the generate the final look of the div.

Hyperlink Syntax for mailto

It has been so long since I have needed to create a hyperlink involving an email address, it is good to know there are good resources out there to explain the advanced argument syntax.

http://www.ianr.unl.edu/internet/mailto.html

Database Schema Searching Utility

This is a php script that I wrote a number of years ago. It allows you to search through schema of MySQL databases by table column name. It can connect to multiple servers and it will search the structure of every table in every database.

It is a great tool to find related columns in a complex server situation. You can get the source code here, feel free to download and use it.

I found this in an old email that I sent to myself. I wrote this utility at my first job out of college. They had a software system that had been through a number of reincarnations over the years and they were left with redundant data across numerous servers and databases. I used this script to locate related fields across systems and would then write translation code that would merge data into a consolidated system.

Javascript Form Validation Template Code

I found this code snippet in a four year old email that I had sent myself. This is the basic code layout that I use for form validation. What is nice about it is that it will parse the whole form and compile a list of all the form validation errors to present to the user at once, instead of presenting it to the user one field at a time and making them hit the submit button over and over again to get the form right.

function verify() {
    var valid = true;
    var info = "Please correct the following:";

    // Below are some example tests that can make the valid flag false and
    // add text to the error message.

    if(document.getElementById('name').value == "") {
        info += "\n - enter your name";
        valid = false;
    }

    if(document.getElementById('zip').value.length != 5 ||
       document.getElementById('zip').value.match(/[^0-9]/g) !== null) {
        info += "\n - zip must be five numbers";
        valid = false;
    }

    // The above are just examples you can add as many tests as you want.

    if(!valid) {
        alert(info);
    }

    return valid;
}

The validation function maintains a flag that tracks whether any errors have been found. You can do as many tests on the form as you want and keep adding to the error message so the user can see everything that is wrong with the form all at once.

Losing Rights to Business Assets Through the use of Online Services

The growth of the Internet and its uses has enabled business to operate in new and profound ways. Businesses can now use online services that allow them to sit across the conference table from people around the world (Skype) or have multiple people work on a spreadsheet at the same time (Google Docs).

Capabilities aside, one of the greatest benefits about these online services is the price point. Many great online services are free or very cheap to use. However the savings received as a user of an online service forces the business behind the service to seek alternate means of monetizing it.

The most popular way to monetize an online service is through effective advertising to its users. This process generally involves a degree of information sharing to the advertiser. If that information happens to be internal documents, potentially filled with ideas and trade secrets, so be it.

In fact, in the fall of last year there was a brief scare that Google, by way of their user agreement, was claiming the right to reproduce and distribute the documents created with Google Docs. This however turned out to be false - it only applies to documents specified as 'public' (see article resources for more information).

Still there is a lesson to be learned. Businesses must exercise great caution when using online services, especially when it comes to free ones. Be sure to thoroughly read all user agreements before agreeing or using a service. It would seem that this is common sense, but the truth is that many people and businesses use things like online email services and document sharing services without giving it a second thought.

If ever unsure about what a section of a user agreement implies, have a lawyer look at it or ask for clarification from the service provider. Historically speaking online agreements have been proven enforceable; be sure not to agree to something that may be regretted down the road.

All in all online services can be a great asset to business as long as care is taken in what services are chosen and the types of information shared.

Educational Resources

The Content in Google Apps Belongs to Google
Does Google own your content?
Gasp! Google Adapts and Modifies Your Docs & Spreadsheets Content?! Yes, But...
Warning: Google Docs Is NOT Safe

- - - - -

This is an article I originally wrote for COSE Mindspring. I have posted it here for personal archival purposes. You can find the original article here.

{
}