PHP

Posts about the server side scripting language PHP

A Useful Error Controller Class For Zend Framework Applications

One useful function of any application is to report on any errors that occurred. Zend Framework comes with a nice error controller system that you can activate by creating an ErrorController class.

The following is an ErrorController class that I use. It detects what sort of error occurred and displays a message to the user. It will also email a detailed report of the error to the server admins.

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.

Creating A Personalised Events List With SimplePie

One nice feature for any blog or site (especially news sites) is to have a little list of forthcoming events that would interest your readers. One way of collating this information is to scour the web and update the list manually. However, this is time consuming and tedious, especially as there is an easier way to do it.

Have a look at the Yahoo site called Upcoming. On this site you can search for events on lots of different subjects in lots of different locations all over the world. Also, if you have an event you can put it up on the site.

PHPNW 08 Conference

The PHPNW conference was this weekend, and 8 members of staff from my company showed up to support the PHP community and learn a couple of things along the way. The conference consisted of 174 attendees, with 12 sessions hosted by 16 speakers.

The talks ranged from simple usability and design to getting started in Drupal and they were all well researched, planned and executed. I was especially impressed with the 3D timeline used in the Twittex talk.

The two talks that stood out for me where the Introduction to the Zend Framework and HTML to Drupal in 30 minutes. I will definitely be looking into these two products, and perhaps even buying a book on the Zend Framework.

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.

Randomising The Middle Of Words In PHP

I was sent an email the other day that contained some text were the start and end letter of each word were left alone, but the middle of each word was randomized. The weird part was that the text was still readable, which is due to the way in which the brain processes words.

I wondered if I could replicate this using a PHP script. All I would need to do is split apart the sentence into the component words and loop through those words, randomizing the middle of them. Clearly, it is not possible to mix up the order of letters in a word less than four characters long so a check would be needed for this. This is what I cam up with:

Generate Password Function In PHP

I have talked about generating random passwords before. Although that function generated some nice passwords, they perhaps aren't as unique as they ought to be.

This function, take from Webtoolkit creates passwords of different length with varying levels of complexity.