Location: PHPKode > scripts > Butterfly - online vistors counter > butterfly/scripts/butterfly.php
<?php
session_start();
$visitor_id = session_id();

#################################################
#    Butterfly - online vistors counter v.9.0   #
#################################################
#
#Copyright (C) 2005  Karma Wangchuk Sonam 
#This program is free software; you can redistribute it and/or
#modify it under the terms of the GNU General Public License
#as published by the Free Software Foundation; either version 2
#of the License, or (at your option) any later version.
#
#Check http://www.phoenixphp.net for newer version
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with this program; if not, write to the Free Software
#Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
############################################################################

include_once ("butterfly_custom.php"); // include settings
date_default_timezone_set($setTimezones);
$day = date("z"); // day in year
$time = time(); // current time

$dir = "../visitors"; // folder with visitors files
$name = "$day-$visitor_id-$time-.txt"; // create name of files

$extend = FALSE; // set extend files on false

// open and read folder
$openDir  = opendir($dir); 
while (false !== ($fileNames = readdir($openDir))) {
          $allfiles[] = $fileNames;   
    }
	
// kick out anything what is not txt file	
for($o = 0; $o < count($allfiles); $o++) { 
		      list($front, $ext) = explode(".", $allfiles[$o]);
			  if($front == "" || $ext != "txt") {
			       continue;
			  } else {
			       $restfiles[] = $allfiles[$o];
			  }
		}
// count and explode file names, delete all files older then preset time 
// and create array with others
for($a=0; $a<count($restfiles); $a++) { 
list($day1, $sess1, $times1, $ext1) = explode("-", $restfiles[$a]);
    if(($time - $times1) > $setTime+2) {
	      unlink("$dir/$restfiles[$a]");   
	} else {
	      $files[] = $restfiles[$a];
    }
}

// if ID exist then rename it with new time, otherwise create new file
$visit = count($files); 
for($i=0; $i<count($files); $i++) {
list($day, $sess, $times, $ext) = explode("-", $files[$i]);

    if(strpos($files[$i], $visitor_id) > 0 ) {  
	     rename("$dir/$files[$i]", "$dir/$name");
	     $extend = FALSE;	
	     break;        
	
	 } else {	
	     $extend = TRUE;
	}
}

// check $extend and chose right number of visitors
if($extend == TRUE && $visit > 1){
   $visitors = $visit;
} elseif($extend == TRUE && $visit == 1) {
   $visitors = 1;
} else {
   $visitors = $visit-1;
}

// write new file for new visitor
if($extend == TRUE) {
   $extendFiles = $dir . "/" . $name;
   $newFile = fopen($extendFiles, "ab");
   fwrite($newFile, "http://phoenixphp.net");
   fclose($newFile); 
}

$checkAgain = $setTime * 1000;
$ajaxVisitors = $visitors;

// send XML headers
header('Content-type: text/xml');
echo '<?xml version="1.0" ?><online><visitors>';
echo htmlentities($ajaxVisitors);
echo "</visitors><reload>$checkAgain</reload></online>"; 
?>
Return current item: Butterfly - online vistors counter