DerekAllard.com

Quick Link: Building a Bulletproof Contact Form with PHP

Matthew Pennel (of The Watchmaker Project) just wrote a short and very clear article on Digital-Web Magazine on handling contact forms with PHP entitled “Building a Bulletproof Contact Form with PHP”.  If you are new to the topic, or are just looking to get that contact form set up and working, then for sure, give it a read.

Great work Matthew!

Few notable links recently

OK, these are the laziest of possible posts, but there have been a few things I’ve found in the last few days that I wanted to bring up.

Case Sensitive in MySQL searches

Interesting problem I encountered this morning.  I needed to make a case sensitive search through MySQL.  So consider this search.

SELECT FROM people WHERE field LIKE '%Derek%' 

This will match

but I only was interested in exact matches.  Ah right!  MySQL cases are case insensitive by default.  Hmm… how to get around this one?

So begin the search… which brought me to a MySQL docs page called Case Sensitivity in Searches.  Buried in that page is this little gem.

If you want to make this search case sensitive, make sure that one of the operands has a case sensitive or binary collation.

But then all their examples suggest changing the collation with the “COLLATE” argument. Then something from years gone by clicked in my head, and I remembered forcing the result to binary, which led me to write this query.

SELECT FROM people WHERE BINARY field LIKE '%Derek%' 

Works a charm, so I wanted to document it (for my benefit), and share it on blog (for anyone reading this).

And now that I’ve got my result, this pretty much ends my investigation into this, so if anyone cares to shed more light, or share a “proper” way of doing this, please just comment below!

Error Handling in CodeIgniter

error messageThis entry gets a little nerdy, and into the underlying depths and behaviour of CodeIgniter.  If you aren’t interested in the explanations, and just want to know what a good best-practice might be for application development with CodeIgniter, then just skip to the bottom paragraph now.  If however you are a glutton for convoluted writing and technical details, then read on!

Recently, I dealt with the idea of PHP error messages and logging in CodeIgniter.  Since CodeIgniter subverts PHP errors for its own purposes, I thought I’d write a bit about how it works.

Ordering Database results by “random” in CodeIgniter

A recent CodeIgniter bug report had got me looking into the depths of the database results functions of the framework.  Essentially, the orderby() function of CI’s Active Record says that you can sort by ASC (ascending), DESC (descending) or RAND().  Imagine this:

$query "SELECT * FROM table ORDER BY RAND()"

Anyone familiar with PHP probably looks and that and thinks of the native PHP rand() function.  This is pretty neat actually, and I’d never really thought about randomly ordering things.  But on further investigation, it became clear that this code wasn’t as nice as it initially seemed.  Firstly, its non-standard SQL, reducing its portability greatly.  While MySQL uses RAND(), other databases have their own way of doing things:

So then, how would one randomly order their database results?  Read on for my solution…

Using PHP for random, non-repeating numbers

Need a series of non-repeating but random numbers? For example, random images from a gallery or portfolio screenshots? Let me save you some time:

$numbers range($min$max);
shuffle($numbers);

$random_1 $numbers[0]// first random number
$random_2 $numbers[1]// next random, non repeating
$random_3 $numbers[2]// next random, non repeating 

Normally I wouldn’t blog about something like that, except that I just spent 20 minutes using rand() to generate a random number, then saved it into an array, then ran an in_array() to look for it, and start the process over.

It wasn’t until I was about 15 minutes into it that I thought to myself… “What on Earth am I doing?”.  Then stubbornness kicked in and I tried to finish my monstrosity, then I finally re-evaluated my strategy.

Funny, because I’ve never used shuffle() before for anything, so my brain could only think of commonly used array functions like sort() and asort() (and of course, your favourite and mine… array_intersect_uassoc() - which I freely admit to only just now looking up to discover it exists).

Why are we sometimes driven to code the most complex solution possible, and worse, even when we know it is utterly involved and inelegant, we still drive to make it work, “just to see it working”?

Not so useless! image_to_text() as a CAPTCHA

This entry is guest-posted by CodeIgniter programmer Alexander "Iksander" Springmeyer, who contacted me about a unique use of my "Most Useless CodeIgniter Helper Ever". I've asked him to write up a few words, that I present here for all. My sincerest thanks to go Alexander for looking for new ways to implement old ideas, and for teaching an the old image2text() dog a new trick!
-Derek

Some time ago I happened across Derek's blog post on the 'useless' image2text helper he had created. Despite all the "it is useless" comments in the entry, I felt it had powerful possibilities offered by its application in my Sentinel re-write and upgrade of the Freak Auth light authentication system (by Daniel "danfreak" Vecchiato). [editors note: if you want to, you can read a very long and detailed account of its evolution.]

I was inspired to use it for a CATPCHA. Here's how did I do it?

Finding the Second Highest Value with SQL

For a project I'm currently working on, subscribed users get up to the minute most current information (as outlined by a date field in MySQL).  Unsubscribed users get the information still, only it is a day behind. 

Due to the way the information is going into the system, there could be multiple entries for one date, and then no entries for a few days, then new, etc.  Basically, the information cannot be predicted by the application.  Retrieving the most recent date is trivially easy with a MAX command, but there is no "almostMAX" command, so I found myself staring at the screen wondering what the most efficient way to approach this was.  Here's my solution:

SELECT * FROM table GROUP BY dateIssued ORDER BY dateIssued DESC LIMIT 1,1

Fortunately for me, LIMIT accepts offsets, and I can specific to grab only the second entry.  If you can think of a more elegant way to do this (or heck, if you just want to comment), please leave a note below.

Most Useless Code Igniter Helper Ever?

Have I written the most useless helper Code Igniter will ever see? I think so.

It takes an arbitrary image and arbitrary text and builds an image out of them.

Code Igniter logo becomes Code Igniter logo as text(full size)

Which really doesn't do it justice until you see it full size, or run the demo. Here's a zoom.
Code Igniter logo as text zoomed in

I should note that the helper handles colours just fine. Here's the rendered robot (2.7MB... be kind)

A red robotRed robot Rendered as text

I proudly present the most useless helper ever written! The (useless) helper is available for download, and I've appropriately named it "useless_fun_helper". Run it just like you'd run any other helper.

$this->load->helper('useless_fun');
$txt = 'Code Igniter is an Open Source Web Application Framework that makes writing kick-ass PHP programs simple as apple pie';
$img = 'http://yourserver/ci_logo.jpg';
echo image_to_text($img, $txt); // typically you'd pass that into a view... I know... no hate mail ok

So this is my formal entry into the most useless helper of all time. Any runner-ups?

Top 10 SQL Performance Tips

Hmm... lools like my love affair with SQL related posts continues.

MySQL has posted an excellent resource posted called Top 10 SQL Performance Tips. Although, it is kind of a misleading name, since its a wiki, and everybody is able to post. Currently it's up to 84 tips, most of which look really great. Here is a small sampling...