Pretty new to PHP but I've always wanted to make a user tracking system.
I'm trying to grab a number (sessionNum) out of a text file and increment it whenever 
someone leaves and comes back to the site.. the problem is the text file appears to be 
cached somewhere, the first time someone comes back the value is incremented and 
appended but if they leave again and then come back the file's contents appear to be 
unchanged. Opening the file up after the first time reveals that it has been appended 
but after subsequent visits it is not (but yet it still goes through the function).
I tried just appending the info on with 'a' but since it didnt work I have it 
rewriting the whole file.... still doesnt work however...
thanks if you can give any help.

an example of the file's contents:
1-12:17:2:Tuesday:February:11:2003--page1,page2,page3,
(sessionNum-date-referer-pagelist)

the function I'm using :
function incSession($trackFile, $pagename){
   
   if (file_exists($trackFile)){

      //read the file
      $fileLine = file_get_contents($trackFile);

print "alert('File contents before write : $fileLine');";

      //split the tracker file's contents into major sections 
Session-Date-Referer-Pages
      $headerArray = explode("-", $fileLine);

      //get correct entry for last session number in the tracker file
      $headerArrLen = count($headerArray);
      $arrayentry = $headerArrLen - 3; //back 3 to get last session number entry
      $arrayentry = $arrayentry - 1;  //correct number to account for number sys. 
beggining with 0

      //get the old session number and increment it
      $sessionNum = $headerArray[$arrayentry];
      $sessionNum = $sessionNum + 1;

      //get timestamp-user function
      $currentDate = makeDate('PHP');

      //get referer site if it exists
      $referer = $_SERVER['HTTP_REFERER'];

      //put it together for output
      $addon = '-' . $sessionNum . '-' . $currentDate . '-' . $referer . '-' . 
$pagename;

      $everything = $fileLine . $addon;
      
print "alert('To be appended to file : $addon');";
print "alert('File contents after write : $everything');";

      //user tracker file is appended with new session date referer and page
      $fp = fopen($trackFile, 'w');
      fwrite($fp, $everything);
      fclose($fp);
      
   }
}

Reply via email to