Articles

Using Netbeans PHP Code Templates

Netbeans is a great IDE and with every version lots more features are introduced that make it even better. One thing that I like to use is the code templates, which have been available from version 6.5. Code templates allows you to type a simple command and get a section of code. What commands you can use depend on what version of Netbeans you are using and which programming language you focused on. As a PHP developer I usually download the PHP version, which comes with a set of PHP code templates. To try one out go into a PHP file in Netbeans and enter if followed by a tab. Netbeans will automatically change this into the following:

Using list() With explode() In PHP

A simple way to convert a string into a set of variables is through the use of the explode() and list() functions. list() is a language construct (not really a function) that will convert an array into a list of variables. For example, to convert a simple array into a set of variables do the following:

list($variable1, $variable2) = array(1, 2);

In this example $variable1 now contains the value 1 and $variable2 contains the value 2. This can be adapted to use the explode() function to take a string and convert it into a set of variables. An example of this in use might be when dealing with addresses, simply explode the string using the comma and you have a set of variables.

$address = '123 Fake Street, Town, City, PO3T C0D3';
list($street, $town, $city, $postcode) = explode(',', $address);

You can now print out parts of the address like this:

JavaScript Text Length Tool

I quite often find myself needing to know how long a string is, especially when testing form validation or when trying to write a page description. I therefore like to have this little tool to help me by simply counting the number of characters in a given string.

Use the JavaScript text length tool here.

The tool uses a single form with a textarea and a text box. The textarea has onkeyup and onkeydown events which causes it to call a function that counts the number of characters and enters this into the text box.

Google Last Cached Date Finder In PHP

When Google looks at a page it takes a snapshot of that page and uses this to match against the query a user entered. To view these cached pages run a Google search and look at the Cached link next to the green URL text of the result. When you view the cached page Google will also give you a date that the page was last cached on. This can be used as a metric of your sites importance as the more often the site is cached, the more favourable Google views your page.

Taking a reading of this metric can therefore be useful, which is why I set about to create a class to retrieve this result.

Download the Google Cache class here.

JavaScript Confirm Action Dialog

If you want to create a link that performs an action that can't be rolled back then you might want to stop the user from clicking that link unless they really want to. The best way to do this is to intercept the link with a confirm() command.

The first way to do this (and especially useful if you want to ad other functionality) is to use the following function.

function confirmDelete(delUrl) {
 if (confirm("Are you sure you want to delete")) {
  document.location = delUrl;
 }
}

For each link that you want to use this function on just replace the href with a simple bookmarklet like the following, pass the URL you want to use within this function call.

Setting Locale In Zend Framework

Every application has a locale, even if that is just the locale of the author. Through the use of locals you can make your application aware of what sort of language, currency and even timezone that the user would like to see. In Zend Framework this is accoumplished via Zend_Locale.

There are many things to do with locale once, but first you need to determine where the user is based. To find this out you simply create a new instance of the Zend_Locale object. The following code will create the Zend_Locale object and print out the language and region of the user.

$locale = new Zend_Locale();
$language = $locale->getLanguage();
$region = $locale->getRegion();
echo $language . ' ' . $region;

What you see here depends on where you are and what language you are running on your machine. For example, a person living in Germany, who speaks German would see the following output.

Backup MySQL Database PHP Script

There are quite a few scripts available on the Internet that allow you to dump data from a database into a format that can be used to replicate that database structure elsewhere. The following function is my take on this commonly occurring script.

Wordpress 2.8.3 Admin Password Exploit And The Fix

A small exploit has been found within the latest (currently 2.8.3) version of Wordpress that can cause any attacker to reset the admin password. The attacker won't be able to see what the password is, and the new password is emailed to you anyway, but it might cause some users to be locked out of their blogs if they can't get access to the email or their email is down. However, it is a real pain so I am writing this blog to allow other bloggers to fix their Wordpress installs.

The Problem

Wordpress allows you to reset the admin password, which some users might want to do. The normal course of events is that the admin user selects that they want to reset their password and Wordpress will email the user a link containing a key. Following this link will reset the admin password. The issue is that it is possible to recreate this link without first sending out the email, which will reset the password. Here is the URL that can be used to reset the password.

Create A Page Of Posts In Wordpress

A common practice when setting up a Wordpress blog might be to create a page and display posts in a certain category on that page. This causes some pages to act very much like category pages.

The first step in creating a page of posts is to create a page template that can be used by the pages. Create a file in your template directory called pageofposts.php and put the following comment in it.

<?php
/**
  * Template Name: PageOfPosts
  */

This will cause it to be displayed in the Template drop down on your Wordpress page admin. This isn't going to do a lot so lets add some other functionality. Add the following lines to include everything you need to create a blank Wordpress page.

Getting Started With Zend Tool

The latest versions of Zend Framework come with a handy little tool that gets you up and running with a basic Zend Framework install within a few moments. When you have downloaded Zend Framework you will notice that in minimal and full there is a directory called bin. This seems to be intended to contain lots of different tools, but at the moment it contains three files that are part of Zend Tool.

The important file in this directory is zf.php which is the actual core of the tool. The other two files are used depending on what operating system you are running. If you are running Windows then it is zf.bat that will be used, otherwise it is zf.sh that is important.