PHP Logo

PHP Headers Already Sent Error

14th February 2008 - 4 minutes read time

Try running the following PHP script.

 echo 'browser output';

You will either see normal output or get the following error messages.

This is because when you try to start the session it adds items to the headers outputted by the browser, including the setting up of cookies. To stop this happening you need to ensure that the session_start() function call is put before any output from the browser. This is the case for all header modifying functions including set_cookie() and header().

PHP Logo

Custom Error Handling In PHP Using set_error_handler()

13th February 2008 - 4 minutes read time

The set_error_handler() function can be used in PHP to allow you to catch any run time errors and act accordingly. This function can take two parameters:

  • Error Handler : This a string which is the name of the function that will be called if an error is encountered.
  • Error Types (optional) : This is an optional parameter used to tell PHP on what error codes to act. This is the same error reporting setting.

The function that is defined in the function must have the following footprint as a minimum.

function handler($errno,$errstr)

You can also get a lot more information out by using other parameters.

function handler($errno,$errstr,$errfile,$errline,$errcontext)

Here is some code that will print two errors to screen with as much information as possible.

PHP Logo

Append One Array To Another In PHP

12th February 2008 - 3 minutes read time

Appending arrays in PHP can be done with the array_merge() function. The array will take any number of arrays as arguments and will return a single array. Here is an example using just two arrays.

$array1 = array('item1', 'item2');
$array2 = array('item3', 'item4');
$array3 = array_merge($array1, $array2);

Will print out.

 [0] => item1
 [1] => item2
 [2] => item3
 [3] => item4

You can also create arrays using the array command inside the parameter list.

PHP Logo

PHP Random Quote Generator

11th February 2008 - 2 minutes read time

The following code loads the contents of a text file and randomly displays a line from it. You can use this to display a random quote on a page every time it loads.

$file= "quotes.txt";
$quotes = file($file);
$randomquote = rand(0, count($quotes)-1);
echo $quotes[$randomquote];

It works by loading a file into memory and picking a line from that file at random. Here is a sample file you might want to use.

This is the first quote - Person One
This is the second quote - Person Two

Fill this with your own quotes and you are away.

Highlight Area With mootools

10th February 2008 - 4 minutes read time

Creating a simple highlight effect is quite easy when you use the JavaScript framework mootools.

The first thing to do is grab the mootools library from the site and link it in your web page. You can select different components with mootools, but if you grab the whole thing you can start to play with whatever you want. Put this line of code in the head section of your web page.

<script type="text/javascript" src="mootools.js"></script>

For this example I want the highlight to occur when the page has finished loading. So I use the window.addEvent function to add an action for the 'domready' event to the window object of the page.

PHP Logo

Sinlgeton Design Pattern With PHP5

9th February 2008 - 6 minutes read time

The singleton design pattern is used to centralise an object in an application that is used to store changing variables that can then be accessed by other parts of the program. It allows only the single instantiation of an object, hence the name.

The main use of a singleton is to create an alternative to the use of global variables. Although global variables are useful they can lead to a major problem if you happen to assign two variables with the same name, PHP generates no errors and your program will start to act oddly. This might not be a problem in small programs, but in larger systems it is very easy to have global variable clashes.

The singleton pattern gets around this by using an single object that is accessible to any part of the program that wants it. If two classes are declared with the same name PHP throws an error so if another part of the program tries to use the same class name you will know about it.

JavaScript Logo

Generic User Agent Detection In JavaScript

8th February 2008 - 5 minutes read time

Detecting the user agent in JavaScript can be important due to the way in which different browsers implement JavaScript. Sometimes it is necessary to create logic to do one thing in Firefox and another thing in IE. Luckily, all modern browsers support the navigator.userAgent property so that is a good start.

Because it is possible to mask your user agent in Opera it is necessary to detect this browser first. There are actually two different ways to hide the user agent in Opera. The default user agent is as follows:

Opera/9.23 (Windows NT 5.1; U; en)

It is possible to identify Opera as either Firefox or Internet Explorer. In this case the version of Opera is appended to the end of the user agent string so it is still possible to detect if the user has Opera and act accordingly.

Identify as IE

PHP Logo

PHP Email Validation Function

7th February 2008 - 2 minutes read time

Every time you accept any input from a use you should attempt to validate it. This is to stop users trying to break the site and also corrects silly mistakes that users might introduce to their input.

Before sending off an email to a new user congratulating them on signing up it is best to validate that email address. Here is function that does this.

function validateEmail($email)
  $reg_exp = '/^[A-z0-9][\w.-]*@[A-z0-9][\w\-\.]+\.[A-z0-9]{2,3}$/';
  if (preg_match($reg_exp, $email) == true) {
    return true;
  } else {
    return false;

This can be used in the following way.

MySQL Logo

Finding Duplicate Values In A MySQL Table

6th February 2008 - 3 minutes read time

To find duplicate values you need to use the MySQL COUNT() function and then pick out all of the counts that are greater than one.

SELECT value,COUNT(value) AS Count
FROM test
GROUP BY value
HAVING (COUNT(value) > 1)

Conversely you can also select the rows that only have a single entry.

SELECT value,COUNT(value) AS Count
FROM test
GROUP BY value
HAVING (COUNT(value) = 1)

However, it is very nice to pick out the duplicate entries in a table, but you might still need to do something with them. Here is a query to delete any duplicate rows from a table. It does a simple self join and deletes the row value with the lowest ID.

PHP Logo

PHP Exif/IFD0 Functions

5th February 2008 - 4 minutes read time

The Exif/IFD0 functions in PHP work with images to pull out meta data associated with them. Most image applications and digital cameras will produce an image with a certain amount of meta data present. This is obvious stuff like file size and creation time stamps, but you can also get stuff like copy right notices, camera name, date picture taken and even things like location if the camera was linked to a GPS system. This meta data can be used to sort or categorise images.


On Linux systems you must configure exif support with the command --enable-exif when calling the configure script.

On Windows all you have to do is uncomment the lines in your php.ini file for the DLL's php_exif.dll and php_mbstring.dll. However, you must ensure that the php_mbstring.dll DLL is loaded before the php_exif.dll DLL. So you will need to edit your php.ini file so that php_mbstring.dll is located above php_exif.dll.