Jump to content

Help debug php user tracking script.


djkee

Recommended Posts

Hello everyone, i have this script that i am trying to implement to work on my website and the script is suppose to track what games are being played at the moment but the only problem is that every time it records in the .txt file the script would erase everything else in the file and just write a new line and i want the script to keep all the other records in the file.

 

What the script does:

Records the gameID, gamename, gamethumb url, IP and time all separated by |.

Example: 1744|The Simpliest Snowboarding|The Simpliest Snowboarding|77.88.42.26|1264717552

 

Look for the IP and if already exists update the record with the new info.

If the IP does not exist already write a new line with the information.

If the record is older then 60min erase it.

 

Its just a simple script that i will use to show what people are currently playing on the website.

 

Here is the script:

$dataFile = "visitors2.txt";

$sessionTime = 60; //this is the time in **minutes** to consider someone online before removing them from our file

//Please do not edit bellow this line

error_reporting(E_ERROR | E_PARSE);

if(!file_exists($dataFile)) {
   $fp = fopen($dataFile, "w+");
   fclose($fp);
}
$game2id = $this->get_template_vars('gameId');
$gamethumb = $this->get_template_vars('gameFilename');
$ip = $_SERVER['REMOTE_ADDR'];
$gamename = $this->get_template_vars('gameName');
$users = array();
$onusers = array();

//getting
$fp = fopen($dataFile, "r");
flock($fp, LOCK_SH);
while(!feof($fp)) {
   $users[] = fgets($fp, 32);
}
flock($fp, LOCK_UN);
fclose($fp);


//cleaning
$x = 0;
$alreadyIn = FALSE;
foreach($users as $key => $data) {
//list( , , , $lastvisit) = explode("|", $data);
list( , , , , $lastvisit) = explode("|", $data);
   if(time() - $lastvisit >= $sessionTime * 60) {
      $users[$x] = "";
   } else {
      if(strpos($data, $ip) !== FALSE) {
         $alreadyIn = TRUE;
         $users[$x] = "|$game2id|$gamename|$gamethumb|$ip|" . time(); //updating
      }
   }
   $x++;
}

if($alreadyIn == FALSE) {
   $users[] = "$game2id|$gamename|$gamethumb|$ip|" . time();
}

//writing
$fp = fopen($dataFile, "w+");
flock($fp, LOCK_EX);
$i = 0;
foreach($users as $single) {
   if($single != "") {
      fwrite($fp, $single . "\r\n");
      $i++;
   }
}
flock($fp, LOCK_UN);
fclose($fp);

 

I hope somebody can help me :)

Please let me know if you need more information and i will provide it.

 

Thanks everyone in advance :D

Link to comment
https://forums.phpfreaks.com/topic/190165-help-debug-php-user-tracking-script/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.