Archive for the ‘Code Snippets’ Category

Fixing a WP Plugin: Per-Post CSS and JS

Sunday, February 17th, 2008

A few weeks ago, I installed the “Per Post CSS and JS” plugin for Wordpress. This allows you to add a custom field to your post and include extra .css or .js files to link to that individual page.

I only realized today – while testing something else – that the plugin had been firing off warning messages because of an oversight in the code. Warning messages annoy me, so I decided to fix it up.
(more…)

Digg API: Grabbing a Random Digg Story with PHP

Saturday, February 16th, 2008

I read about the Digg API the other day, and I thought it was about time I played around with it.

There is a ton of cool stuff you can do with this. Basically, you send a request to the Digg server and it sends back whatever kind of information you want – category names, recent diggs, recent stories, archived stories, etc. You can read through the API to see everything you can do.

For now, we’ll focus on one nifty little trick – grabbing a random story from a given topic.
(more…)

New Script: PHP Length Conversion Class

Sunday, February 10th, 2008

I was looking for a project to tackle today, and I decided to make a script that would convert lengths from one unit or system to another.

It doesn’t sound all that complicated – and it’s not. I had a working script in a few minutes, but I didn’t like it. It had too many functions to do conversions for individual units. I wanted this script to be pretty well abstracted so that I could easily add new unit types – and so that the source code for the class would be very short.
(more…)

How to Create Multi-Page Forms in PHP, Revisited

Friday, February 8th, 2008

A couple weeks ago, I wrote a short article about how to create a multi-page form.

The simple solution I suggested involved a foreach loop that cycled through the $_POST array and sent every value along in a hidden input element. After a bit of reflection (and some useful comments), I realized there’s a teeny tiny security hole in that approach – so I’ve slightly modified it to close the loophole.
(more…)

How to Create a Random Date in PHP

Thursday, February 7th, 2008

While browsing some forums the other day, I came across this question – “How do I find a random date in PHP?”

This is a pretty simple operation, if we make use of a few built-in PHP functions – time(), strtotime(), and date(). In this article, we’ll see how these three can work together to find a random date within a given range.
(more…)

How to Send E-Mail in PHP: A “Share This” Form

Wednesday, February 6th, 2008

This question comes up a lot in forums – how do you send e-mail in PHP?

There are plenty of reasons you’d want to do this. Perhaps you want to send a newsletter to your users. Or you want people to be able to leave feedback through e-mail. Or you just want your users to be able to e-mail the page (or a link to it) to a friend.

How do we do this? The mail() function.
(more…)

How to Create a Random Password for Users

Friday, February 1st, 2008

If you’re working on a user-management system, you may find it useful to be able to create a random password for users.

Some systems generate these initially and have the user log in to set a permanent password. You might also have a “Reset” button, where the script generates a random password and e-mails it to the user.

This quick tutorial will show you how to create an 8 character random password containing a mix of letters and numbers. Or, if you’re impatient, jump straight to the function’s source code
(more…)

How to Use PHP To Dynamically Resize an Image

Wednesday, January 30th, 2008

Screenshot of a Dzone PageOne of the most annoying things about working with images is getting them into the right size. Screenshots start out huge – and you need to resize them to an appropriate size for your website.

Some blogs automatically resize the pictures for you into a thumbnail – like the image to the right. What if you want the image a different size, though? You could allow the browser to resize it for you, or – better yet – write your own php script to resize the image dynamically.
(more…)

How to Pass All Elements of a Form to the End in a Multi-Page Form

Sunday, January 27th, 2008

Sometimes forms get long, unsightly, and intimidating. It’d be great if you could cut them up into three or four parts – with a few fields on each page.

In order to do that, you need to figure out some way of storing or passing along the information from the first pages. I’ve heard a bunch of crazy ideas – from storing the information in session variables to writing each part to the database.

The easiest thing to do is attach a very brief php function to the bottom of each page. It’ll do all the work for you and continue to post every item the user has entered. (more…)