LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Multicount in PHP , displays visits on current day, week, month, year and total visit (https://www.linuxquestions.org/questions/programming-9/multicount-in-php-displays-visits-on-current-day-week-month-year-and-total-visit-316569/)

xbaez 04-24-2005 12:23 AM

Multicount in PHP , displays visits on current day, week, month, year and total visit
 
// GLOBAL VARIABLES
$file_name="counter/visits_total_network.txt";
$day_name=date("l"); // Monday-Sunday
$day=date("j"); // 01-31
$month=date("m"); // 01-12
$year=date("Y"); // 2005
if (empty($ip) ) { $ip = getenv('REMOTE_ADDR'); }

// IF FILE DOESN'T EXISTS, CREATE IT WITH FORMAT
if (!file_exists($file_name)) {
fopen ($file_name,"w+"); //create file if doesn't exists
if (!isset($ip)) {$ip="xx.xx.xx.xx";}
$counter="1|$ip|1|1|1|1|$day|$day_name|$month|$year";
flock($fp, LOCK_EX); # exclusive lock
fwrite($fp,$counter);
flock($fp, LOCK_UN); # release the lock
fclose($fp);
}
// CONVERTING TO ARRAY
$fp=fopen($file_name,"r");
$file_contents=fread($fp,filesize($file_name));
$counter_array=explode("|",$file_contents);
fclose($fp);

// CHECK IF FILE HAS THE FORMAT OF VERSION 1.0
if ( empty($counter_array[2]) ) {
$fp=fopen($file_name,"w+");
$counter=$counter_array[0]."|".$counter_array[1]."|".$counter_array[0]."|".$counter_array[0]."|".$counter_array[0]."|".$counter_array[0]."|".$day."|".$day_name."|".$month."|".$year;
flock($fp, LOCK_EX); # do an exclusive lock
fwrite($fp,$counter);
flock($fp, LOCK_UN); # release the lock
fclose($fp);
$fp=fopen($file_name,"r");
$file_contents=fread( $fp,filesize($file_name) );
$counter_array=explode("|",$file_contents);
fclose($fp);
}// close if, check for antique format in txt file

// START CHECKING IF DAY, WEEK, AND MONTH ARE THE SAME
if ($day!=$counter_array[6]) {
$fp=fopen($file_name,"w+");
$counter_array[6]=$day;
$counter_array[2]=0;
$counter=$counter_array[0]."|".$counter_array[1]."|".$counter_array[2]."|".$counter_array[3]."|".$counter_array[4]."|".$counter_array[5]."|".$counter_array[6]."|".$counter_array[7]."|".$counter_array[8]."|".$counter_array[9];
fclose($fp);
}

// IMPORTANT: Problem here

if ($year!=$counter_array[9]) {
$fp=fopen($file_name,"w+");
$counter_array[9]=$year;
//$counter_array[5]=0;
$counter=$counter_array[0]."|".$counter_array[1]."|".$counter_array[2]."|".$counter_array[3]."|".$counter_array[4]."|".$counter_array[5]."|".$counter_array[6]."|".$counter_array[7]."|".$counter_array[8]."|".$counter_array[9];
fclose($fp);
}
// End problem, fix tomorrow


if ($day_name!=$counter_array[7] && $counter_array[7]=="Sunday") {
$fp=fopen($file_name,"w+");
$counter_array[7]=$day_name;
$counter_array[3]=0;
$counter=$counter_array[0]."|".$counter_array[1]."|".$counter_array[2]."|".$counter_array[3]."|".$counter_array[4]."|".$counter_array[5]."|".$counter_array[6]."|".$counter_array[7]."|".$counter_array[8]."|".$counter_array[9];
fclose($fp);
}
if ($month!=$counter_array[8]) {
$fp=fopen($file_name,"w+");
$counter_array[8]=$month;
$counter_array[4]=0;
$counter=$counter_array[0]."|".$counter_array[1]."|".$counter_array[2]."|".$counter_array[3]."|".$counter_array[4]."|".$counter_array[5]."|".$counter_array[6]."|".$counter_array[7]."|".$counter_array[8]."|".$counter_array[9];
fclose($fp);
}
// FINISH CHECKING IF DAY, WEEK, AND MONTH ARE THE SAME


#if ( empty($ip) || $counter_array[1]!=$ip ) { //check if the ip address is different
$counter_array[0]++;
$counter_array[2]++;
$counter_array[3]++;
$counter_array[4]++;
$counter_array[5]++;
$counter=$counter_array[0]."|".$ip."|".$counter_array[2]."|".$counter_array[3]."|".$counter_array[4]."|".$counter_array[5]."|".$counter_array[6]."|".$counter_array[7]."|".$counter_array[8]."|".$counter_array[9];
$fp=fopen($file_name,"w+");
flock($fp, LOCK_EX); # do an exclusive lock
fwrite($fp,$counter);
flock($fp, LOCK_UN); # release the lock
fclose($fp);
#}// close if

//Envio a impresion de la variable contenido, para ser mostrada con un javascript
$fp=fopen($file_name,"r");
$file_contents=fread( $fp,filesize($file_name) );
$counter_array=explode("|",$file_contents);
echo "<center><font size='2'>".$counter_array[0]."\n";
echo "visit (".$counter_array[2]." today, ".$counter_array[3]." this week, ".$counter_array[4]." this month, ".$counter_array[5]." this year)<br>\n";
echo "<i>Page views to the GamingAccess Network since March 31st, 2005</i>\n";
echo "</font></center><br>\n";
?>


Can anybody please help me?
This counter gets resetted about every day :(

Regards

xbaez 04-24-2005 02:50 AM

The main problem is that at the start of the script, the variable $day_name gets stored

However when the script tries writes to the counter/counter_total_network.txt file, that variable isn't recorder.


All times are GMT -5. The time now is 12:22 AM.