The Stickybeak

Home : Documentation : Examples : Download


Examples


Image Based Logging

Pixel Logging

The following code will produce a 1x1 transparent pixel which can be used as an image in any webpage. It is available in the file pixel.php in The Stickybeak distribution.


 <?

 $config=array(
  "mysqlhost" => "localhost", 
  "mysqldb" => "stickybeak", 
  "mysqltable" => "stickybeak_logs",    
  "mysqluser" => "username", 
  "mysqlpass" => "password",
  "p3pURL" => "http://www.example.com/w3c/p3p.xml"
  "pixel" => true 

 );

 include "TheStickybeak.php";
 $s=new stickybeak($config);
 $s->log();

 ?>

Using custom images

It is possible to configure The Stickybeak to use a specified image instead of the default transparent pixel. This is done by using the image and imagetype variables in the configuration array.

The following code is available in the file image.php in The Stickybeak distribution.


 <?

 $config_image=array(
  "mysqlhost" => "localhost", 
  "mysqldb" => "stickybeak", 
  "mysqltable" => "stickybeak_logs",    
  "mysqluser" => "username", 
  "mysqlpass" => "password", 
  "p3pURL" => "http://www.example.com/w3c/p3p.xml",
  "image" => "TheStickybeak.png", 
  "imagetype" => "png"
 );

 include "TheStickybeak.php";
 $s=new stickybeak($config_image);
 $s->log();

?>


Including The Stickybeak in Webpages

HTML Code

The simplest way of including an image logger into a webpage is by placing the following HTML somewhere within the page you want to log.


<img src="pixel.php">


The image can also be called from a remote server, allowing centralised logging of multiple websites.


<img src="http://www.example.com/pixel.php">

For good measure specify an alternative text, width and height in the <img> tag.


<img src="pixel.php" width="1" height="1" alt="The Stickybeak" />

One of the limitations of using an image to log accesses to a webpage is that the referrer variable is aways set to be the page that included the HTML calling the image. Under normal circumstances the referrer would be the page containing the link the user had ckicked on to arrive at the page being logged. To ensure that the referrer of the page is logged rather than the referrer of the image, the referrer variable can be passed to the image as GET variables in the URL.

Several other variables may be passed to The Stickybeak via the <img> tag. The value of each variable is base64 encoded to ensure that a valid URL results. The variables that may be passed to The Stickybeak in this manner are:

An example of the HTML code required to send additional logging variables to The Stickybeak appears below:


<img 
alt="The Stickybeak" 
src="http://www.example.com/log.php?
    h=d3d3Lm5ld2Nhc3RsZW11c2ljLm5ldA==
   &u=L25ld3MucGhw
   &r=aHR0cDovL3d3dy5uZXdjYXN0bGVtdXNpYy5uZXQv
   &p=bmV3cw==" 
width="1" 
height="1"  
>

Although several of these variables may be the same each time the page is called, others will be different each time the page is called. For this reason it is beneficial to generate the <img> tags dynamically each time the page is requested. This allows the referrer and other variables to be base64 encoded and passed to The Stickybeak as GET variables. Several techniques can be used to dynamically generate the <img> tag including PHP and Javascript. These methods are outlined below.

PHP Generated Image Tags

The following PHP function will return the HTML code required to call The Stickybeak with base65 encoded variables including referrer and the page and identifier user defined variables.



 function stickybeak_image_code($page="",$identifier=""){

	$string.="      <img alt=\"\" src=\"image.php?";
	$string.="h=";
	$string.=base64_encode($_ENV["HTTP_HOST"]);
	$string.="&u=";
	$string.=base64_encode($_ENV["REQUEST_URI"]);
	$string.="&r=";
	$string.=base64_encode($_ENV[HTTP_REFERER]);
	if($page){
		$string.="&p=";
		$string.=base64_encode($page);
	}
	if($identifier){
		$string.="&i=";
		$string.=base64_encode($identifier);
	}
	if($_ENV["QUERY_STRING"]){
		$string.="&q=";
		$string.=base64_encode($_ENV["QUERY_STRING"]);
	}
	$string.="\" ";
	$string.=" width=\"88\" height=\"31\" ";
	$string.="/>\r\n";
	return $string;
 }



In order to use the above function, include the following HTML code somewhere in the page to be logged.


<?

 echo stickybeak_image_code("examples","image");

?>


JavaScript Generated Image Tags


<script type="text/javascript">

var base64s="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

function encode(decStr){

  var bits, dual, i = 0, encOut = '';
  while(decStr.length >= i + 3){
    bits =
    (decStr.charCodeAt(i++) & 0xff) <<16 |
    (decStr.charCodeAt(i++) & 0xff) <<8  |
     decStr.charCodeAt(i++) & 0xff;
    encOut +=
     base64s.charAt((bits & 0x00fc0000) >>18) +
     base64s.charAt((bits & 0x0003f000) >>12) +
     base64s.charAt((bits & 0x00000fc0) >> 6) +
     base64s.charAt((bits & 0x0000003f));
    }
  if(decStr.length -i > 0 && decStr.length -i < 3){
    dual = Boolean(decStr.length -i -1);
    bits =
     ((decStr.charCodeAt(i++) & 0xff) <<16) |
     (dual ? (decStr.charCodeAt(i) & 0xff) <<8 : 0);
    encOut +=
      base64s.charAt((bits & 0x00fc0000) >>18) +
      base64s.charAt((bits & 0x0003f000) >>12) +
      (dual ? base64s.charAt((bits & 0x00000fc0) >>6) : '=') +
      '=';
    }
  return encOut
  }


function js_pixel_code(page,identifier){
	document.write("      <img alt=\"\" src=\"pixel.php?");
	document.write("h=");
	document.write(encode(document.domain));
	document.write("&u=");
	document.write(encode(document.URL));
	document.write("&r=");
	document.write(encode(document.referrer));

	document.write("&p=");
	document.write(encode(page));
	if(identifier){
		document.write("&i=");
		document.write(encode(identifier));
	}

	document.write('\" ');
	document.write(" width=\"1\" height=\"1\" ");
	document.write("/>\r\n");

	return true;
}


</script>



  <script type="text/javascript">
    js_pixel_code("examples","javascript_pixel");
  </script>

  <noscript>
    <img src="pixel.php" width="1" height="1" alt="The Stickybeak" />
  </noscript>


Embedded PHP

This method of installing The Stickybeak is the only method that does not use image based logging.

The Stickybeak can be embedded into existing PHP code on a webpage using the code below.


 $config_standard=array(
  "mysqlhost" => "localhost", 
  "mysqldb" => "stickybeak", 
  "mysqltable" => "stickybeak_logs",    
  "mysqluser" => "username", 
  "mysqlpass" => "password", 
  "usecookie" => true
 );

 include "TheStickybeak.php";
 $s=new stickybeak($config_standard);
 $s->log();

When The Stickybeak is embedded in such a way the log() function must be called before any other output has been sent to the browser as it sets a cookie. If cookies are not used the code may be embedded at any stage of the PHP script from which it is called.


The Stickybeak Powered by PHP Powered by MySQL

© 2001-2021