Bouncing visitors to another page

Let’s imagine you have a page on your web site that you don’t want visitors to your site to be able to access until they have seen another page first. For example, if you have content on that page that you want to make clear has terms and conditions attached to its use; or perhaps the nature of the content might disturb sensitive souls, and you want them to be forewarned before they read it!

The problem with any plain old unprotected HTML page is that anyone can point their browser straight to it if they know the URL. In fact, some visitors might land there directly from a search engine, possibly by accident when looking for something else.

The solution

One solution might be to password-protect the sensitive page. You could link to the page only from the page you want visitors to read first, and provide on that page (perhaps at the foot, after the stuff you want them to read) the user name and password they will need to enter your protected page. A slight complication to this method is that — and this does depend on your passwording mechanism — it’s likely that anyone coming to your protected page by entering the URL directly into their browser or from a link on another site is going to encounter your password dialogue, and not know what to do from there. So you would need to put information in your password dialogue to direct them to the page you want them to read first. (Read that again, slowly…)

Another method is not to keep your protected page as a static HTML page at all, but rather use a CGI program to build the page “on the fly”. That same CGI program could first of all

  • detect whether the visitor has come from the page you want them to read first;
  • if so, then deliver your sensitive page;
  • if not, deliver some kind of warning or explanation; and
  • redirect them to your “read me first” page.

This is the method I’ll describe here.

A quick ’n’ dirty example

Let’s say that this page, the one you’re looking at right now, is the page I want you to see before going on to my protected page. On this page, I’d have a link to the CGI program like this (don’t click it yet!):

My super-sexy protected page

Before you try clicking the link, copy and paste the URL of the program into your browser’s address bar, punch the return key, and see what happens. Here’s the full URL:

You’ll find you get an explanatory page before being redirected back here.

Now you can click the link above to the program. This time, you’re transported to my protected page, because you’ve come from this page — just as I wanted.

What you need

You will need

  • access to a CGI directory on your web server;
  • Perl installed on your server (most web hosts and ISPs are very good a keeping the latest and greatest version on their servers, but in any case this is a simple program that will work with older versions of Perl);
  • a way to install the program on your server.

All of these matters are dealt with in more detail on the CGI basics page.

You will also need the program script bounce.cgi and the readme.txt files, which you can download now. These are supplied as compressed archives in two formats, Zip (.zip) and StuffIt (.sit). Choose whichever format you prefer.

Download file Zip format      or      Download file StuffIt format

Finally, you will need the protected page itself. Just build it like a normal HTML page initially. In due course, you are going to drop all of the HTML code for the page into the CGI program. One thing to watch out for when you’re coding this page: references to other files on your server need to be relative to the directory holding the CGI program, not to the directory holding your “see me first” page. If you get confused by relative URLs, perhaps the easiest thing to do is to make every URL either a full absolute URL like this:

or the equivalent local absolute URL fragment like this:


What to do

Once you’ve downloaded the archive, decompress it and open the files named bounce.cgi and readme.txt in a text editor. (Need help decompressing archives? Note: this link opens in a new browser window.)

Edit the configuration variables in bounce.cgi as described in readme.txt (there’s really very little to do).

Save the modified program file, changing the extension if your server needs something other than .cgi.

Upload the CGI program to your CGI directory, making sure you do so in ASCII mode. If your server is a Unix-type, you need to make sure the file permissions are set to 755 (see the CGI basics page for an explanation).

Of course, the page that you want visitors to see first needs to have a link on it to your protected page. The link is just a link to the CGI program. In the href attribute of the <a> tag, enter the full or local absolute URL of the CGI program, like this:

or this: /cgi-bin/bounce.cgi

Copyright, licence and support

Err… there isn’t any. I wrote it, but I make no copyright claim on the program script. I deem it to be free software that you can use, modify or redistribute as you wish. One thing, though: don’t copy and redistribute my explanation of the program on this page — that is copyright material. But feel free to link to this page for others to get the details.

Sorry, but I don’t have time to offer support on this script, so you’re on your own. But I think the instructions on this page, on the CGI basics page, and in the readme.txt file should cover most eventualities.

Of course, the script comes “as is”, with no warranties of any kind, express or implied, as the lawyers say. Use at your own risk.

This page was last modified on Thursday, February 26, 2009