LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-24-2005, 12:23 AM   #1
xbaez
Member
 
Registered: Mar 2004
Location: USA
Distribution: Ubuntu
Posts: 284

Rep: Reputation: 30
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
 
Old 04-24-2005, 02:50 AM   #2
xbaez
Member
 
Registered: Mar 2004
Location: USA
Distribution: Ubuntu
Posts: 284

Original Poster
Rep: Reputation: 30
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
transform month number to month name in php ALInux Programming 1 11-09-2005 10:45 AM
The joke of the week. (Or, maybe, the year?) z-vet General 15 12-04-2004 08:05 PM
Schedule by day of the week stand Linux - General 1 08-30-2004 11:49 PM
Top posters of the month/week masand LQ Suggestions & Feedback 20 08-30-2004 12:07 AM
Starting day of month, month length chrisk5527 Programming 2 03-03-2004 04:03 PM


All times are GMT -5. The time now is 06:32 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration