Tag Archives: PHP

The PHP Anthology: 101 Essential Tips, Tricks & Hacks [Book Review]

About a year ago, I purchased a copy of Sitepoint‘s book The CSS Anthology, which I enjoyed. While browsing through the “Computers” shelf in the local library’s New Books room, I came across a copy of The PHP Anthology.

This isn’t your garden variety teach yourself PHP-type book. This is intended for people who already know their way around PHP moderately well, and have some experience with it. Instead of teaching you the language, it’s a respository of “101 Essential Tips, Tricks, & Hacks.”

It’s a good book, informative and easy to read. It gives examples on how to deal with dates, secure your scripts, create thumbnails effectively, built an HTTP authentication system, and much more. While you can read the book all the way through (I did), it works well as a reference, and a way of learning some new techniques.

Like a lot of computer books, it’s a little pricey, with a list price of about $40 (though Amazon has new copies for around $25).

Yahoo BOSS API

Yahoo BOSSYahoo recently released a new search API. Known as Yahoo BOSS, for “Build your Own Searcg Service,” the API allows you to query search results from their servers, format them however you want, mash the data up with other services, and even re-order results. You get “Unlimited*” queries (they just reserve “the right to limit unintended usage, such as automated querying by bots”) and they don’t even require attribution.

Even though I’m what you could possibly call a “Google/Apple fanboy” (though you would be advised to not say such things…), and I’ve long dismissed Yahoo as boring, geared towards web newbies, among other things, I have to admit, this is a great API. Google never gave us anything like this (despite their seemingly unlimited resources) and they discontinued their fairly limited search API. (As a side note, I also admit that Yahoo owns some great web services, such as Flickr and Del.icio.us.)

I’ve already got to work playing with the API, creating a sort of search mashup. I figured I’d share a little bit of code, and show you how to create a basic SERP. Be warned, the following requires PHP5 and some cURL black magic. (If you have no idea what I just said, read a book, and come back later.)

Continue reading →

WordPress Email Obfuscation

Email obfuscation is a useful technique to use to stop spammers from finding your email address (as opposed to the more common “munging” where you write “me [at] blah [dot] com”). An obfuscated email address is unreadable to humans and most robots in the source code, but is rendered correctly in a browser, and will function correctly in a mailto link. I’ve found it works well…but it’s a pain in the arse to do by hand. There are free online tools that will obfuscate an email address for you, but what if you want an automatic solution.

I didn’t know this until WordPress Garage pointed it out, but it seems that WordPress has a built-in obfuscation function. It’s called antispambot().

The function antispambot() above parses the e-mail address passed by get_the_author_email() (this is the same as the_author_email(), except it returns rather than displays the author’s e-mail address). Use of the echo command displays the output of antispambot(). An interesting feature is it encodes only portions of an address, and does so randomly so the letters encoded are different each time the page loads, adding a little more firepower to the spam protection arsenal.

The wiki page on WordPress.org has an example on how to use the function.

What Plugin Coders Must Know About WordPress 2.6

Planet Ozh has a new post up by the name of What Plugin Coders Must Know About WordPress 2.6. Ozh put out a similar post back when 2.5 was in development, and I found it very useful. This time around, there’s even more to worry about, er, be aware of.

It seems that users will be able to move their /wp-content directory wherever they want when the new release is out. That means you can’t rely on it being located at /wp-content anymore. A user could have it at /stuff/w-content or ../lib/wp-content, or anywhere, really.

Luckily, Ozh explains how to programmatically find the /wp-content URL or path.

No More CSS Hacks: PHP Browser Detection

You can’t make your page render correctly in every browser. It just isn’t possible. Between several versions of the bug-ridden Internet Explorer (why can’t Microsoft just switch to Gecko or WebKit?) and a sprinkling of older browsers, there are too many bases to cover. Internet Explorers 6 and prior are notorious for their bad CSS implementations (IE7 is better, but it still has a ways to go), and if you tweak your CSS to look right in them, chances are you’ll create another problem somewhere else. When does the endless cycle of fixing things end? When you give up and say “it’s just not going to work in all browsers.” Now, you don’t want to give up too soon, since there are still a lot of people on IE6, but you have to know when to call it quits.

And don’t forget about mobile browsers. Sure, their improving, like Apple’s mobile Safari browser on the iPhone, but it’s still a lot easier to use a mobile-specific version of a site than zooming and scrolling around on the tiny screen.

As usual, PHP has a solution. That solution comes in the form of the global variable $_SERVER['HTTP_USER_AGENT']. It holds a string that contains a bit of information about the browser and platform a user us using. By searching through the string, you can figure out what browser your users are browsing with, and write-in the right code depending on the browser. You can do this in as simple, or as complicated, of a way as you want.

Continue reading →

Using WordPress Custom Fields to Control AdSense Sizes

Two of the most common AdSense placements on blogs are

  1. A rectangular ad (such as the 250×250 unit) in the post, floated to the left, with the text wrapping around it.
  2. A 468×60 “banner” unit between the post title and the content.

In some posts, though, the floated ads get in the way of other elements, such as images. Suppose you want to have an image floated to the right, at the top of your post. That could conflict with your ad, couldn’t it? If the image is wide enough, it would run right into the ad. Or what if you wanted to have a wide image above the content, like on Copyblogger? That left-floated ad would get in the way. What’s the solution? No, you don’t need to switch to a 468×60 ad, which often doesn’t perform as well as the floated ad. It’s time for a little WordPress magic.

Continue reading →

How to Feature Your Best Posts in Your Sidebar

Looking for a way to feature some of your better posts? Here’s a method I’ve been using for a few months (visible on the index and the screencap to the right).

The five most recent posts that I’ve marked as “Featured” will appear in the list, along with a “view all” option that takes the clicker to a custom date-based archive.

How does it work? It’s done with multiple loops and categories.

Continue reading →

Zenphoto – Lightweight Web Gallery Script

Tired of the bloated Coppermine Photo Gallery? The much lighter Zenphoto gallery script may be a good alternative.

The compressed download is under one megabyte, and the installation looks fairly simple. Just upload via FTP, and run the installation script.

The installed gallery is responsive and clean. Don’t take my word for it, though. Look at the demo gallery.

The script is fairly new, and is still evolving. Some features, like RSS support, have just been added.

If you need a gallery (for a design portfolio, to show off your photos, etc), then be sure to check-out Zenphoto.

Start Using Polls on Your Blog

Polls are a great way to solicit opinions, and add some community to your site.

With a poll, a user can just click a button to give you their two cents. Meanwhile, it takes a lot more effort to leave a comment. Which do you think your readers prefer? While comments allow them to say anything they want, polls enable a quick multiple-choice response (which can be very useful). Also, I’ve found that blog posts with attached polls tend to generate more comments.

Once you decide you need a poll, you need to actually add one. You have plenty of choices for implementing your poll. Here are a few of the better services/scripts:

Continue reading →

When Should You Use AJAX?

AJAX, or Asynchronous JavaScript and XML, was probably the most-hyped web programming technique in the last two years. It’s no surprise, as it enables you to do a lot that couldn’t have been done just a few years ago.

AJAX is being used in more and more places, often when it doesn’t need to be…and when it shouldn’t. With all the talk about how you can use AJAX for everything, the real question is when should you.

You should use AJAX in places where it will improve the user experience. One example is with polls. Why should a full pageload be required just to vote in a poll (or view the results)? That’s a waste of your users’ time, and a waste of your server resources.

Do not use AJAX for loading your main content, though. It’s not a good idea. I’ve seen a few sites that have tried it, and it doesn’t work that well. You have to come up with extra solutions for search engines, because they can’t understand your JavaScript trickery (you thought the dreaded “text-only version” link only applied to Flash sites? ). You also cause problems for people using some browsers (Safari, IE5, etc).

Basically, you want to use AJAX for things where an extra pageload would be irritate the heck out of everyone. Suppose you have a star-rating system, like on Netflix. Wouldn’t it be horrible if you had to sit through a page refresh every time you rated a movie? Use your own judgment.