James Little

Google Maps Precache

leave a comment

I’m off to Switzerland tomorrow and this is one of the reasons I love my Android phone:

A Google Maps Precache in progress on Android

Now I can switch my data off (and save a load of cash) without getting lost, as the GPS will function as normal.

Do cool features like this help make carrier contracts a bit obsolete? I think so; there’s so many ways of working round expensive data costs when you have a decent smartphone. That’s why I’ve ditched my contract and gone with Giffgaff.

To enable this on your Android on a recentish version of Google maps, just go to Settings > Labs > Precache Map Area. Now when you click on an address you’ll get the option to precache, which grabs the 10 square miles around it. The one above (of Basel) costs 6MB of disk space, a small price to pay not to have to wonder around trying to find stuff in a language I don’t really understand. :)

Written by James Little

January 11th, 2012 at 10:48 pm

Posted in Android,giffgaff

Giffgaff Review

52 comments

giffgaff - the mobile network run by you

Ditch your phone contract! ‘Coz with Giffgaff you can get unlimited data, 250 minutes any time/network and unlimited SMS for £10/month. And they’ll even throw in a FREE FIVER when you activate your SIM.

I’ve been with Giffgaff for eight months now, and I’m impressed. They operate on the O2 network, so they benefit from their infrastructure and signal quality, which seems to be pretty decent. At least it’s been rare that I’ve not had a 3G signal down here on the south coast, even in fairly rural areas. Anyway, this article is mainly to get you thinking about whether you need to be tied to that (expensive, long) mobile phone contract. In short, on giffgaff I’m currently spending £10/month in total including unlimited data, with no contract, and no hidden obligations. You order a SIM card (for free), it arrives, you top it up, and you forget about it. How do I get by on a tenner? Simples:

  • 250 minutes of call time, any UK network, any time of day.
  • Unlimited Texts
  • Unlimited Data usage

Read the rest of this entry »

Written by James Little

March 30th, 2011 at 4:58 am

Posted in giffgaff,Reviews

The Asus Eee PC 1005PE & Ubuntu

11 comments

It seems there’s some mixed information about whether the wireless chip in this model works out-of-the-box with Ubuntu. I’ve tested with Karmic and the Lucid Beta (Netbook Remix versions) and for me it didn’t work. The chip in my model is the Atheros AR2427, which although not a new chip, seems to have an an unfamiliar hardware device ID of 002c, and so is not picked up by the ath9k wifi driver found in the kernel. The driver has been patched by the Linux Wireless developers but this patch has not worked its way into a kernel release yet, so the solution is to compile and install the latest bleeding edge wireless drivers, which is actually a pretty painless operation. It’s also a very safe operation, because the installer leaves the original kernel drivers intact and provides an uninstall script; but I have not needed to revert.

Read the rest of this entry »

Written by James Little

March 31st, 2010 at 2:13 pm

Posted in Linux

Tagged with , , ,

Call to undefined function imagetypes()

leave a comment

I came across this PHP error after installing the WordPress plugin Contact Form 7 and then using Really Simple CAPTCHA. The exact error given was:
Fatal error: Call to undefined function imagetypes() in ... on line 201.

After searching around for a while, I found that the GD library is required on the server, and (on linux at least) it’s a simple case of installing the library from the repository. So on Ubuntu/Debian, run:
sudo apt-get install php5-gd

and on Redhat/CentOS, etc. sudo yum install php-gd should do the trick. This will install several dependencies, such as the underlying C Library itself.

Obviously if you don’t have full control over your server, you will have to ask your hosting company or administrator to do this for you.

Written by James Little

October 22nd, 2009 at 8:56 am

Posted in Linux,PHP

Tagged with , ,

Macbook Uptime

2 comments

I knew there was a reason I bought a Mac! 47 days of uptime is pretty impressive for any non-server system, let alone a laptop which often rattles around in my bag (when cycling), comes on holiday, stays “asleep” for a whole weekend, etc. etc.

My Macbook's Uptime

My Macbook's Uptime

And I guess that’s the point really: the sleep function on Apple’s laptops “just works”. In this case I only had to reboot after 47 days for a system update. Of course, it’s generally easy to avoid reboots on most *nix systems, but I’m yet to find a Linux desktop distro/hardware combination where the sleep function reliably works. If anyone out there would like to correct me, that would be most appreciated :)

Also, who can beat 47 days on a Macbook (or MB Pro)?

Written by James Little

October 21st, 2009 at 3:46 pm

Posted in Hardware

Tagged with ,

Detect visitor’s country with PHP & MySQL

14 comments

Of course you don’t have to use PHP, or MySQL for that matter. But it’s my method of choice for most web apps, and it’s also a pretty common one. The general gist is to do a lookup on a database of geographical locations for IP addresses, having taken your visitor’s IP address from the PHP superglobal array $_SERVER. Yes there are caveats: the database is not 100% complete/accurate, and some ISPs (like AOL!) use proxies across different countries so the user will appear to come from somewhere other than their true country of origin. Boo hoo, let’s do it anyway; according to MaxMind, their free(!) GeoLite Country database is 99.3% accurate, and their licensed version, 99.8%.

The database is released monthly in CSV format, so I’ll have to import it into MySQL using mysqlimport, or LOAD DATA INFILE. I prefer the first option. Those of you that are MySQL fans probably know that there is a CSV storage engine available, but that’s only in version 5.1 which is still in Release Candidate stage, so I’ll stick to mysqlimport.

Download the GeoLite database from Maxmind, extract the CSV file and rename it to something more convenient; mysqlimport uses the filename for the name of the MySQL table it imports into:

root@jim-desktop:/home/jim/data# wget http://www.maxmind.com/download/geoip/database/GeoIPCountryCSV.zip
root@jim-desktop:/home/jim/data# unzip GeoIPCountryCSV.zip
root@jim-desktop:/home/jim/data# mv GeoIPCountryWhois.csv geo_csv.csv

Before we import the data into MySQL we need to create a table for it to go into. The following DDL accurately describes the structure of the data. Obviously create a new database if you want; here I have one called geo_ip:

CREATE TABLE  `geo_ip`.`geo_csv` (
 `start_ip` char(15) NOT NULL,
 `end_ip` char(15) NOT NULL,
 `start` int(10) unsigned NOT NULL,
 `end` int(10) unsigned NOT NULL,
 `cc` char(2) NOT NULL,
 `cn` varchar(50) NOT NULL
 ) ENGINE=MyISAM DEFAULT CHARSET=latin1

If you look at the data in the CSV file you’ll see it’s delimited by commas and the text is qualified by double quotes. With that in mind, we use the following statement to import the data:

root@jim-desktop:/home/jim/data# mysqlimport --fields-terminated-by=","  --fields-optionally-enclosed-by="\"" --lines-terminated-by="\n" geo_ip /home/jim/data/geo_csv.csv
geo_ip.geo_csv: Records: 102957  Deleted: 0  Skipped: 0  Warnings: 0

If the mysqlimport binary is not in your environment path, use locate to find it. If you don’t have it at all then use LOAD DATA INFILE.

So we now have the raw data imported into MySQL, but how do we use it? First let’s take a look at the data:

mysql> select * from geo_csv order by rand() limit 10;
 +---------------+----------------+------------+------------+----+----------------+
 | start_ip      | end_ip         | start      | end        | cc | cn             |
 +---------------+----------------+------------+------------+----+----------------+
 | 207.209.7.0   | 207.209.7.255  | 3486582528 | 3486582783 | AU | Australia      |
 | 79.99.200.0   | 79.99.207.255  | 1331939328 | 1331941375 | BE | Belgium        |
 | 217.27.192.0  | 217.27.207.255 | 3642474496 | 3642478591 | DE | Germany        |
 | 194.59.180.0  | 194.59.180.255 | 3258692608 | 3258692863 | FR | France         |
 | 81.16.160.0   | 81.16.175.255  | 1360044032 | 1360048127 | SE | Sweden         |
 | 62.23.198.192 | 62.23.198.207  | 1041745600 | 1041745615 | GB | United Kingdom |
 | 64.49.231.240 | 64.49.232.15   | 1077012464 | 1077012495 | US | United States  |
 | 83.217.68.32  | 83.217.68.95   | 1406747680 | 1406747743 | BE | Belgium        |
 | 91.193.20.0   | 91.193.23.255  | 1539380224 | 1539381247 | CH | Switzerland    |
 | 194.37.249.0  | 194.37.249.255 | 3257268480 | 3257268735 | SE | Sweden         |
 +---------------+----------------+------------+------------+----+----------------+
 10 rows in set (0.22 sec)

The table is essentially a big list (~103k records) of IP ranges, given in both dot-decimal and decimal form. The decimal form is the most efficient to search on as the datatype int requires less memory than char, and with integers we can reliably make use of operators such as greater than, less than, BETWEEN, etc. Exactly how you will use the data will depend on your scenario. I began looking into this when I was working on a German website that wanted to know when a visitor was from Switzerland, so it could display prices in CHF rather than Euros. So in fact, all I needed to know was whether the visitor was Swiss, and if they were from any other country, they would see Euros. So the only columns I’ll need from the table are start and end, and all the rows belonging to Switzerland, or ‘CH’. So to make searches more efficient I’ll grab only the data I need and put it in a new table called ch_ip:

mysql> create table ch_ip as select start,end from geo_csv where cc='CH';
Query OK, 2023 rows affected (0.05 sec)
Records: 2023  Duplicates: 0  Warnings: 0

Great, that’s cut the data from nearly 103 thousand records to just over 2 thousand, and we’ve also lopped off four columns. I’ll now be searching on a table that’s 18K in size, rather than the original 5.3MB. Maybe you need every row of data in your scenario, but in many cases you only need a fraction. And in any case, you really don’t need the start_ip and end_ip columns (as you will see shortly). You could also split off the country names (cn column) into another table so that cc becomes a foreign key. Or you could ditch the country names completely and create an array of CC => CN in your application; there are only 239 unique CCs after all:

mysql> select count(distinct cc) from geo_csv;
 +--------------------+
 | count(distinct cc) |
 +--------------------+
 |                239 |
 +--------------------+
 1 row in set (0.05 sec)

So I have my table of 2,023 Swiss IP ranges. Now I need to grab a visitor’s IP address and convert it into decimal notation. For this we can use PHP’s built-in function ip2long. We use sprintf to ensure the result is always unsigned:

<?php  $ip_num = sprintf("%u", ip2long($_SERVER['REMOTE_ADDR']));  ?>

Once we have $ip_num we can create our MySQL query:

$qry = "SELECT '' FROM ch_ip WHERE $ip_num BETWEEN start AND end";

All we need to know is whether the query returns > 0 rows. If it does, then the visitor is Swiss and we’ll set their locale appropriately. Obviously we don’t want to be performing this query on every page, so once it has been performed once for the visitor we’ll set a session variable. So the final code looks like this:

<?php 

session_start();
if (!session_is_registered("locale")) { //check if the session variable has already been set first
    $con = mysql_connect('localhost', 'geo_user', 'geo_password');
    if ($con) {
        $ip_num = sprintf("%u", ip2long($_SERVER['REMOTE_ADDR']));
        mysql_select_db("geo_ip", $con);
        $result = mysql_query( "SELECT '' FROM ch_ip WHERE $ip_num BETWEEN start AND end" );
        $num_rows = mysql_num_rows($result);
        if ($num_rows > 0) {
            $_SESSION['locale'] = "ch";
        }
        else { $_SESSION['locale'] = "de"; }
    }
    else { $_SESSION['locale'] = "de"; //If no db connection can be made then set their locale to German }
};

?>

Written by James Little

June 7th, 2008 at 6:17 pm

Posted in MySQL,PHP

Tagged with , ,

mysqlslap for MySQL 5.0

7 comments

mysqlslap is a very useful tool for emulating client load, something that would normally be very difficult in the real world (until you go live!). The binary is bundled into the MySQL 5.1 releases (still at Release Candidate stage) but not 5.0, so the only option is to compile from 5.1 source and then you can use it with your 5.0 server installation.

Fortunately when compiling you can save some time by configuring with the --without-server option, which will compile just the client tools (mysqldump, mysqlbinlog, mysql CLI, etc.). The following worked for me on an installation of CentOS5 64-bit, on the same machine that runs my 5.0 server.

1. Download the MySQL 5.1 source code in compressed tar format (.tar.gz). Go to the download page, or do a wget (in my case from the Mirror Service from the University of Kent, UK). Version 5.1.23-rc was current at the time of writing:
wget http://dev.mysql.com/get/Downloads/MySQL-5.1/mysql-5.1.23-rc.tar.gz/from/http://www.mirrorservice.org/sites/ftp.mysql.com/

2. Unpack the archive:
tar -xvvzf mysql-5.1.23-rc.tar.gz

3. Install required development packages and compile:
cd mysql-5.1.23-rc
yum install glibc gcc libtool ncurses-devel gcc-c++
./configure --without-server --disable-shared
make
make install

The ‘make’ stage will take some time. Why configure with --disable-shared? I wanted my binary to be portable so I could share amongst a group of similar-spec machines, and I can’t be sure that the shared libraries are all in the same location. See the MySQL Installation pages for more details on configuration options; you may want to make use of --libdir=... instead.

4. Copy/move the mysqlslap binary (it will be in /usr/local/bin by default) to wherever the rest of your v5.0 client binaries are. For example:
cp /usr/local/bin/mysqlslap /usr/local/mysql/bin/mysqlslap
5. Test it out! Run something like:
./mysqlslap --user=root --auto-generate-sql --concurrency=100 --iterations=5
as a quick test, and check out the MySQL Documentation for a more detailed use-guide.

Since compiling I have thrown the binary around various other Intel machines (and VMs) running CentOS, without any problems. Read the rest of this entry »

Written by James Little

April 2nd, 2008 at 3:18 pm

Posted in Linux,MySQL

Tagged with ,

mprime for Mac

leave a comment

For those of you that don’t know what a Mersenne Prime is (most people I suspect), it’s a prime number of the form 2^n -1 where n is a natural number. The largest known prime number, 2^32,582,657 -1 is a Mersenne prime. Many, no doubt fascinating individuals (like me for example) like the idea of finding the first prime number that is > 10 million digits long because 1) there’s a $100,000 reward and 2) because it would be cool (sort of). Check out GIMPS and Wikipedia for more info.

I searched around for ages for a version of mprime (highly optimized software used to search for Mersenne Primes) compiled for Intel Macs and didn’t have much luck; for some reason it doesn’t seem to be linked from the GIMPS website. Eventually I found a random link to this page which contains all the software downloads. So if you want to contribute to the worldwide distributed search and you use a Mac, please download it. And you never know, you might get lucky!

For those of you that couldn’t care less about Mersenne primes (most people I suspect), but want to do some stress testing/benchmarking on their Mac, mprime is a very useful tool. Overclockers of the Windows variety will be familiar with the Prime95 app, a Windows GUI version of mprime.

Written by James Little

March 8th, 2008 at 9:50 pm

Posted in Mathematics

Tagged with ,

Installing CentOS 5 on a Dell R200

16 comments

This is the latest value rackmount offering from Dell. It’s a great spec for the price, but installation of CentOS 5 seems surprisingly difficult, thanks to the SATA DVD drive used in the unit. RHEL/CentOS doesn’t have the driver for the controller so can’t find the DVD during installation. I’m sure there are other workarounds (network install, USB DVD drive etc.), but eventually I got hold of a Redhat driver disk from Dell Support (it doesn’t seem to be on the Dell site btw).

I’ve uploaded the disk image here; just uncompress it and copy the .img file onto a USB key. At the installation command-prompt type ‘linux dd’ and then hit Enter. You’ll then be asked for the location of the driver disk image; just navigate to the USB disk, select the image, and you’re in business.

SATA driver disk
.zip version

Written by James Little

February 15th, 2008 at 7:51 pm

Posted in Hardware,Linux

Tagged with , ,

How to Access MySQL with an SSH Tunnel

3 comments

This is a particularly useful method for gaining access to your remote MySQL databases, such as those held on a web hosting account where the MySQL port may not be open. You can use this method to gain access to other services too (SMTP, IMAP, FTP), but in this post I’ll explain how I use it in combination with MySQL Query Browser to administrate my DBs with a GUI. You need to have SSH access to your remote server (normally over port 22) for this to work. My instructions are for Ubuntu but it’s easily transferred to other distros, Mac OS X, and Windows (just download an SSH client).

Run sudo apt-get install ssh if it isn’t installed already, which will install several SSH connectivity tools (more info here). Query Browser is an excellent tool with which to run queries, updates, create views and stored procedures, and loads more besides. Run sudo apt-get install mysql-query-browser. Now to create the SSH tunnel by using port forwarding; here’s how I access a MySQL instance on my local network:
james@james-laptop:~$ ssh -L 3307:localhost:3306 root@192.168.1.211
root@192.168.1.211's password:

Essentially this forwards all traffic on port 3307 on the local machine (james-laptop) to port 3306 on 192.168.1.211. The general format is
ssh -L localport:host:hostport
. Note that in my example I used localhost, but this is resolved after the connection has been made to 192.168.1.211 and so it refers to that IP address.

Effectively we can now access port 3306 (the default MySQL port) on 192.168.1.211 via port 3307 on james-laptop even though port 22 (the SSH port) is the only port open on 192.168.1.211. Keep the connection open (i.e. don’t close the terminal) and open Query Browser. In the connection dialogue set the hostname to 127.0.0.1 and the port to 3307 and enter a username/password as required. Hit connect and you should see a graphical representation of your database(s). Note that in *nix OSs (including Mac OS X) you must use 127.0.0.1 rather than ‘localhost’ or the connection will be made via a named pipe rather than TCP.

Written by James Little

September 10th, 2007 at 10:18 pm

Posted in Linux,MySQL

Tagged with , ,