PHP Logo

PHP Headers Already Sent Error

14th February 2008 - 3 minutes read time

Try running the following PHP script.

  1. <?php
  2. echo 'browser output';
  3. session_start();
  4. ?>

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

PHP Logo

Custom Error Handling In PHP Using set_error_handler()

13th February 2008 - 3 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.

PHP Logo

Append One Array To Another In PHP

12th February 2008 - 2 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.

PHP Logo

PHP Random Quote Generator

11th February 2008 - 1 minute 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.

Highlight Area With mootools

10th February 2008 - 3 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 - 4 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 - 4 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.

MySQL Logo

Finding Duplicate Values In A MySQL Table

6th February 2008 - 2 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.

PHP Logo

PHP Exif/IFD0 Functions

5th February 2008 - 3 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.

Installation

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.