Jump to content
php.lv forumi

Unique visitor counter


radikaalz

Recommended Posts

Sveiki!

 

Nezinu kaapeec, bet idejas, kuras ienaak tieshi pirms aizmigshanas parasti meegjinu izmeegjinaat. Nav nekaa sarezhgjiita, bet ceru uz Juusu paliidziibu.

 

 

Kaarteejais unique visitor counter. Datubaaze nav vajadziiga.

 

Pats .php scripts, kuram liidz dodas .txt faili: log.txt, raw.txt, temp.txt counter.txt un tas php scripts jaaincludee savaa lapaa, ko maaku.

 

counter.php

 

 

<?php

extract($HTTP_GET_VARS);
extract($HTTP_POST_VARS);
$ip = getenv(REMOTE_ADDR);
$date = date("d");
$today = date("d:M:Y");
$user = file("counter.txt");
$lis = 0;
$log_file = "log.txt";
$log_lenght = 30;
$max_file_size = 3;
$file_size= filesize($log_file);
$log_size =	$file_size/1024;
if ($action!="stats"){
$qwe = file("raw.txt");
for($b = 0; $b <sizeof($qwe);$b++){
$last = explode("|",$qwe[$b]);
}
$last1file = fopen ("raw.txt", "w");
fwrite ($last1file, $last[0]+1);
fclose ($last1file);
if ($log_size > $max_file_size) {
$filename = "log.txt"; 
$fd = fopen ($filename, "r"); 
$stuff = fread ($fd, filesize($filename)); 
fclose ($fd);
$last_file = fopen ("temp.txt", "a+");
fwrite ($last_file, $stuff);
fclose($last_file);
$fz = fopen ("log.txt", "w");
fwrite ($fz, "");
fclose($fz);
$filename1 = "temp.txt"; 
$fd1 = fopen ($filename1, "r"); 
$stuff1 = fread ($fd1, filesize($filename1)-1024); 
fclose ($fd1);
$fm = fopen ("log.txt", "a+");
fwrite ($fm, $stuff1);
fclose ($fm);
$fy = fopen ("temp.txt", "w");
fwrite ($fy, "");
fclose($fy);
}
for($x=0;$x<sizeof($user);$x++) {
$temp = explode(";",$user[$x]);
$opp[$x] = "$temp[0];$temp[1];$temp[2];";
$such = strstr($temp[0],$ip.".6978521");
if($such) {
$list[$lis] = $opp[$x];
$lis++; 
}
if($temp[1] != $date) {
$fp = fopen ("log.txt", "a+");
$fw = fwrite ($fp, sizeof($user));
$fw = fwrite ($fp, ";");
$fw = fwrite ($fp, $temp[2]);
$fw = fwrite ($fp, ";");
$fw = fwrite ($fp, "$last[0]");
$fw = fwrite ($fp, ";");
$fw = fwrite ($fp, "\n");
fclose ($fp);
$last2file = fopen ("raw.txt", "w");
$mm =  fwrite ($last2file, "1");
fclose ($last2file);
$fq = fopen ("counter.txt", "w");
$fy = fwrite ($fq, $ip);
$fy = fwrite ($fq, ".6978521");
$fy = fwrite ($fq, ";");
$fy = fwrite ($fq, $date);
$fy = fwrite ($fq, ";");
$fy = fwrite ($fq, $today);
$fy = fwrite ($fq, ";");
$fy = fwrite ($fq, "\n");
fclose ($fq);
break;	   
}
}
if(sizeof($list) != "0") {
}else{
$fp = fopen ("counter.txt", "a+");
$fw = fwrite ($fp, $ip);
$fw = fwrite ($fp, ".6978521");
$fw = fwrite ($fp, ";");
$fw = fwrite ($fp, $date);
$fw = fwrite ($fp, ";");
$fw = fwrite ($fp, $today);
$fw = fwrite ($fp, ";");
$fw = fwrite ($fp, "\n");
fclose ($fp);
}
}
if($action == "stats"){
$db_file = "log.txt";
$latest_max = 30;
$lines = file($db_file);
$a = count($lines)-1;
$u = $a - $latest_max;
$unique = file("counter.txt");
$ut = count($unique);
$file_size= filesize("log.txt");
$log_size =	$file_size/1024;
$raw = "raw.txt"; 
$fn = fopen ($raw, "r"); 
$puff = fread ($fn, filesize($raw)); 
fclose ($fn);
echo "<font face=arial size=2>Unique Hits Today: <B>$ut</B><BR> Raw Hits Today: <B>$puff </B></font><HR color=#CCCCCC>";
?>

<STYLE type=text/css>
TD {
COLOR: #000000; FONT-FAMILY: Verdana, Helvetica, Arial; FONT-SIZE: 13px
}
</STYLE>
<font face=arial size=2><U><B>Statistics of last 30 days</B></U> (Days with 0 visits are not shown)<table border=1 cellspacing=0 bordercolor=#00000 width=50% bgcolor=#AFC6DB><td border=1 width=15%><B>Day</B></td><td width=15%  border=1><B>Unique Visits</B></td><td border=1 width=15%><B>Raw Visits</B></td>
<?php
for($i = $a; $i >= $u ;$i--){
$temp = explode(";",$lines[$i]);
echo "<tr><td border=1 width=15%>$temp[1]</td><td width=15% border=1>$temp[0]</td><td width=15% border=1>$temp[2]</td>";
}
?>
</table></font>
<?php
}
?> 

 

 

 

Meegjinu kaut kaa dabuut, ka ieejot piemeeram hxtp://mysite.com/counter tiek spiesti dati no hxtp://mysite/counter/index.php?action=stats uzreiz pa taisno. Tas ir, nevajag spiest veel vienu linku, lai apskatiitu apmekleejumu statistiku. ieejot hxtp://mysite.com/counter vareetu izsaktiities shaadi:

 

 

 

8hzgirm6rkwqcmeadhsv.jpg

 

 

 

Kaads var pateikt, kaa man dabuut statistiku jau pashaa index failaa, jo scripa read-me ir teikts tikai includeet counter.php

paldies

Link to comment
Share on other sites

spoiler ftw ;)

 

 

 

echo '<div style="font: 20px Arial, sans-serif; color: #000; text-align: center; border: 3px solid #e50909; -moz-border-radius: 10px; padding: 10px">' .
'Today this site has been visited by<br />' . count(file('counter.txt')) . '<br />glad and happy unique visitors.</div>';

unq_hit_1263228243.png

 

 

Link to comment
Share on other sites

Sry ka paceļu vecu topiku, bet atkal sakarā ar šo pašu php scriptu.

 

Scripts ir palabots.

 

 

 

<?php
       ini_set('date.timezone', 'Europe/Riga');

       $ip = $_SERVER['REMOTE_ADDR']; 
       $date = date("d");
       $today = date("d:M:Y");
       $users = file("counter.txt");   // parsaucu no user par users, lai velak nodemonstretu nelielu magiju;
       $lis = 0;
       $log_file = "log.txt";
       $log_lenght = 30;
       $max_file_size = 3;
       $file_size= filesize($log_file);
       $log_size =     $file_size/1024;
       if ($_GET['action'] != "stats"){
               $qwe = file("raw.txt");
               $last = end($qwe);
               $last = explode("|", $last);
               $last1file = fopen ("raw.txt", "w"); // lai ari kas tur netiktu saglabats, šadi ir isak un atrak. ;-)
               fwrite ($last1file, $last[0]+1);
               fclose ($last1file);
               if ($log_size > $max_file_size) {
                       $filename = "log.txt"; 
                       $fd = fopen ($filename, "r"); 
                       $stuff = fread ($fd, filesize($filename)-1024); // pec faila aizveršanas mainigaja paliek vertiba, ko tu taja esi ielasijis, tadel nevajag veidot otro failu un cakareties ar to;
                       fclose ($fd);
                       $fm = fopen ("log.txt", "w");
                       fwrite ($fm, $stuff);
                       fclose ($fm);
               }
               $list = array(); // pirms cikliem un if`iem ir labi pateikt, ka mainigais bus masivs, ja to izmanto ari kaut kur talak koda, savadak velak var tikt mestas kludas;
               foreach($users as $user){
                       $temp = explode(";",$user);

                       $opp = $temp[0] . ';' . $temp[1] . ';' . $temp[2] . ';'; // nav jegas lieki piedirst atminu, liekot datus masiva, ja mes talak koda izmantojam tikai ta pašreizejo elementu;
                       if(strpos($temp[0],$ip.".6978521")){ // tev vajag noskaidrot tikai vai teksta ir apakšteksts, tadel 
                               $list[] = $opp; // pieškiršana bez elementa skaititaja automatiski palielina masiva skaititaju par 1 un ieliek jaunaja vieta vertibu;
                               // gadijuma te nevareja uzreiz likt iekša $user? Tada gadijuma mes varetu iztikt bez salimešanas atpakal;
                       }
                       if($temp[1] != $date) {
                               $fp = fopen ("log.txt", "a+");
                               $textLine = count($users) . ';' . $temp[2] . ';' . $last[0] . ";\n";
                               $fw = fwrite ($fp, $textLine);
                               fclose ($fp);
                               $last2file = fopen ("raw.txt", "w");
                               $mm =  fwrite ($last2file, "1");
                               fclose ($last2file);
                               $fq = fopen ("counter.txt", "w");
                               $textLine = $ip . '.6978521;' . $date . ';' . $today . ";\n"; 
                               $fy = fwrite ($fq, $textLine);
                               fclose ($fq);
                               break;     
                       }
               }
               if(count($list) == "0") { 
                       $fp = fopen ("counter.txt", "a+");
                       $textLine = $ip . '.6978521;' . $date . ';' . $today . ";\n"; 
                       $fw = fwrite ($fp, $textLine);
                       fclose ($fp);
               }
       }
       if($action == "stats"){
               $db_file = "log.txt";
               $latest_max = 30;
               $lines = file($db_file);
               $a = count($lines)-1;
               $u = $a - $latest_max;

               // te nak videja apmeklejuma skaitišana, ja es pareizi esmu sapratis, ka apmeklejuma dati tev glabajas faila log.txt
               $maxHits = $maxHosts = 0; // sakuma maksimums bus nulle; šadi 2 mainigajiem pieškir vienu un to pašu vertibu (slinka metode, lai nav jaraksta 2 rindas);
               $sumHits = $sumHosts = 0; // šajos mainigajos vienkarši summesim kopa visus;
               foreach($lines as $line){
                       $items = explode(';', $line);
                       $sumHosts += (int)$items[0]; // vienkarši pieskaitam klat; ar (int) mes pasakam, kodam, ka mes gribam redzet integer tipa mainigo
                       $sumHits += (int)$items[2]; // vienkarši pieskaitam klat;
                       if((int)$items[0] > $maxHosts){ // ja esam atraduši lielako, tad atceramies to;
                               $maxHosts = (int)$items[0];
                       }
                       if((int)$items[2] > $maxHits){ // ja esam atraduši lielako starp hostiem, tad atceramies to;
                               $maxHits = (int)$items[2];
                       }
               }
               $avgHosts = round(($sumHosts / $a), 2);
               $avgHits = round(($sumHits / $a), 2);

               // tagad druka lauka $maxHosts, $maxHits, $avgHosts un $avgHits, kur vien velies, jo tajos ir visi tev vajadzigie dati.

               $unique = file("counter.txt");
               $ut = count($unique);
               $file_size= filesize("log.txt");
               $log_size =     $file_size/1024;
               $raw = "raw.txt"; 
               $fn = fopen ($raw, "r"); 
               $puff = fread ($fn, filesize($raw)); 
               fclose ($fn);
               echo "<font face=arial size=2>Šodienas unikalie apmeklejumi: <B>" . $ut . "</B><BR> Šodien atkartotie apmeklejumi: <B>" . $puff . "</B></font><HR color=#CCCCCC>";
?>

<STYLE type=text/css>
       TD {
               COLOR: #000000; FONT-FAMILY: Verdana, Helvetica, Arial; FONT-SIZE: 13px
       }
</STYLE>
<font face=arial size=2><U><B>Statistika par pedejam 30 dienam </B></U><table border=1 cellspacing=0 bordercolor=#00000 width=50% bgcolor=#AFC6DB><td border=1 width=15%><B>Diena</B></td><td width=15%  border=1><B>Unikalie apmeklejumi</B></td><td border=1 width=15%><B>Atkartotie apmeklejumi</B></td>
<?php
       for($i = $a; $i >= $u ;$i--){
               $temp = explode(";",$lines[$i]);
               echo "<tr><td border=1 width=15%>" . $temp[1] . "</td><td width=15% border=1>" . $temp[0] . "</td><td width=15% border=1>" . $temp[2] . "</td>";
       }
?>
</table></font>
<?php
}
?> 

 

 

 

Man bija vajadzīgs lai es varu dabūt ārā vidējo apmeklējumu skaitu un maksimālo sasniegto vienas dienas laikā.

 

Domāju, ka viss ir scriptam kā vajag, tikai 1 maza problēma.

 

tad kad es ievadu http://blablabla.com/counter.php?action=stats vispār tukša bilde, bet vajadzētu būt līdzīgai tādai tabulai:

 

 

 

rmba2zrqkuz2rbh4qyb.png

 

 

 

kodulasīju un pētiju, bet tik un tā nevaru saprast, kāpēc tabula nerādās, jo PHP zināšanu nav nekādu lielo. Kāds var palīdzēt?

 

paldies

Edited by radikaalz
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...