School of Computing  |  Napier  |  Cisco  |  SFC  |  NOS  |  Code |  Old page

PHP - Userdetails

A common task is to log the accesses to a page. This can be done by inserting a graphics image on a page which is only displayed with one pixel by one pixel. The HTML sends off to the PHP file for the graphic, which logs that it has been called, and then returns the graphic file to the page.

Accessing a PHP log file from an HTML page: The following script shows the code that would be inserted into the HTML. It can be seen that the graphic file is displayed as one-pixel by one-pixel, therefore it will be invisible to the user. The PHP file should return the reference to the file in the Location: field.

Logging task

<a href ="http://myserver.co.uk/php_log.php">
<img src="http://myserver.co.uk/php_log.php" border=0 width=1 height=1"></a>

Executed code

 

Login file: This file logs the accesses to the log file, and then returns back the small graphics file. It also tests the log file. If it is greater that 5000 byte, it reads it and sends it to the owner, by email. It then erases the contents of the file (by opening the file with the "w+" attribute.

Login file

<?php
$str = date("F j, Y, g:i a");
$str = "<P>$str Refer: $HTTP_USER_AGENT";
$str = $str . "$REMOTE_ADDR " . gethostbyaddr($REMOTE_ADDR) . "\n";
$filename = "logfile.htt";
$fd = fopen ($filename, "a+");
$size=filesize($filename);
$str = $str . "Size = " . $size;
$contents = fputs ($fd, $str);
if ($size>5000)
{
$headers = "Content-Type: text/html; charset=iso-8859-1\n";
fseek($fd,0);
$contents=fread($fd,filesize($filename));
mail('bill@buchananweb.co.uk',"Logfile", $contents, $headers);
fclose($fd);
$fd = fopen ($filename, "w+");
fclose($fd);
}
else
{
fclose ($fd);
}
print "Location: http://singlepixel.gif\n\n";
?>


Executed code

August 28, 2001, 3:37 pm Refer: Mozilla/4.0 (compatible; MSIE 6.0b; Windows 98; Win 9x 4.90; L1 IE5.5 December 2000)172.190.50.179 ACBE32B3.ipt.aol.com Size = 0
August 28, 2001, 3:38 pm Refer: Mozilla/4.0 (compatible; MSIE 6.0b; Windows 98; Win 9x 4.90; L1 IE5.5 December 2000)172.190.50.179 ACBE32B3.ipt.aol.com Size = 163
August 28, 2001, 3:40 pm Refer: Mozilla/4.0 (compatible; MSIE 6.0b; Windows 98; Win 9x 4.90; L1 IE5.5 December 2000)172.190.50.179 ACBE32B3.ipt.aol.com Size = 328

 

If you have ever wondered what a 1-by-1 pixel graphic looks like then click here.

 

School of Computing  |  Napier  |  Cisco  |  SFC  |  NOS  |  Code |  Old page

Page maintained by bill