Creating a Beacon Image

Until recently I was stuck with trying to get a script to return a 1 x 1 (sized) gif image back to the user's browser. The 1x1 pixel gif image returned by most scripts is popularly known as a beacon image. It's used extensively by most scripts as a means to verify or log views of banners and such.

Anyway, I had been working on a script that needed this to be in place but like I said, I was stuck with trying to show a beacon image at the end of the script.

Some necessary background information of the problem

I had a JavaScript code that would collect the screen resolution of the user and send the information back to the server (and PHP) that looks something like this:

code:
<img src="/set_width.php?w=800" alt="" border="0" />

Now, the file set_width.php should process the information (i.e. width = 800) and return a 1x1 beacon image back to the user's browser.

Sending 'content-type' headers

The first problem was easily fixed. I used the PHP header() function to let the webserver send the right header - however, this was just part of the solution.

php:


<?php

//  Filename: SET_WIDTH.PHP

//  Initialise session stuff here

//  Start processing user's browser information
$_SESSION['width'] = ( !is_numeric($_GET['w']) ? 800 : $_GET['w'] );
//  End of processing.

// Send a BEACON image back to the user's browser
header( 'Content-type: image/gif' );

?>


All you will get using this script is the dreaded Missing Image File icon where you were expecting the transparent, 1x1 pixel sized, gif image!

PHP code for a transparent, 1x1 pixel sized, gif file

Perhaps one day soon, I will tell you how I figured this one out but for now, here's the code:

php:


<?php

//  Filename: SET_WIDTH.PHP

//  Initialise session stuff here

//  Start processing user's browser information
$_SESSION['width'] = ( !is_numeric($_GET['w']) ? 800 : $_GET['w'] );
//  End of processing.

//  Send a BEACON image back to the user's browser
  header( 'Content-type: image/gif' );
  # The transparent, beacon image
  echo chr(71).chr(73).chr(70).chr(56).chr(57).chr(97).
      chr(1).chr(0).chr(1).chr(0).chr(128).chr(0).
      chr(0).chr(0).chr(0).chr(0).chr(0).chr(0).chr(0).
      chr(33).chr(249).chr(4).chr(1).chr(0).chr(0).
      chr(0).chr(0).chr(44).chr(0).chr(0).chr(0).chr(0).
      chr(1).chr(0).chr(1).chr(0).chr(0).chr(2).chr(2).
      chr(68).chr(1).chr(0).chr(59);
?>


Of course, like nearly everything else with programming, there are probably more ways to 'skin a cat' but this works for me and until I figure out a better way, this is how I will keep doing it! :)

TOP

Do you like the new design?

 Yes

 No

 Don't know