Coding

Ever since I advanced beyond the “hello worlds” of Java, I had never stopped coding. This section is for documenting code usages that I had employed while solving the technological problems that I had encountered.

Connecting to and disconnecting from a MongoDB server in PHP

The first step to manipulate data in the MongoDB ecosystem is to connect to a MongoDB server. In php, we can use the Mongo class to help us connect to one or more MongoDB servers.

For the purpose of this demonstration, let’s assume that we had installed a MongoDB server instance as a windows service which listens on port 12345. In addition, our PHP web server runs on the same machine as the MongoDB instance.

Make your PHP webpage loads faster by aggregating external CSS scripts

In most cases, the speed of which a browser completes the rendering a of webpage is very much dependent on the network connection which connects it to a web server. While some browsers have the luxury of fibre connections, there could be others that are rendering your webpages via a mobile network.

Because each HTTP response consists of the header portion in addition to the actual content, we could reduce the number of bytes that our browsers need to read from our servers by aggregating external CSS and JS scripts.

How to ensure that your Javascript is able to write unicode characters from external Javascript files to your HTML contents

Ever tried injecting Chinese characters into your HTML DOM from an external Javascript file but got something that resembles the following?

Gibberish characters was shown when jQuery attempts to display error message in Chinese

Well that was what I got for the Chinese version of my bilingual form initially as a result of validating an input field as the user is filling up my form.

In this post, I will discuss why I got the unwanted characters and what I did to solve the problem.

Deciding which HTTP status code to use in a HTTP response

When we are in the business of creating web applications, we are always crafting HTTP responses.

A HTTP communication session is initated with a HTTP request from the client and is ended with a HTTP response from the server. In order for a HTTP communication session to take place successfully, the server must be reachable via an IP Address and a port number.

However, a successful HTTP communication session does not imply that the client always get what it wants from the server. Within the HTTP response, the server tells the client whether or not the HTTP request is plausible to fulfill via HTTP status codes.

This is a discussion of some of the status codes that I use at the HTTP server end, in an environment where most of my HTTP clients are robotic in nature.

PHP codes to tell browsers to open the download dialog box for users to download a file

Ideally, when we build web applications which generate files on demand, we will want our users to be able to use their browsers to save a copy of our files in their local harddisk. For instance, the tool for generating customized codes for countries in the world, which I had created earlier, will tell browsers to prompt a download dialog box after users had submitted the form.

Image of firefox dialog box after submitting request to generate custom codes from list of countries in the world.

However, this is not the default behavior of browsers. By default, browsers will show the contents of a HTTP response generated by our PHP script in the browser window. This behaviour can be seen when I point my browser to http://www.techcoil.com/robots.txt.

In this post, I will discuss how we can tell browsers to show a dialog box for users to save the contents generated by a PHP script as a file.

How to compress and decompress files in C#

Files are useful for applications to communicate with others. For instance, web applications can communicate with Google’s crawlers via sitemaps on the Internet. A web application write urls to its pages to its sitemap. When Google’s crawlers come to this web applications, they will download these sitemaps to index urls to the pages of .

To reduce the time needed for files to be trasmitted over the network, it is a good idea for applications to compress files before sending them out to the network, or for a web application, making them available for download via HTTP.

In this post, I document how we can compress and decompress files via the GZip implementation in C# – System.IO.Compression.GZipStream.

How to ensure that your user only runs one instance of your C# program

There are times when we want to limit our users to run an instance of our C# program at any one time. Such situations can be applicable for network monitoring tools, messaging applications, web server monitoring application and etc. In this post, I discuss how we can achieve that via the mutual exclusive (Mutex) mechanism available in C#.

Handy Javascript code segments

The augmentation of Javascript to CSS and HTML is beautiful. While trying out jQuery to create my own tic tac toe game, I realised that there are Javascript/jQuery code segments that could be helpful to me when building web applications.

In this post, I collate Javascript and jQuery code segments that I had utilized in my web projects for ease of future references.