Archive for the ‘PHP’ Category

July 2, 2010 0

First Thing’s First: Validate

CakePHP Javascript PHP

Have a sweet function written that requires valid variables? Make sure you’re validating those variables at the top of your function. Validating variables before (as apposed to while) they are used is key in making sure that important logic isn’t executed using crappy data.

Here’s an example of validating first, then executing:

	/* The good... */
	public function insertNewInvoiceItem($item, $quantity)
	{
		if(!$item instanceof Shop_Item) {
			throw new InvalidArgumentException ('Expecting an item to be an instance of Shop_Item.');
		}

		if(!is_numeric($quantity)) {
			throw new InvalidArgumentException('Quantity is not numeric.');
		}

		// We're good to go. Continue with the function...
	}

The method above is also much cleaner than if... else... statements and limits the extra (perhaps useless) work required by your code:

	/* The bad... */
	public function insertNewInvoiceItem($item, $quantity)
	{
		if($item instanceof Shop_Item) {
			// Do some $item stuff...

			if(is_numeric($quantity)) {
				// Do some $quantity calculations
			} else {
				// Uh oh, $quantity was bad but we've
				// run some Shop_Item logic already.
			}
		}
		else
		{
			// $item is not part of Shop_Item
		}
	}

I used a simple example to demonstrate my point but when this technique is used on more complex logic, it’s benefits are much more obvious.

Happy coding!


April 21, 2010 2

Three Bitchin’ PHP Classes – Cache, Last.fm API, Time

CakePHP PHP

There’s nothing like the feeling that comes with typing a few lines in a PHP application and letting some included classes take care of the dirty work; you know, the filthy stuff like caching, calling the Last.fm API, purifying timestamps, and handling file uploads. (Dirty, right?)

All of the following PHP classes are “works in progress”, but hopefully each is mature enough to find its way into your code. Continue reading to view, download, and use each class.

Read the rest of this entry »

Tags: , , , , ,


December 11, 2007 3

CakePHP 1.1 File Upload Plays Rough With PHP 4

CakePHP PHP

Uploading files via CakePHP has been covered a few times on Labs. Based on the feedback, it’s safe to assume bridging the gap between file uploads and PHP can be tricky. Usually Cake is ready to lend a helpful hand. Not this time.

Read the rest of this entry »


November 27, 2007 6

A Simple/Secure Email Class For PHPMailer

CakePHP PHP

If you’re not careful, sending email via PHP is about as safe as cookies at Fat Camp. BKWLD was recently tagged by a spammer who noticed one such insecurity on one of our websites and ended up using our server to send hundreds of emails. It wasn’t until MediaTemple informed us of this breach that we realized what had happened.

The method most spammers use is called a “mail injection”, in that the spammer manipulats the PHP mail() function via a custom form post, etc. I won’t go into details, as a quick search on Google came up with over 225,000 pages describing this technique – here are a few.

Getting around these hacks requires detailed validation of your data.Thankfully I’ve done everything for you using the best PHP email class around, PHPMailer. My class is called SendMail and it’s an extension of the PHPMailer class. The result is a powerful script capable of sending email via SMTP or POP3, all without handing over your server to a toothless spammer.

Read the rest of this entry »


November 19, 2007 5

Vision, Simple PHP/JS/CSS Photo Viewer

CSS Labs PHP

The simplest image viewer known to man has been created. Visión uses a light touch of PHP, Javascript and CSS to create a straightforward, stylish image viewer. It can be incorporated into any website in under three minutes and takes up less than 20kb of space. Stop reading this and download Visión.

Read the rest of this entry »