PHP

FilterChain Element In Phing

The FilterChain element is where the power of Phing really comes into its own. This element will allow you to change the contents of the files of a fileset. This can range from a simple stripping of comments, to replacing values and numerous other filters.

One of the simplest thing that can be done with filterset is to strip all comments from the files in question. Take the following PHP file with two comments.

<?php
/**
 *
 * This is a comment
 *
 *
 **/
echo 'Hello World!';
 
// another comment.

These comments can be stripped out of the file by using the stripphpcomments element. This is added to the copy element in the following way.

Using Patternset With Fileset In Phing

When a project gets complicated then so to can the build.xml file associated with it. You might find it necessary to have multiple different filesets, each of which do something different, but all of which have the same core files that they use. Multiple filesets are useful if you want to create a version of your project with all of the testing files in place so that it can be tested by external developers.

Creating multiple filesets can create problems, one of which is maintenance. Confusion can also occur even if you have a single large fileset where lots of different rules for including and excluding files are defined.

This is where the patternset element comes in. You can create a set of reusable file lists that can be used by more than one fileset, but which also makes maintenance a little easier.

The following snippet shows two patternset elements that show include files and exclude files.

Force Browser To Refresh All Content Using PHP

One issue, especially when creating AJAX applications, is that the browser can cache the contents of the page so that when a similar request is made the same content is presented.

To force the browser to present the content you want it to without caching you can add the following headers to your page.

header("HTTP/1.1 202 Accepted");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, proxy-revalidate, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Cache-Control: max-age=10000000, s-maxage=1000000");
header("Pragma: no-cache");

Remember to put this before you print out any content as you will get some nice looking errors otherwise.

Also, I wouldn't put this on every page on the site as it can create an overhead that just isn't needed.

Introduction To The Phing build.xml File

By default, Phing will look for a file in the current working directory called build.xml when you run it. This document tells Phing what it will be doing during the build. You can change this by using the -f or the -buildfile property of phing and giving the build file as the parameter. The following code makes phing look for a build file called wibble.xml.

phing -f wibble.xml

Assuming that the project is called myProject, then a minimal buildfile would be look like the following.

<!--?xml version="1.0"?-->
<!-- This is a comment -->
 
<project default="main" name="myProject">
  <target name="main">
</target>
</project>

Of course, this doesn't actually do anything at all, so it is utterly useless.

Automated Build With Phing

Phing is a PHP tool that allows you to build projects. Phing stands for PHing Is Not Gnu make, which is kind of a common coder's joke. It is basically a tool that will take a set of files in multiple directories and create either a zip file or a numerous other things that you might want to do with a project.

Phing itself is a PHP program, so all that is needed to run it is a working copy of PHP. Also, because Phing is part of PEAR it is easy to install, and you don't need to install Apache to run it.

Over the next few posts I will be talking about the sort of things you can do with Phing, how to install it, how to create zip files and how to use variables to simplify things. I will add a list here for each of the posts.

Turn Off PHP Parsing In A Directory

Sometimes it is necessary to turn off PHP parsing for a directory. You might want to give away some source code and therefore don't want to parse that code when the user tries to download it. You could just rename the file to have the extension .phps, which is a PHP source file, but this is an alternate method.

To turn off PHP parsing in a directory just create a .htaccess file with the following content.

php_flag engine off

You can expand on this by adding the following:

AddType text/plain .php

This will force all files to be served as plain text files.

If you are going to use this then be sure that you do not put this in any directory that contains your applications. This will cause your files to be downloadable, including any files with usernames and passwords in.

Zend Framework

Zend Framework is a PHP framework, written and maintained by Zend. It is designed to make development of web applications secure, streamlined and reliable.

Zend Framework

As with all frameworks there is a learning curve with Zend. However, as soon as you get over this it gets a lot easier. I found that the best way to understand how to program with the Zend Framework is to find some tutorials. Luckily, there are people out there who have created a few Zend tutorials.

Rob Allen has a nice Zend Framework Tutorial which is available at his site. Rob also has a nice tutorial on Zend Framework authentication.

Finding Out Where Users Have Come From In PHP

It is sometimes a good idea to find out where users can come from. When PHP is run the $_SERVER superglobal is always available and if the user has clicked on a link and landed on your page then the HTTP_REFERER value will be set. You can retrieve and view it on a test page like this.

if ( isset($_SERVER[ HTTP_REFERER ]) ) {
 echo $_SERVER['HTTP_REFERER'];
}

Of course you might want to do something useful with this. For example, you might want to know what link a user clicked on when they broke your application.

Some .htaccess Rules To Improve PHP Portability

PHP is a powerful tool, but if you create any piece of software there are one or two things that you should never rely on.

A good example is using PHP short tags. This is a short hand way of stating that this block are to be parsed as PHP. This is an example of a normal tag.

<?php echo 'Hello World'; >

Here is the same code using short tags.

<? echo 'Hello World'; >

Another alternative, if you just want to print the output of a variable, is to use the following.

<?='Hello World'; >

The short tags setting can be turned on or off in the php.ini file. If you create an application that relies on these short tags then you will find that on some systems the short tags setting is turned off, which means that your software will simply not work.

Converting UK PostCode To Longitude And Latitude With PHP

This common problem has stumped many programmers in the past, so I thought I would add in my little part. Whilst doing research for this I managed to find a site called www.streetmap.co.uk which has a nice little PostCode to geographical reference tool. Using a simple URL parameter I was able to give the site a PostCode and strip the longitude and latitude from the resulting HTML. Here is the function I came up with.