Drupal 7: Login Destination Based On User Role

19th August 2011 - 3 minutes read time

The Login Destination module is a neat little module that allows site admins to customize where the user will be sent to when they login. It provides a few of different ways of customizing, including a PHP snippet box that allows fine grained control.

What I needed to do on a recent project was to change the login destination for a single user role, but redirect everyone else to their user profile page. This required the use of the PHP snippet box. In order to get access to the current user object I had to include it into the scope of the PHP snippet code by using the following.

Getting Started With Xdebug And Netbeans 7 On Windows

16th August 2011 - 16 minutes read time

Note: This article assumes that you have a working Apache/PHP environment running with the latest major version of Netbeans (current 7) installed.

Xdebug is a brilliant PHP extension that provides both debugging and profiling tools. From a basic install it will vastly improve the quality of the error messages but it can also be made to interactively debug scripts using a number of different IDE's. It can also be used as a code profiling tool, but I won't be going into that in this post as that is a little beyond the scope of what I will be talking about.

Installation

If you are using XAMMP to host your Apache/PHP environment then you can skip the following step. Xdebug is actually bundled with XAMMP so you just need to enable it by going into your php.ini file and uncommenting the options you need.

Using The Zend Framework FlashMessenger

14th August 2011 - 6 minutes read time

The FlashMessenger in Zend Framework has a bit of an odd name as it has nothing to do with Adobe Flash at all. It is a controller action helper defined in the class Zend_Controller_Action_Helper_FlashMessenger, which is used to store and retrive messages that are temporarily stored in the user's session. This is useful if you want to provide form validation and redirection at the same time as you can print out messages after the page has been loaded. If you are familiar with Drupal then this class acts in the same kind of way as the drupal_set_messages() function.

Because FlashMessenger is an action helper it can be initialized in a number of different ways, however, using the _helper property (the helper broker) of the controller is probably the easiest way. The FlashMessenger can be retrived through the helper broker in the following ways:

PHPUnit Skeleton Classes

25th July 2011 - 7 minutes read time

If you create classes in PHP then you should be unit testing them as much as you can. Setting up unit testing classes for your code can be time consuming and involve a bunch of copying and pasting. Thankfully, PHPUnit comes with a couple of helper functions that allow the creation of unit testing classes automatically, which can save a bit of copying and pasting.

As an example for this post I will use the following Spider class, which is part of some code I am working on at the moment to create a simple site spider in PHP.

Installing A Realtek Wireless Card On OpenSuSE Linux 11

21st July 2011 - 4 minutes read time

I've been using my Medion Akoya E1222 netbook for about a year now and I still think it's an awesome little machine. Recently, I decided to swap from Ubuntu to another Linux distro and have been experimenting with Fedora, JoliOS, PuppyLinux before finally going for OpenSuSE. The only problem I had was figuring out how to get the wireless card working.

I had a little bit of trouble tracking down this information, so I thought I would put it here both for future reference and for anyone else looking for the same information. This is mainly specific for my netbook and particular distribution, but it might help others with the same sort of problems.

The first thing you need to do is find out if your system can actually see the wireless card. This can be done using the lspci command, which in my version of SuSE needed to be called directly from its location in the sbin directory.

Prevent Enter Key Submitting Forms With JQuery

15th July 2011 - 2 minutes read time

A common practice that some users have is to press the enter key when they are filling in forms on the web. This might be when they are just moving to the next field, but the trouble is that this will submit the form.

To prevent this from happening you can simply stop the form being submitted if the enter key had been pressed. This is done by binding a JQuery event to the input elements of the form and returning false if the key pressed is enter, which has the keyCode value of 13. We also include a call to the JQuery method preventDefault() to stop the event propagating.

Case Insensitive Like Searches In MySQL

2nd July 2011 - 1 minute read time

I needed to create a query that did a case insensitive search using the LIKE command in MySQL and I quickly realised that in order to do this I would need to alter both the parameter and the table data to be the same case. This can be done by using the MySQL UPPER() command on the table data and the strtoupper() PHP function on the input data.

Netscape HTTP Cooke File Parser In PHP

30th June 2011 - 5 minutes read time

I recently needed to create a function that would read and extract cookies from a Netscape HTTP cookie file. This file is generated by PHP when it runs CURL (with the appropriate options enabled) and can be used in subsequent CURL calls. This file can be read to see what cookies where created after CURL has finished running. As an example, this is the sort of file that might be created during a typical CURL call.

Logging Errors In WordPress

21st June 2011 - 3 minutes read time

To debug WordPress many sites will tell you to add the WP_DEBUG setting on its own to your wp-config.php file, but this can be quite harmful as many server configurations will start showing PHP errors and warning messages on your site pages. Most sites will also log any errors to some form or error log, but this isn't always the best place for them to go.

It is possible to add a set of debug options that will allow WordPress to log any errors or warnings to a file. Open up your wp-config.php file and add the following lines of code above the line that says "/* That's all, stop editing! Happy blogging. */". Any changes to this file should always go above this line so that you can keep track of them.