Posts about the server side scripting language PHP

PHP IP To Location

1st March 2010 - 12 minutes read time

Converting an IP address into some useful location information can be useful if you want to find out where sites are hosted or customise content to users depending on their location.

All this code is freely available over at github.

There are several ways to do this, all of which have their advantages and disadvantages, but sticking with one can cause rewriting a lot of code in the future. So rather than pick one and stick with it I decided to use dependency injection to allow different classes to be used that convert IP addresses to locations in different ways. The first task is to create an abstract class that will be used to construct the rest of the IP location classes. Each class that extends this abstract class will contain a method called getIpLocation() that will convert an IP address into a location, and a method that will update the data source for the location lookup. Rather than lump all of the classes into a single directory I have created a directory called Service, into which all of the different classes that lookup IP addresses will be kept.

Using Authentication And file_get_contents()

15th February 2010 - 2 minutes read time

Using file_get_contents() to fetch the contents of a file is quite a common practice. This might be just to get the contents of a text file or to get the ImageCache module in Drupal to pre-cache images. The file_get_contents() function can get a local or remote file and is usually run like this.

$data = file_get_contents($url);

However, when trying to use this function to communicate with an authenticated server you will see the following error appearing.

Default Function Parameters In PHP

26th January 2010 - 6 minutes read time

When creating functions in PHP it is possible to provide default parameters so that when a parameter is not passed to the function it is still available within the function with a pre-defined value. These default values can also be called optional parameters because they don't need to be passed to the function. I have seen this sort of code being used incorrectly quite often recently so I thought I would go over it in a post.

Creating a default parameter in a function is very simple and is quite like normal variable assignment. The following function has a single parameter that is set to 1 if it is not passed when calling the function.

  1. function testFunction($a = 1)
  2. {
  3. return $a;
  4. }

PHP Overloading

11th December 2009 - 11 minutes read time

Overloading in PHP describes the way in which properties and methods of an object can be dynamically created or accessed without having to define them first. Traditionally, the word overloading in programming is used to describe accessing object methods with the same name but with different parameters. It is not possible to do this in PHP as it will complain about methods having the same name, so the term describes calling a method or accessing a property that hasn't previously been set or is out of scope. In object orientated terms this means that the method or property is private.

Object Property Overloading

Property overloading allows you to access the property of an object through a method without having to write them first. It can also be used to access any properties that are inaccessible. There are two basic property overloading methods available, these are __set() and __get(), both contain a double underscore (_) in their name.

Cross Platform Directory Slashes In PHP

22nd October 2009 - 1 minute read time

I'm not sure where I found this, but I have been using it on a few projects recently and it's helped a lot. It basically detects what system you are on and will give you a constant that keeps hold of the slash for that system.

PHP instanceof Operator

15th October 2009 - 4 minutes read time

The instanceof operator is used in PHP to find out if an object is an instantiated instance of a class. It's quite easy to use and works in the same sort of way as other operators. This can be useful to controlling objects in large applications as you can make sure that a parameter is a particular instance of an object before using it. Lets create a couple of classes as examples.

PHP: The Second Bracket Is Optional

14th October 2009 - 1 minute read time

When writing PHP class or function (basically any file containing only PHP code) files you might have learnt to write them something like this:

  1. <?php
  2. class Users
  3. {
  4. }
  5. ?>

However, did you know that the second bracket is optional? The following class file is perfectly legal:

Advanced Use Of PHP Function strtotime()

19th September 2009 - 12 minutes read time

Finding the next day of the week from a given date can involve some complicated loops and if statements. In PHP it is made quite easy through the use of the strtotime() function. This function, which is part of the PHP core since version 4, can take just about any string representation of the time and convert it into a Unix timestamp.

The most common use of strtotime() is to convert a string into a time. Here are some random examples, the first two convert a date and the second two print more or less the same, but (obviously) will change with time. I say more or less as the "now" parameter will return the current timestamp whereas the "today" parameter will return the timestamp from the beginning of the current day.

Password Validation Class In PHP

15th September 2009 - 5 minutes read time

When validating a password it is easy enough to make sure that the password is of a certain length, but what happens if you want to make sure that the password has at least one number, or contains a mixture of upper and lowercase letters? I recently had to validate a password like this and so I created a password validation class that allows easy validation of a password string to a set of given parameters, but also allow these parameters to be changed when needed.

Download the Password class here.

The Password class takes a set of parameters, which can be altered at runtime, and uses these to validate a password. To run the password validator with default parameters do the following: