Linux Terminal Reference

I’ve put up another reference gist, this time for general terminal commands and common config file locations in Linux. Will try to remember to update this more with some useful / rarely used and easily forgotten stuffs.

Posted in Linux | Tagged Terminal | Leave a comment

Git Reference

I have put up a gist of my Git cheat sheet that has been developing since the switch from SVN earlier this year, also testing a Gist embed plugin….

Posted in Git | Tagged Reference, Version Control | Leave a comment

Running PHP Scripts With Cron

Yesterday I came to a problem which could be solved by a PHP script ran at regular intervals on a web server, and as I have not touched crontab in a while I thought it worth a quick summary.

Without a native way for PHP scripts to schedule tasks it is often useful to couple server-side scripting with user-level scheduling using cron.

Running A PHP Script From CLI

To make a PHP file executable, the location of the PHP interpreter must be included:


Make sure this appears first in the file with no hidden characters at the end of the line (open with vim to be sure, some editors like nano can hide some characters).

A permissions change is also required to make the script executable:

chmod +x []

This will then allow you to simply call the file from terminal.

Alternatively the script can be passed straight to the php CLI tool using the -f flag:

php -f [script.php]

In the PHP script itself it is often best to stick to absolute paths for includes, you will not have the benefit of $_SERVER variables normally populated by Apache and if you use relative paths you are likely to have a bad time.

Running The Cron Job

To edit the crontab file for a user:

crontab -e

Each line in a user’s crontab will run a command at the defined intervals, with five whitespace delimited values for the intervals followed by the command itself:

minute hour dayofmonth month dayofweek command

Using * means all, so * for hour would run the command at the defined point in every hour. You can also use the format */x to tell a command to run at every x increment. For example:

0 2 * * 0 php -f script.php //2am, every Sunday
1 0 * * * php -f script.php //One past midnight, every day
*/1 * * * * php -f script.php //Once a minute

You can check the lines from a user’s crontab using the -l flag:

crontab -l

It can also be useful to check that the service is running using ps and grep:

ps -A | grep cron

Control the service with:

sudo /etc/init.d/cron start
sudo /etc/init.d/cron stop
sudo /etc/init.d/cron restart

Note that some distros seem to have crond instead of cron here.

A simple method I have used in the past to check if a crontab file is running as and when intended is to add a touch command with the same scheduling as the command you are setting up e.g.:

*/1 * * * * touch ~/tempfile //touch tempfile in home directory every minute

Posted in Linux | Tagged Cron, PHP | 1 Comment

Using Google Analytics For Keyword Research –

I have written another Google Analytics post on The Online Retailing Blog on the topic of “Using Google Analytics For Keyword Research“.

Posted in | Tagged Google Analytics | Leave a comment

PHP Database Based Session Handling

Due to a move to a multi-server environment at work to cope with peak time performance I was asked to implement a replacement to PHP’s standard session handling so that sessions would work in this new environment.

In PHP 5.4 it is possible to use a simple function for setting a replacement session handler if it implements the SessionHandlerInterface however for compatibility I settled for the older format. Which in the case of my class resulted in:

From the above sample it should be clear to see that we require a database connection (injected into the class to reduce coupling) as well as defined methods for CRUD (create, read, update, delete), a method to handle garbage collection as well as open / close methods for storage setup and of course a construct for taking the database object at time of instantiation.

Let’s go through these methods in turn.


Simple taking and storing of a PDO object, the type hint will ensure that only a valid database connection will be accepted. Note that PHP type hints will only work with PHP 5.3+, this could be replaced with a simple is_a check.


This is where we need to ensure that our data storage is ready. In our case this is of course a database table so we need to ensure it exists, and create it if it does not. The parameters passed to the method are not actually used in this instance but must still be included in the method definition.


This is the counter method to __construct / open, here we can simply null the database connection object.


The R in CRUD, this function takes a sessionId and returns the session data, which leads to a simple SELECT query method.

The only point of note here really is the commented out section. I had added this for some extra error checking however the behaviour of PHP’s session storage caused this to break the whole thing. This seemed to be due to PHP doing a read before a write for a given session, I am assuming this is a mechanism for avoiding collisions.


This method forms both the C and U in CRUD, being responsible for both creation and updating of session data by id, this naturally leads to use of the REPLACE SQL statement which will update a record with a matching primary key and insert otherwise.


The delete method, nice and simple.


Garbage collection is the process of removing out of date data which is no longer required, in this case we are referring to session data for users who have signed out / timed out. This is simply a case of removing any sessions beyond the max lifetime (set using session.gc_maxlifetime) or with NULL values.

During testing I found it necessary to force garbage collection to ensure that I could see it in action, garbage collection is run on a probability basis (so there is a chance of it being run when a script executes), by default this is 1 in 1000 but this can be altered by changing the session.gc_divisor and session.gc_probability, setting these so that they match will result in garbage collection always being run however remember to change these settings back for production environments as there will otherwise be a hit on performance.


I have put up my complete class as a gist. It should be clear that other mechanisms such as file storage could just as easily be used to replace standard storage be implementing the same methods as above. Replacing your session storage can be a requirement for an application depending on your environment, however it has other desirable aspects not the least of which being the ability to separate out session storage for different systems under a single domain.

Posted in PHP | Tagged Databases, PHP, Sessions | Leave a comment

Cloning And Expanding .vdi Files

After a lot of frustration with attempting to clone / expand a .vdi file for Oracle VM VirtualBox I found a CloneVDI GUI tool on the forums which simply does the job.

Hoping this saves others the annoyance of trying the command line utilities which simply do not work. UUID values for hdds don’t seem to be recognised and using named files generates errors of the type “Cannot register hard drive as already registered” which is not the intent of using the “VBoxManage clonehd” command, many thanks to the author of the tool which got me around the problem :)

Why Oracle didn’t have a simple way of doing this type of operation in the existing GUI I don’t know… guess it’ll teach me to ensure that I take account for database data when setting the original max size :P

Edit: The partition will need to be extended (with something like gparted) to use the now unallocated space within the VM, this may involve removing your swap space which will mean that it will need resetting to avoid a major hit on performance.

Posted in Windows | Tagged Oracle, Virtual Machines, VirtualBox | Leave a comment

Improve Your Search Rankings and Usability with Page Speed Optimisation –

I have written a new post over on The Online Retailing Blog on the topic of “Improve Your Search Rankings and Usability with Page Speed Optimisation“.

Posted in | Tagged Page Speed, SEO, Usability | Leave a comment

PHP cURL Wrapper

I’ve put up a gist of a simple PHP cURL wrapper which simplifies basic content fetching with cURL. The class throws standard Exceptions in case of no content being returned or incorrect settings being passed as arguments.


Simple setters for timeouts:

The object’s methods return the instantiated object itself to make chaining possible, for example:

Note: PHP must be compiled with the –with-curl[=DIR] flag

Update: I’ve updated the gist with changes made by Barry Parkin. Adds some extra validation as apparently FILTER_VALIDATE_URL is not to be trusted as it will let through many styles of rubbish. Additionally Barry has added an optional headers only argument to fetch:

Posted in PHP | Tagged cURL, PHP | Leave a comment

Generic Archive Page WordPress Template

Now that I have a few posts up I realised it would probably be a good idea to add an archive links page to my blog.  As the standard Twenty Ten WordPress theme does not have one of these I decided to write one for my child theme.

This only involved a few functions from the WordPress codex for fetching the required information and the template should be good for any theme, just drop it into the theme’s folder in …wp_content/themes/yourtheme/ and you will have an ‘Archive’ option when making a page.

The template can be found here, and you can see it in action here.

Posted in WordPress | Tagged PHP, Templates | Leave a comment

£50 AdWords Vouchers

I was recently invited to Google Engage and as part of the program was given a stack of £50 voucher codes for introducing people to Google Adwords. I still have lots of these to give away, so if you or anyone you know is interested in starting search advertising for a site, then send me a message and I will mail over a code for £50 credit on new accounts, valid until 30th Aug.

Posted in Marketing | Tagged Google AdWords, PPC | Leave a comment