Mar 8 2010

HTTP Header to Force Browser Download

A content disposition header can be used to tell web browsers to ask the user where they want to save the file (and download it) instead of actually opening the file in a browser tab/window. Very handy if you want to have a download button.

Content-Disposition: attachment; filename="filename.mp3"

Tested in: Firefox, Safari, IE, Opera, and Google Chrome


Mar 2 2010

PHP Session Sharing Among Subdomains

PHP supports sharing sessions among subdomains which can be very useful for sharing cookies and sessions among multiple web applications.

I wanted my users to remain logged in while navigating from www.example.com to site1.example.com. By default PHP will treat these domains as two separate sites and users would have to login to each subdomain.

All you have to do is to either set the following setting in your php.ini file:
session.cookie_domain = .example.com

Or, set it from within your code before you create your session:
ini_set("session.cookie_domain", ".example.com");
session_start();


Dec 22 2009

Facebook vs You!

Have you had the… how should I describe it… experience of writing a Facebook application? No? OK, here’s an introduction to what you can expect.

When developing an application you must learn Rule #1 Facebook is the gatekeeper. Everything you do, be it HTML, CSS or JavaScript, is restricted by Facebook. Only the commands, functions, CSS properties, HTML tags etc. that Facebook allows are permissible. If you have an error in your HTML such as an unclosed tag or if you try to use a CSS property that does not exist, Facebook will render a very nice message saying you made an error.

Rule #2 The never ending cycle. The Facebook platform is constantly changing and APIs that you are using in your application could disappear one day without any notice. It’s the life of a Facebook application, working one instant, inexplicably broken the next. Be prepared for irregular maintenance just to keep your application working.

A Facebook Application lives in the world of Facebook and while it might initially seem like you’re developing a standard web application you are not. Rule #3 You’re application is on the Facebook web. It’s not a regular web application which is a very slight but critical distinction. Facebook has used their powers as the gatekeeper to make modifications to the web. Some HTML tags are allowed, some are not. JavaScript has been altered to remove a lot of its power and replace only some of them with Facebook specific functions and even CSS has restrictions. Don’t expect existing code to function when placed in the Facebook web.

Additional Caveats

  • You cannot include any external JavaScript or style sheet files, they must be included in-line. This is so Facebook can parse them and allow only approved commands. Yes it enhances the security of the social networking site as a whole, but it will slow down your development time.
  • Facebook will filter, compile, and drastically alter your original code. Clicking view-source on a Facebook page with your application will show your JavaScript code mushed into awkward Facebook functions.

Do you see that it’s a bit of a battle of you against the mighty Facebook? Don’t sweat too much, other developers have persevered and with a few wounds and time you can build and maintain a successful Facebook application. Good luck!


Dec 7 2009

Quality Software @ Thirdi

I work at Thirdi Software, and one of the initiatives I started was a Quality Software @ Thirdi email newsletter and series of blog posts highlighting quality solutions to software problems. It’s mostly targeted at developers but there’s also some insights to be had for the quality assurance team and for project managers.

The posts were originally posted on Senses and reproduced here for archival purposes. They touch on a lot of basic information for software developers including software design, upcoming technologies and useful tips to make your life easier.

Check the Quality Software page for links to each article.


Oct 20 2009

Intro to Objects in JavaScript

Despite what you might think, JavaScript supports programming in full object oriented style including inheritance, encapsulation and polymorphism. But before we dive into all those topics I'm going to start with the basics and demonstrate how to create and use a simple JavaScript object.

Lets say you have a web application that needs to track how long an AJAX request took. If you were writing it in Ruby, PHP or Python you'd have a timer object that you can start() and stop() and a seconds() method to get the number of seconds that passed between calling the first two methods.

To build this in JavaScript we first create our constructor:

JAVASCRIPT:
  1. Timer = function() {
  2.   var seconds_passed = 0;
  3. }

Here we are creating a Timer object with a private seconds_passed variable. To create this object we we simply create a new timer:

JAVASCRIPT:
  1. var elapsed_timer = new Timer();

This timer object now needs some public methods. We can attach a public method by using this.method_name:

JAVASCRIPT:
  1. Timer = function() {
  2.   var seconds_passed = 0;
  3.  
  4.   this.start = function() {
  5.     // implementation here
  6.   }
  7. }

Our Timer class now has a start() function. Additional public methods can be added in the same way.

To add a private method, don't define it with the this keyword but instead create a regular function inside the class:

JAVASCRIPT:
  1. Timer = function() {
  2.   function increment() {
  3.     // implementation of private method here
  4.   }
  5. }

Now we have a Timer object with a public start method and a private increment method. The rest of the class is fairly simple, so lets jump to the finished solution.

Complete Example

JAVASCRIPT:
  1. Timer = function() {
  2.   var timeout_id = null;
  3.   var seconds_passed = 0;
  4.  
  5.   this.start = function() {
  6.     increment();
  7.   }
  8.  
  9.   this.stop = function() {
  10.     clearTimeout(timeout_id);
  11.     timeout_id = null;
  12.   }
  13.  
  14.   this.seconds = function() {
  15.     return seconds_passed;
  16.   }
  17.  
  18.   function increment() {
  19.     seconds_passed++;
  20.  
  21.     timeout_id = window.setTimeout(
  22.       function() {
  23.         increment();
  24.       }
  25.       , 1000
  26.     );
  27.   }
  28. }
  29.  
  30. var elapsed_timer = new Timer();
  31. elapsed_timer.start();
  32.  
  33. // do some stuff
  34.  
  35. elapsed_timer.stop();
  36. alert(elapsed_timer.seconds());

There you have it. A simple JavaScript timer to keep track of time.