Redirecting all mail from Sendmail on PHP

Making websites, eh? Trying to write something for use on your testing server without bombarding your database victims with development spam? You need.. slurp mail! (ahem)

Here’s how to write your own sendmail client that’ll redirect all emails to the address of your choice. Create /usr/sbin/slurpmail and stick this in it!

formail -R cc X-original-cc \
-R to X-original-to \
-R bcc X-original-bcc \
-f -A"To:" \
| /usr/sbin/sendmail -t -i

Chmod it to make it -rwxr-xr-x. Then in php.ini find the sendmail path line that reads /usr/sbin/sendmail -t -i and change it to slurpmail!

Rock and/or Roll! We are now getting all the mails that would have went to god knows who! yay! Mind and restart Apache now first!


Sorting Multidimensional Arrays using PHP

noticed that asort() doesn’t cut the mustard? You need usort()!

take this array of arrays for example:

$people = array();
$person = array(
    'name' => 'Zak',
   'age' => 24,
   'born' => 'Timbuktu'
$person2 = array(
    'name' => 'Dave',
   'age' => 30,
   'born' => 'Australia'
$person3 = array(
    'name' => 'Fred',
   'age' => 18,
   'born' => 'California'
$people[] = $person;
$people[] = $person2;
$people[] = $person3;

Now we can create your own custom sort fields. Here I’ll create an age one. The return values are -1 for less than, 0 for equal, and 1 for greater than (first value versus second):

// Update - PHP 7 way with spaceship operator
function sortAge($a, $b)
    return $a['age'] <=> $b['age'];

function sortAge($a,$b)
    if($a['age']> $b['age']){
        return 1;
    } elseif ($a['age'] == $b['age']) {
        return 0;
    } elseif($a['age']< $b['age']) {
        return -1;

And here’s one that sorts alphabetically:

function sortName($a,$b)
     return strcmp($a['name'], $b['name']);

Finally, to use either of your sorts, we call usort.




Sorted! 😉

Custom jQuery validator functions

The jQuery validator is really cool, and you can create your own validators. Here’s one that counts the words in a text area, and validates :

$('.word_count').each(function() {
 var input = '#' +;
 var count = input + '_count';
 word_count(input, count);
 $(this).keyup(function() { word_count(input, count) });
function word_count(field, count) {
var number = 0;
 var matches = $(field).val().match(/\b/g);
 if(matches) {
 number = matches.length/2;
 $(count).text( number);
jQuery.validator.addMethod("twentywords", function(value, element, param) { 
 if($(element+'_count').html() > 19){return true;}
 else{return false;}
 }, jQuery.validator.messages.url);

And in the rules section:

rules: {
f_review: {
 twentywords: true
 messages: {
f_review: "Please leave a review of the agent at least 20 words"

I’ve hard coded the name input into the validator, as I’m in work and it needs done quickly, but I’m sure this could be tweaked. Finally there is a hidden element that the word cound gets stored in and the input textarea has a class of word_count:

<div id="f_review_count"style="display:none"></div>
 <textarea name="f_review" id="f_review" rows="8" cols="40" class="text-long word_count"></textarea>

The word count stuff can be used on its own without the validator too. 🙂

Zend Framework 2.0 released today!

I had a Zend Framework related question, so I went onto IRC to get help. IRC is the place to be! You” probably know Akrabat (Rob Allen). Well he was there too. And the chat was ‘congrats on the release!’

Lo and behold when I checked, the entire site had changed! ZF2 is here! And I couldn’t have timed it more perfectly. My PHP is now at 5.4, and  I have Doctrine 2 up and running!

I suggest we waste absolutely no time, and download it then run through the tutorial! We have lots to learn! The new PHP namespaces, and dependency injection and a whole bunch more! Back soon with some juisy gossip lol!

Upgrade to PHP 5.4 on Mac OS X Lion

The PHP that ships with Mac is getting rusty. Time to get PHP 5.4!

I tried several methods, and several fail failed. This one ( didn’t.

sudo port selfupdate
sudo port -u upgrade outdated

sudo port install php54 php54-apache2handler
cd /opt/local/apache2/modules
sudo /opt/local/apache2/bin/apxs -a -e -n php5
sudo port install php54-curl php54-ftp php54-iconv php54-mbstring php54-mcrypt php54-mysql php54-openssl php54-soap php54-sqlite php54-xsl php54-zip php54-xdebug php54-mongo

sudo port install php53 php53-apache2handler
sudo port install php53-curl php53-ftp php53-iconv php53-mbstring php53-mcrypt php53-mysql php53-openssl php53-soap php53-sqlite php53-xsl php53-zip php53-xdebug php53-mongo

cd #
curl -o go-pear.phar
sudo php go-pear.phar

And then we can select which php the system uses by saying either of these:

sudo port select php php54
sudo port select php php53

When installing Pear:
Option 1. Installation base ($prefix) : /opt/local/lib/php54
4. Binaries directory : /opt/local/bin
Then get PHPUnit

pear info pear && rm go-pear.phar
sudo pear config-set auto_discover 1; sudo pear install

Lastly we nano /etc/apache2/httpd.conf and ctrl-w to look where php is. Comment the 5.3 one, and add the line:

#LoadModule php5_module libexec/apache2/
LoadModule php5_module /opt/local/apache2/modules/

So we can switch between the two by moving the comment. Finally restart apache!

sudo apachectl restart

phpinfo() and php -v both showing 5.4! Fantastico! Remember to set your default timezone now!

Password Protect folders in Apache with htpasswd

Want a completely insecure password protected folder using htpasswd?  Great!
A pointless exercise unless it’s in a setup at your work and you have no say in the matter, htpasswd’s are sent in unencrypted plain text, so anyone hanging around packet sniffing will pick up the password easily enough! Anyway, thats besides the point, how is it done?

First up we put this in the .htaccess for the directory we wish to protect (ha):

<Directory "/home/user/username/www/folder/to/protect">
AuthType Basic
AuthName "My Private Directory"
AuthUserFile "/path/to/htpasswd"
Require valid-user

Then we generate the htpasswd file like this:

 htpasswd -c /path/to/htpasswd username

Hooray! The illusion of ‘security’! At least it keeps non geeks out 😉

Default editor for Git

Just created a new site, and went to perform my initial commit to my Git repository, and yuk! Vi, or Vim, appeared! :-s Don’t get me wrong, Vi/Vim is powerful and can do all sorts, but I like Nano, or Pico! Last time this happened I type in a command to change the editor, but what I forgot to add was to make it the GLOBAL  default. A slight tweak, and the command looks like this. No more Vi! Awesome 🙂

git config --global core.editor "nano"