Archive for the ‘CakePHP’ Category

July 2, 2010 Off

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: , , , , ,


January 13, 2008 11

Session-Based Flash Messages Look Better (CakePHP)

CakePHP

The CakePHP blog tutorial uses a rather archaic method of displaying user messages (like, “Your post has been saved.“) that needlessly breaks up the flow of your application. Rather than Cake’s typical method of displaying flash messages on a separate page (ugly), learn how to seamlessly display the same messages within your application and make your users clap with joy.

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 20, 2007 3

Custom CakePHP Errors – A Complete Run Down

CakePHP

Taking advantage of custom error documents usually requires tapping on an .htaccess file, as discussed in my previous custom error article. However CakePHP makes for easy custom error handling thanks to a pre-defined Cake system.

I’ll walk you through creating custom error pages, both simple and complex.

Read the rest of this entry »


November 8, 2007 16

Secure CakePHP via Sessions & Magic (Login / Logout)

CakePHP

Making sure a CakePHP application is secure is a total snap thanks to “sessions” – the rock animal of websites. Put on your All-Stars and boot up your MacBook – I’m about to go Discovery Channel on your ass.

Read the rest of this entry »


November 6, 2007 14

Multiple Image Uploads Into Single MySQL Table (CakePHP)

CakePHP

The impossible has been done – uploading multiple images in CakePHP. Find out how easy it is, and why it was never really impossible to begin with.

Read the rest of this entry »