PHP5 Filter Functions Part 1

14th March 2008

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.

Using JavaScript To Run Form Events

13th March 2008

Creating a form as part of an AJAX control is a common practice, but when you include a submit button within the form the default behaviour of the form is to redirect to the destination supplied in action. To run a form using JavaScript you need to include two things.

In order to overwrite the normal operation of a form you will need to replace the action attribute with some JavaScript code that returns false. This is can be done in the form of come in-line JavaScript code (commonly called a bookmarklet) in the place of any URL in the action attribute of the form.

PHP Page Redirection

12th March 2008

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

PHP Function To Work Out Average Values In Array

11th March 2008

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.

Biased Random Value From Array With PHP

10th March 2008

Sometimes you will want to get a random value form an array in a biased random way, that is, you will want certain values to be returned more than others. Here is a function that will generate a single key from an array, with a greater change of a higher value being retrieved.

Discover Auto Increment ID After MySQL Insert With PHP

9th March 2008

Inserting a value into a database with an auto incrementing field is quite common. Once you insert the new row you would expect that you need to do another query to get the newly created ID.

Another option is to use the mysql_insert_id() function to retrieve the ID created by the last insert statement.

PHP Password Generator

8th March 2008

Here is a very simple function that will generate a string of random characters, ideal if you want to create a password for a new user.

Changing Your Wordpress Database Prefix

7th March 2008

A good security tip when installing your Wordpress blog is to change the database table prefix, the idea is that this will hide the tables from any hackers looking to compromise your blog. This can be done in the wp-config.php file and the variable $table_prefix. Changing this value from the default wp_ to, for example, blog_ will change the table wp_posts to blog_posts, making it more difficult for hackers to find it.

Using mod_rewrite On Form Parameters

5th March 2008

Using mod_rewrite on websites is fairly straightforward and can create some lovely looking URL structures. Instead of having a URL that contains lots of odd looking parameters like this:


You can use a .htaccess file to rewrite the URL on the server side in order to shorten this to something like this: