Articles

Prevent People Messing Up Your Goolge Analytics

After running coding sites for a couple years there is one little problem that really annoys me, so when I set up #! code I resolved to fix it. The issue arises when you use some JavaScript based analytics software that allows multiple domains to be used, like Google Analytics. There is nothing wrong with analytics software that allows this, and it is potentially useful for tracking all manner of things. However, some web masters might not be that good at running sites and will lift code from your site (analytics and all) in order to implement a single widget on their site. What this means is that when a user lands on one of their pages it will register as a hit on your site.

Extract Keywords From A Text String With PHP

A common issue I have come across in the past is that I have a CMS system, or an old copy of Wordpress, and I need to create a set of keywords to be used in the meta keywords field. To solve this I put together a simple function that runs through a string and picks out the most commonly used words in that list as an array. This is currently set to be 10, but you can change that quite easily.

The first thing the function defines is a list of "stop" words. This is a list of words that occur quite a bit in English text and would therefore interfere with the outcome of the function. The function also uses a variant of the slug function to remove any odd characters that might be in the text.

PHP Class To Interact With is.gd API

The is.gd URL shortening service is yet another site that converts a long URL into a small one, but is.gd differs in that it generally produces smaller URLs than others. The is.gd API doesn't require that you create an account on the site, but there are one or two limitations to using the service that you should be aware of first. You can find out these limitations by reading the is.gd terms and conditions. You can use this class to interact with is.gd, but the service doesn't have a massive amount of features the class only contains shorten() and expand() functions that work exactly as expected. You can download the class using the following link:

Download the is.gd API interaction PHP class.

How To Check And Uncheck A Checkbox With jQuery

To check and uncheck a checkbox using jQuery you first need to access the checkbox element using the $() function. Once you have done that you can retrieve or change the value of the checkbox quite easily.

To uncheck a checkbox use the following snippet, which makes nice use of the jQuery attribute filters:

$('input[name=mycheckbox]').attr('checked', false);

To check a checkbox use the following:

$('input[name=mycheckbox]').attr('checked', true);

To test if a checkbox is set or not use one of the following, both return true if checked and false is unchecked.

var checked = $('input[name=mycheckbox]').is(':checked');
var checked = $('input[name=mycheckbox]').attr('checked');

Overriding The Poll Module In Drupal 6

The Poll module is a useful little module that comes with Drupal and allows the addition of simple polls to pages or blocks. However, there was one major issue that I wanted to correct on a certain site, but I didn't want to directly edit the core functionality of the module. The default behaviour of the module is to record one vote per IP address or user, which is fine for normal uses but in some situations it does tend to fall over. A Drupal site with one administrator that allow anonymous users to vote on your polls seems fine, but lets say that this site wants votes to come from people in the business world. The problem here is that multiple people might work for the same business, be situtated behind a firewall and therefore would have the same IP address. What this means is that if one person in that company votes on your poll it will block all other people from voting for that poll.

Lazy Instantiation In PHP

Lazy instantiation (also known as lazy load) is an object orientated design pattern that attempts to reduce the amount of resources needed to load an application by only loading certain parts of it if they are needed. This makes sense as you don't need all parts of an application on every page load, so cutting down the data loaded cuts down the resources and processing time needed to load the page.

A good example of this might be when creating a student and courses application or similar. Obviously you would want to link students with their courses, but you don't want to have to load all of the students registered with a course when you are looking at a list of courses. Lazy instantiation allows you to only load the student information when it is called for.

To build this example we will first need a Student class.

Null Response From Drupal 6

I recently had some trouble with a Drupal 6 site I was updating. I wanted to create a local working copy of the site to test so I downloaded the files and backed up the database, but for some reason I couldn't get the site to run. In every browser I tried I would get "page cannot be displayed" or "host unresponsive". These messages were basically telling me that something on the site was causing it to fall over before it ever got around to producing any HTML, and so the browsers were treating it as best they could. I tried clearing the Drupal cache and disabling some modules but this didn't appear to do anything, or simply broke the site. Searching the internet for this problem yielded very few results, so I eventually had to track the problem down myself.

Clearing The Filter Cache In Drupal

Filters are used in Drupal to change the content of the text of a node when it is viewed. The important thing to note is that Drupal filters should never alter the actual content of the node itself. Instead, when a node is saved it stores the output of the filter in the cache_filter table and displays this content the next time the node is viewed. This is useful because it doesn't mess about with the original text, and it speeds up the displaying of the node by running the filters once, rather than every time the node is loaded.

However, there is one thing that you should watch out for when creating your own modules. If you are using any data from other tables in your filters then you will need to clear the cache when this data changes.

PHP Class To Interact With bit.ly API

There are a few URL shortening services about, of which bit.ly is just one. However, it does provide a nice API for developers to interact with the site. Not only is it possible to shorten URL's, you can also reverse them, find out information about the site and get information about how many people have used to link. The API requires you to create an account to interact with the API. Once you have created an account you can use the API service.

Download the bitly API interaction PHP class.

There are several functions available in this class but the main two are shorten() to turn a URL into its bit.ly equivalent and expand(), which will do the exact opposite.

To convert a URL into its bit.ly equivalent you can call the shorten() function like this.

PHP Variable Assignment Within If Statement

The usual practice when checking for the return value of functions is to run the function and store the value in a variable, and then test that variable. Here is an example of that process using the strstr() function.

$string = 'abcde';
$var = strstr($string, 'f');
if ( false !==  $var ) {
    var_dump($var);
} else {
    var_dump($var);
}

This code will output "bool(false)" as that was the return value of the strstr() function.