Turn hotlinks into advertising - part II

In Monday's entry, I explained some simple techniques to turn a drain on resources into a free 1 method of advertising.

In today's entry, I'll be going into more depth about how to use a little PHP to dynamically generate your adverts. Why would you want to do this? For starters, you can modify the image to include a discount code, which will help track where the orders came from. Who knows, you could even strike up a deal with the site owner to display ads on their site should it bring you a lot of customers.

Please note that most of the code will just be fragments, and I'm not tying it down to one implementation. It's quite likely that I'll modify this method over time and post updates in the future. There's also the small question of “is it worth it”. Again, I'll keep you updated.

First things first

First, we'll create a database table to store several things:

  • The full referring URL – The unique identifier.
  • The number of hits to this file.
  • The discount code, either for this URL or for the domain.

Nothing particularly complex there, although you may wish to tweak it to allow for different advert styles.

Basic PHP

Our file, which I'll call "image-script.php" needs to carry out several functions:

  • Check to see if we've had a referrer from this site before.
    • If we have, log the hit and get the discount code
    • If we have not, create an entry and generate a discount code.
  • Create and output an image

Lets look at some sample code :

// Get the referrer and look for this site in the database
// Please note: HTTP_REFERER is not a typo
$referrer_url = $_SERVER['HTTP_REFERER'];
$siteData = find_site($referrer_url);

if ($siteData) {
    // Seen the site before, so grab discount code
    add_site_hit($referrer_url);
    $discountCode = $siteData['discount_code'];
} else {
    // A new site, so create a discount code and add it to the db
    $discountCode = create_discount_code($refferer_url);
    add_new_site($referrer_url, $discountCode);
}

// Image Creation code

create_discount_code would need to be tailored to each particular solution, and I've deliberately left out the database code to save space.

Image Creation Code

The final bit of code is creating our image, complete with embedded discount code. You'll need the GD engine installed to use these functions. More information about that is available in the image section of the PHP manual.

The code itself is relatively simple. We open an advert image, and then add the discount code to it before outputting it to the browser.

// Image Creation code

// Setup where we want the code to appear
$codeX = 5;
$codeY = 5;

// Load advert image.
$advertImage = imagecreatefrompng('background-image.png');

// Draw discount code
$textColour = imagecolorallocate($advertImage, 255, 255, 255);
imagestring($advertImage, 3, $codeX, $codeY, $discountCode, $textColour);

// Output our image and cleanup
header('content-type: image/png');
imagepng($advertImage);
imagedestroy($advertImage);

In case you're wondering if creating an image works, here's one I made earlier (using slightly modified code for the shadow and position).

image-0001.png

One of the advantages of using dynamically generated images is that you can modify the code to perform a split test if you wish to check ad wordings or appearances. That falls outside the scope of this article, but I may write about it in future.

All done

So there we have it – how to change an unpleasant situation into something that can benefit your business.

The code is still a little rough around the edges, so I'll be cleaning it up to make it ready for release. My goal is to create something that is simple to install and use, which will take some work.

In case anybody's wondering, I (was) using "iG::Syntax Hiliter" to produce the highlighted code snippets.

Footnotes:

1

Not including bandwidth costs, but seeing as your bandwidth was already being stolen at least now you're getting some value out of it.


Turn hotlinks into advertising

If you own a website, then it's likely you've had images "hotlinked" or "leeched". Not only is this annoying and rude, but it also costs bandwidth. The usual response is to replace the linked image with something suitably amusing or offensive, but in today's post I'm going to look at the alternatives.

A recent post in the private ASP newsgroups by Bret Reece of Five O'clock Software interested me. After some tweaking, the hotlinked image became "free" advertising. I thought about this and realised that with a bit of coding wizadry, a more flexible method could be developed.

There are several methods of preventing hotlinking, ranging in difficulty.

The Easy method

Replace the linked image file with your “special” image. We'll assume that it's a pleasant advert for your software or company, rather than something that will blind them. Simple and easy, although you will need to replace references to the image on your own site, which can be time consuming.

The Advanced Method

If you're using an Apache web server and have access to modrewrite, you can automatically redirect offsite referrers to your special image. The code below, placed in a “.htaccess” file will do the trick. If you're not sure how to do this, I recommend this tutorial at freewebmasterhelp.com.

The following code will redirect all external referrers that link to png, gif of jpg files to "www.your-domain.com/image.gif". You can add more "safe" urls to the list if required.

<ifmodule mod_rewrite.c>
  RewriteEngine on

  # Make sure referrer is not blank or from your site.
  RewriteCond %{HTTP_REFERER} !^$
  RewriteCond %{HTTP_REFERER} !^http://(www.)?your-domain.com/.*$ [NC]

  # Redirect the offsite image links to your anti-hotlink image
  RewriteRule .(gif|jpg|png)$ http://www.your-domain.com/image.gif [R,L]
</ifmodule>

The Extra Special Method

If you want to get really technical, you can combine PHP with your .htaccess file to dynamically generate images for each site. Instead of redirecting to "image.gif", you would redirect to "your-image-script.php". This script would then dynamically generate an image using PHP's imaging functions.

In Thursday's entry, I'll cover the PHP side of this method in depth. This will include using a MySQL database to track referrers, as well as using discount coupon codes to track where the orders come from.

See Also:Turn hotlinks into advertising - part II


Joe Indie on starting small

A good post over at Joe Indie about why indies should start with a small project before moving on to bigger things. He states the main benefits of this approach as:

  1. Experience with the entire software development process.
  2. Experience shipping software to end users.
  3. Experience setting up shop on the Web.
  4. Experience as a business entity with a product to sell.

All of these are excellent points, and after some thought I've expanded on them:

Experience with the entire software development process.

As a developer at heart, I'd put this at the top of my list. A small project gives you a chance to try out tools that you may later rely on, such as code versioning systems and build tools. It also gives you a chance to experiment with development methodologies, design processes and all the other wonderful things that come with developing software. As an indie, you have the particular joy of being able to do things your own way, so it's a good idea to find what works best for you.

It's important to push yourself to learn at this point. The object of the exercise is to produce to small, fully functional product, which brings me nicely onto the next part.

Experience shipping software to end users.

How are your support skills? Do you have a system for tracking user problems? How will you keep track of what bugs you've fixed (and when)? How will you deliver these fixes to end users? More importantly, how easy is it for an end user to apply a patch?

You can guarantee that as soon as your software is released, it will start misbehaving. If you've ever demonstrated one of your products to someone else, you'll know the phrase "Hmm, it's not supposed to do that".

Experience setting up shop on the Web.

Which payment processor will you use? Plimus? BMT Micro? ShareIT? Do you want to offer affiliate opportunities?

Next comes writing the product page that will sell your product, which is perhaps one of the toughest things to do. My advice: Get as many of your friends to read it as possible. Experiment with layouts, phrases and colours. Remember that you have the flexibility of a website, and that you can change it whenever you wish. Take advantage of it!

Marketing is another important factor, and this is a good chance to experiment with different kinds of marketing. Will you handle it all yourself, or use a third party such as Shareware Promotions?

Experience as a business entity with a product to sell.

Tax, insurance and other legal issues abound here. If you don't know much about the legal side of business, now would be a good time to learn!

And more… There's far too much to cover in a single article, but the important thing is to make sure you learn something from your experiences. Experiment, and make sure you find what works for you.

Read: Why New Indies Should Start with a Small Project; or "How do you get to Carnegie Hall?"


Five utilities that make my life easier

I've installed a lot of applications on my PC over the years, but several have earned the distinct honour of being allowed to stay:

  1. Freemind – An excellent mind-mapping tool that makes it easy to grab lots of ideas. I use this to note down all my rough ideas, and although it's not quite as flexible as a pen and paper, it's extremely useful.
  2. Keynote - I've tried several tools like this, and although it doesn't have the power of Microsoft's OneNote, it's much smaller and sits quite happily in the tray.
  3. CommandBar - If you use the command line for any amount of time, this is an invaluable asset. It allows you to open an explorer bar, similar to "History" and "Favourites", that contains a working command prompt. I use this quite regularly, usually when building resource files for software, and it has made my life so much easier.
  4. Foldersize- Another explorer extension, this time allowing you to view the size of a folder in a column in explorer. Quite handy when trying to find junk hiding on your hard drive.
  5. Vim - There's not a lot for me to say about Vim that hasn't already been said. I use it mostly for editing batch files and .htaccess files, but also if I just need to edit a file quickly.

I hope you find them as useful as I do.