Xin Chào, Bạn đang muốn tạo mục thống kê lượt truy cập website của bạn, hôm nay blog sẽ chia sẻ cho bạn một code thống kê đơn giản lượt truy cập cũng như lượt online trên website của bạn.
Giới thiệu
Đây là các hàm thống kê, đếm số lượt truy cập theo IP:
Bước 1 : Bạn tạo một file thongke.php. Và coppy - paste code phía dưới  vào - save lại.
<?phpsession_start();functiononline(){$rip=$_SERVER['REMOTE_ADDR'];$sd= time();$count= 1;$maxu= 1;$file1="counter/online.log";$lines= file($file1);$line2="";foreach($linesas$line_num=>$line){if($line_num== 0){$maxu=$line;}else{$fp=strpos($line,'****');$nam=substr($line,0,$fp);$sp=strpos($line,'++++');$val=substr($line,$fp+4,$sp-($fp+4));$diff=$sd-$val;if($diff< 300 &&$nam!=$rip){$count=$count+1;$line2=$line2.$line;}}}$my=$rip."****".$sd."++++\n";if($count>$maxu)$maxu=$count;$open1=fopen($file1,"w");fwrite($open1,"$maxu\n");fwrite($open1,"$line2");fwrite($open1,"$my");fclose($open1);$count=$count;$maxu=$maxu+200;return$count;}///////////////////////$ip=$_SERVER['REMOTE_ADDR'];$file_ip=fopen('counter/ip.txt','rb');while(!feof($file_ip))$line[]=fgets($file_ip,1024);for($i=0;$i<(count($line));$i++) {list($ip_x) = split("\n",$line[$i]);if($ip==$ip_x) {$found= 1;}}fclose($file_ip);if(!($found==1)) {$file_ip2=fopen('counter/ip.txt','ab');$line="$ip\n";fwrite($file_ip2,$line,strlen($line));$file_count=fopen('counter/count.txt','rb');$data='';while(!feof($file_count))$data.=fread($file_count, 4096);fclose($file_count);list($today,$yesterday,$total,$date,$days) = split("%",$data);if($date==date("Y m d"))$today++;else{$yesterday=$today;$today= 1;$days++;$date=date("Y m d");}$total++;$line="$today%$yesterday%$total%$date%$days";$file_count2=fopen('counter/count.txt','wb');fwrite($file_count2,$line,strlen($line));fclose($file_count2);fclose($file_ip2);}functiontoday(){$file_count=fopen('counter/count.txt','rb');$data='';while(!feof($file_count))$data.=fread($file_count, 4096);fclose($file_count);list($today,$yesterday,$total,$date,$days) = split("%",$data);return$today;}functionyesterday(){$file_count=fopen('counter/count.txt','rb');$data='';while(!feof($file_count))$data.=fread($file_count, 4096);fclose($file_count);list($today,$yesterday,$total,$date,$days) = split("%",$data);return$yesterday;}functiontotal(){$file_count=fopen('counter/count.txt','rb');$data='';while(!feof($file_count))$data.=fread($file_count, 4096);fclose($file_count);list($today,$yesterday,$total,$date,$days) = split("%",$data);echo$total;}functionavg(){$file_count=fopen('counter/count.txt','rb');$data='';while(!feof($file_count))$data.=fread($file_count, 4096);fclose($file_count);list($today,$yesterday,$total,$date,$days) = split("%",$data);echoceil($total/$days);}?>
Bước 2 : Tạo thư mục counter.
Bước 3 : Trong thư mục counter bạn tạo thêm 3 file sau : count.txt, ip.txt, online.log
Tiếp theo là code hiển thị :
Đang online: <?phpechoonline(); ?> <br>Truy cập hôm nay: <?phpechotoday(); ?> <br>Truy cập hôm qua: <?phpechoyesterday(); ?> <br>Tổng số truy cập: <?php total(); ?> <br>Truy cập trung bình: <?php avg(); ?> <br>
Chúc Bạn Thành Công !


Leave a comment: