Archive for July, 2008

PHP __set and __get Magic methods

Tuesday, July 29th, 2008

Object oriented programming principles encourage encapsulation, i.e. containing all the functionality inside a class. Usually this means member variables should be private or protected, one of the common ways to do this is with various set and get methods. In the case of a user class it may have setName, getName, setAge, getAge and so on. This can be time consuming to set up, change and maintain. The magic methods __set and __get provide a quick way to implement generic set and get methods.

<?php

 

class Foo{

   

    private $name;

    private $age;

   

    public function setName($name){

    [...]

Recording users by country

Saturday, July 19th, 2008

Detecting a user’s location requires recording and analysing their IP address. Different IP ranges are assigned to different countries. This isn’t always full proof, for example if the ISP is in another country from the user it will be recorded incorrectly. After taking note of the IP the easiest way to detect the country is to use a predefined database such as ip2nation (http://www.ip2nation.com/ ). Ip2nation is a free mySQL database of IP’s and countries. However it is isn’t documented, so takes a moment to work out how you want to use it. You can download it here: http://www.ip2nation.com/ip2nation/Download

SELECT c.country, [...]

Sharing your Google reader shared items

Saturday, July 12th, 2008

I use Google reader all the time, and its cool sharing items that you read about. The only problem is I don’t know many people that use Google Reader and therefore not that many of them see what I share.
So how can we get around this? Well I found out thanks to this website that you can get a RSS feed for your shared items! So I could just hand that out to people but, it would be nice to display it somewhere. So I thought why stop there? My url incase you want to know is:
http://www.google.com/reader/public/atom/user/01019558499128141290/state/com.google/broadcast
So, I then [...]

JavaScript: Detecting Caps lock

Wednesday, July 2nd, 2008

I wanted to see if you could detect caps lock in JavaScript. Why? As a small usability touch, basically alerting users that caps lock is enabled when they are entering passwords for example. I’ve wrapped up the logic in a simple function that can help you detect caps lock on a key press. Here is the demo and following is the function followed by a simple usage.

function isCapslock(e){

   

    e = (e) ? e : window.event;

   

    var charCode = false;

    if (e.which) {

        charCode = e.which;

    } else if (e.keyCode) [...]