Category: PHP

PHP IP To Location

1 March, 2010 | PHP | No comments

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.

(more...)

Written by Philip Norton.

Using Authentication And file_get_contents()

15 February, 2010 | PHP | 5 comments

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.

Warning: file_get_contents(http://www.example.com/test.php): failed to open stream: HTTP request failed! HTTP/1.1 401 Authorization Required in test.php on line 4

To get around this issue you will need to pass a third parameter to the file_get_contents() function that causes the function to use a context. This context will pass an additional Authorization header to the server and is created through a function called stream_context_create(). Here is all the code you need to use file_get_contents() in an authenticated manner.

$username = 'username'; $password = 'password';   $context = stream_context_create(array(     'http' => array(         'header'  => "Authorization: Basic " . base64_encode("$username:$password")     ) )); $data = file_get_contents($url, false, $context);

The second parameter is for the addition of a flag and is skipped here by using a null value, but false is also applicable. For more information about what flags are available see the file_get_contents() page on the PHP manual.

Written by Philip Norton.

Using Phing To Deploy To FTP

31 January, 2010 | Phing | 2 comments

Although most developers might not like it FTP is quite a common way of deploying the files for a site to the server. To simplify this process Phing comes with a handy FTP transfer action called ftpdeploy.

In order to use the ftpdeploy we first need to install the Net_FTP package. To install this package just enter the following command:

pear install Net_FTP

The first thing we need to create is a build.properties file to store our ftp details

ftp.destination.host=ftp.theftphost.com ftp.destination.port=21 ftp.destination.username=theftpusername ftp.destination.password=theftppassword ftp.destination.dir=public_html ftp.destination.mode=binary

Below is a trivial example of ftpdeploy in action. The build file simply defines a single fileset, which contains a single file in a directory. The ftpdeploy task accepts the parameters of the build.properties file and uses the fileset to determine which files should be uploaded. The only option that might not be obvious is the mode option. This is for telling ftpdeploy which FTP transfer mode to use, these are either ascii or binary, the default value being binary.

<?xml version="1.0"?> <project name="FtpUpload" default="main">     <!-- Include properties file. -->     <property file="build.properties" />       <fileset dir="upload" id="TheFiles">         <include name="phpinfo.php" />     </fileset>       <ftpdeploy          host="${ftp.destination.host}"         port="${ftp.destination.port}"         username="${ftp.destination.username}"         password="${ftp.destination.password}"         dir="${ftp.destination.dir}"         mode="${ftp.destination.mode}">         <fileset refid="TheFiles" />     </ftpdeploy>       <target name="main">         <echo>FTP Upload Finished!</echo>     </target>  </project>

Assuming you have called your build file ftpupload.xml, you can run this Phing build file like this.

phing -f ftpupload.xml

This will produce the following output.

Buildfile: \ftpdeploy.xml  [property] Loading \build.properties   FtpUpload > main:        [echo] FTP Upload Finished!   BUILD FINISHED   Total time: 2.4250 seconds

There is an additional option called clearfirst which can be used to delete all of the files from the remote directory. The default value for this setting is "false". The Phing documentation states that once the directory has been cleared the set files will be uploaded. I have found that although Phing will clear the directory, it will not upload the files.

Have a look at the Phing documentation for more information about the Phing ftpdeploy task.

Written by Philip Norton.

Default Function Parameters In PHP

26 January, 2010 | PHP | 1 comment

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.

function testFunction($a = 1) {     return $a; } (more...)

Written by Philip Norton.

Format A List Of Items In PHP

8 January, 2010 | PHP Strings | No comments

It is usual when writing a list of items to separate each item with a comma, except the last two items, which are separated with the word "and". I recently needed to implement a function that took a string and converted it into a list of this type so I thought I would expand on it and post it here.

The function takes a single parameter, which can either be an array or a comma separated string. If an array is passed to the function then it is converted into a comma separated string and then passed onto the next part in the function. The function then removes any trailing commas, any commas that have nothing in between them and then makes sure that each comma has a single space after it. The final step is to replace the last comma with the word "and". Once the manipulation is complete then the resulting string is returned. If the string (after removing any trailing commas) doesn't contain any commas then it is simply returned.

Here is the function in full, will comments for each step.

/**  * This function will take a string in the format of a single item or  * multiple items in the format 1,2,3,4,5 or an array of items.  * The output will be a readable set of items with the last two items   * separated by " and ".  *  * @param  string|array $numbers The list of items as a string or array.  * @return string                The formatted items.  */ function formatItems($numbers) {     if (is_array($numbers)) {         // If numbers is an array then implode it into a comma separated string.         $numbers = implode(',', $numbers);     }       if (is_string($numbers)) {             // Make sure all commas have a single space character after them.             $numbers = preg_replace("/(\s*?,\s*)/", ", ", $numbers);             // Remove any spare commas             $numbers = preg_replace("/(,\s)+/", ", ", $numbers);                     // The string contains commas, find the last comma in the string.             $lastCommaPos = strrpos($numbers, ',') - strlen($numbers);             // Replace the last ocurrance of a comma with " and "             $numbers = substr($numbers, 0, $lastCommaPos) . str_replace(',', ' and', substr($numbers, $lastCommaPos));         }     }     return $numbers; }

Here are a few examples of this function in action.

echo formatItems('1'); echo formatItems('1,2'); echo formatItems('1,2,3,4,5,6,7'); echo formatItems(range(1,6)); echo formatItems(range('a','g')); echo formatItems('sdfgdf g,sdf, g,dfg,df,g ,df g,df,g ,d fg'); echo formatItems('1.45/76,5/,85/6.,45./6,456'); echo formatItems('sdfah,      ,,, ,, ,,,, ,  ,  ,  568776ythU~O@)_}_:{>9l,65653224,253,4,236,56,98./,978/59'); echo formatItems('4575,8 56,456,36,45656      ,,    , 4, 56, 546, 546, , 6, 456, , ');

This code produces the following output.

1 1 and 2 1, 2, 3, 4, 5, 6 and 7 1, 2, 3, 4, 5 and 6 a, b, c, d, e, f and g sdfgdf g, sdf, g, dfg, df, g, df g, df, g and d fg 1.45/76, 5/, 85/6., 45./6 and 456 sdfah, 568776ythU~O@)_}_:{>9l, 65653224, 253, 4, 236, 56, 98./ and 978/59 4575, 8 56, 456, 36, 45656, 4, 56, 546, 546, 6 and 456

Written by Philip Norton.