PHP

PHP Browser Detection

Retrieving the current user agent using PHP is done via the use of the $_SESSION super global array. The following line of code will print off your user agent.

echo $_SERVER['HTTP_USER_AGENT'];

For Firefox on Windows this user agent will look like this.

Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.12) Gecko/20080201 Firefox/2.0.0.12

This is all fine, but what about getting more meaningful information, like just the version number. Here is a function that will get the version number (major or minor) from anyone visiting a page with Internet Explorer.

PHP Easter Eggs

There are a couple of hidden features that you can see on just about any PHP driven website. You can get either an image or a list of credits for PHP by appending one of the following strings to the end of the URL.


?=PHPE9568F34-D428-11d2-A769-00AA001ACF42
?=PHPE9568F35-D428-11d2-A769-00AA001ACF42
?=PHPE9568F36-D428-11d2-A769-00AA001ACF42
?=PHPB8B5F2A0-3C92-11d3-A3A9-4C7B08C10000

Here is a quick explanation of each code.

The glob() Function In PHP

The glob() function in PHP uses simple pattern matching to find files and directories in a directory and return those file names as an array. It simplifies all of the PHP directory functions, so instead of opening the directory and then reading every file name one by one, you can just use glob() and so it in one function call. Additionally, glob() works very closely with the file system and so it very quick.

Here is a simple example.

$globOut = glob('*');

The $globOut variable now contains a list of all of the files and directories in the root folder. If you want to find all of the files ending with HTML in a directory then use the following.

The function takes another parameter which is a constant that defines some ways of changing the output. Here is a list of the available constants.

Save Browser Output To A File With PHP Output Buffering Functions

The PHP output buffering functions provide a handy way of intercepting the contents of the buffer before it is sent to the browser. The output is whatever is sent to the browser whenever you print something off. PHP allows you to capture this output in a buffer before it is sent to the browser.

Output buffering is controlled by two mechanisms. The first is through the php.ini directive output_buffering, which is usually set to off. It can be turned on by setting this to either on, or the number of bytes that the buffer can take up. When this byte allocation is full the output is sent to the browser.

A Garbage Collection Mechanism In PHP

Garbage collection is a term for a maintenance function in a class or script that you don't want to run every time the script is run.  The main function of the script is to clean up anything that the script has used previously, but is now not important in the general running of the system and can be removed with no ill effects.  However, it is important that the garbage collection is not run every time the script is run as it may have a detrimental effect on the speed of the system.  To get around this we can use a random number generator to generate a number within a range, and use this to test if the garbage collection function should be run.  Here is the code.

www.php.net

By far the best resource for finding information about PHP and all of the functions available is from the PHP website. Not only can you view the PHP documentation, but you can also download PHP and many of the extensions like the Smarty template system.

Each PHP function and section has its own page with lots of detailed information about usage and instillation, which can be found quite easily on the site by entering the domain name followed by the function name you want to look up. If the function isn't found that the site points you towards a search results page.

PHP5 Filter Functions Part 2

Following on from the previous post about the PHP filter functions there are two more filter functions that require some extra explanation. These functions are filter_var_array() and filter_input_array().

They work in much the same way as filter_var() and filter_input() but they accept an array as the input. This enables you to sanitize or validate many different variables at the same time.

The first step in using these functions is to create an argument array. This is an associative array of data identifiers that allow you to set filter and sanitizer flags for different values. For example, assume that the following array is going to be used.

PHP5 Filter Functions Part 1

The filter functions are part of the PECL library and should come as standard on most PHP 5 installs. If they aren't there then ask your server administrator to install them.

The filter functions where created to avoid developers having to write lots of unmaintainable code in order to check the validity of variables and to sanitize these variables once validated. So rather than using many different functions and regular expressions to tell if a value is a number, a boolean or even a URL, you can just use these filter fucntions.

The main functions that you might be interested in are filter_var() and filter_input(). The filter_var() function is used to validate a single input, the parameters are:

PHP Page Redirection

To redirect to a different page using PHP you can use the header() function with the parameter 'Location: ' and the destination of the redirect.

header('Location: http://www.hashbangcode.com');

However, if any headers are sent before this function call then the script will fail. To get around this you can either ensure that nothing is printed out on the page, or if that is not possible for some reason then you can use a JavaScript redirect. The headers_sent() function will allow you to see if any headers have been sent to the browser yet, if they have then you will need to use the JavaScript redirect like this:

PHP Function To Work Out Average Values In Array

Working out the average of a bunch of values is quite a common task, but rather than looping through the array, adding together values as you go and the using the count() function to find out the average at the end.

function average($array)
{
 $total = 0;
 foreach ($array as $item) {
  $total += $item;
 };
 return $total/count($array);	
}

However, a much simpler way of doing things is just to use the PHP function array_sum(), which adds up all of the values in the array. Because this is done by the PHP engine it should take less time than using a for loop.

function average($array) {
 return array_sum($array) / count($array);
}

Tests show that the second function is only just faster when using short (2 or 3 items) arrays, but the second function is significantly faster when looking at longer (10 or more) arrays.