This is pretty simple to accomplish with a little PHP
Fisrt you create two PHP pages, one called
track.php and one called
index.php.
First we need to create the actual tracking page.
This is the
track.php page
PHP Code:
//VARIABLEs
$CHECK_URL = TRUE;
$CHECK_URL_REGEX = "/^(http:|ftp:\/\/).*(\.+).*/i";
$CHECK_REFERER = FALSE;
$REFERER_SUBSTRING = "yoursite.com";
/*--------------
DO NOT EDIT BELOW THIS LINE!
--------------*/
$QS = $_SERVER["QUERY_STRING"];
if($CHECK_URL && !preg_match($CHECK_URL_REGEX, $QS)){
//this didn't match
echo "<p>The link does not seem to point to a valid URL.</p>";
exit;
}
if($CHECK_REFERER){
if (!stristr($_SERVER["HTTP_REFERER"], $REFERER_SUBSTRING)){
echo "<p>The referring site does not match. Please make sure that the link you clicked came from the right site.</p>";
exit;
}
}
$LogFilePath = dirname(__FILE__) . "/" . md5($QS) . ".log";
//echo "Log path: " . $LogFilePath;
/*
Log file format
The file contains individual pieces of information per line. The lines are:
0: QS, which is the URL
1: Last hit unix time stamp
2: No. of requests
3: Log creation time
*/
//If the file doesn't exist, create it and write the log.
if(!file_exists($LogFilePath)){
touch($LogFilePath);
chmod($LogFilePath, 0666);
$LogString = $QS . "\n" . time() . "\n1\n" . time();
$FH = fopen($LogFilePath, "w+");
fwrite($FH, $LogString);
fclose($FH);
}
else{
$FA = file($LogFilePath);
$Hits = trim($FA[2]);
$Hits += 1;
$FA[2] = $Hits . "\n";
$FA[1] = time() . "\n";
$LogString = implode("", $FA);
$FH = fopen($LogFilePath, "w+");
fwrite($FH, $LogString);
fclose($FH);
}
header("Location: " . $QS);
Next we create the
index.php page.
This pages hould have a 1 x 4 table on it.
the table creation html is
Quote:
<table cellspacing="0" cellpadding="2" border="0">
<tr>
<td class="tblHeading">Requested URL</td>
<td class="tblHeading">Number of Requests</td>
<td class="tblHeading">Most Recent Hit</td>
<td class="tblHeading">First Recorded Hit</td>
</tr>
</table>
</body></html>
|
the code included in it would be:
PHP Code:
$dir = dirname(__FILE__);
// Change to directory
chdir($dir);
// Open directory;
$handle = @opendir($dir) or die("Directory \"$dir\"not found.");
// Loop through all directory entries, construct
// two temporary arrays containing files and sub directories
while($entry = readdir($handle))
{
if($entry != ".." && $entry != "."){
$PathParts = pathinfo($entry);
if($PathParts["extension"] == "log"){
$dir_files[] = $entry;
}
}
}
// Print all files in the curent directory
for($i=0; $i<count($dir_files); $i++){
$FA = file($dir_files[$i]);
echo "<tr><td class=\"DataTD\"><a href=\"" . $FA[0] . "\">".$FA[0]."</a></td><td class=\"DataTD\">".$FA[2]."</td><td class=\"DataTD\">".date("D dMy\nh:iA", $FA[1])."</td><td class=\"DataTD\">".date("D dMy\nh:iA", $FA[3])."</td></tr>";
}
// Close directory
closedir($handle);
Next we track the links.
Any link you want tracked (say
www.yoursite.com/file1.zip) then your link would need to be:
/trak.php?
http://www.yoursite.com/file1.zip
note!!
the /track.php assumes you created this in the same directory as the rest of your links files.
Good luck with it.