写在前面得话

很多人有那样的要求,便是统计分析网页访问IP及其线上的总数。今日大家就看一下实际完成方式 。

打开依赖涵数控制模块

完成这一作用,必须依赖putenv()涵数。下边二种方法均可。

变更php.ini文件方式

找到php.ini文件,检索putenv关键词,删掉就可以。

isable_functions = passthru,exec,system,putenv,chroot,chgrp,chown,shell_exec,popen,proc_open,pcntl_exec,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,imap_open,apache_setenv

应用宝塔面板

点一下左边应用管理,找到php,随后设定->禁止使用涵数。

php实现统计IP数及在线人数的示例代码 在线人数 统计IP数 php PHP编程  第1张

删掉putenv,随后重新启动php就可以。

完成涵数

在count.php同文件目录下创建文件:count,temp,online。在建文本文件count.txt,除掉后缀名即是count了;

linux系统软件中请设定文件名后缀为:777。

文档count.php代码,采用了php函数--explode、isset、empty及sprintf等:

<?php
$file       = "count"; // 计数文件目录
$startno     = "1000";  // 起止标值
$tempfile     = "temp"; 
$t_now  = time();
$t_array = getdate($t_now);
$day   = $t_array['mday'];
$mon   = $t_array['mon'];
$year  = $t_array['year'];
if (file_exists("$file")) {
    $count_info=file("$file");
    $c_info = explode(",", $count_info[0]);
    $total_c=$c_info[0];
    $yesterday_c=$c_info[1];
    $today_c=$c_info[2];
    $lastday=$c_info[3];
} else {
    $total_c="$startno";
    $yesterday_c="0";
    $today_c="0";
    $lastday="0";
}
if ( !isset($HTTP_COOKIE_VARS["countcookie"]) || $HTTP_COOKIE_VARS["countcookie"] != $day) {
    $your_c=1;
    $lockfile=fopen("temp","a");
    flock($lockfile,3);
    putenv('TZ=JST-9');
    $t_array2 = getdate($t_now-24*3600);
    $day2=$t_array2['mday'];
    $mon2=$t_array2['mon'];
    $year2=$t_array2['year'];
    $today = "$year-$mon-$day";
    $yesterday = "$year2-$mon2-$day2";
    if ($today != $lastday) {
           if ($yesterday != $lastday) $yesterday_c = "0";
               else $yesterday_c = $today_c;
        $today_c = 0;
        $lastday = $today;
    }
    $total_c  ;
    $today_c  ;
    $total_c   = sprintf("d", $total_c);
    $today_c   = sprintf("d", $today_c);
    $yesterday_c = sprintf("d", $yesterday_c);
    setcookie("countcookie","$day",$t_now 43200);
    $fp=fopen("$file","w");
    fputs($fp, "$total_c,$yesterday_c,$today_c,$lastday");
    fclose($fp);
    fclose($lockfile);
}
if ( empty( $your_c ) ) $your_c = 1;
setcookie("yourcount",$your_c 1,$t_now 43200);
$your_c = sprintf("d", $your_c);
//////////////////////////刚开始统计分析线上
$filename="online";
$onlinetime=600; //同一IP在线的时间,企业:秒
$online_id=file($filename);
$total_online=count($online_id);
$ip=getenv("REMOTE_ADDR");
$nowtime=time();
 for($i=0;$i<$total_online;$i  ){
     $oldip=explode("||",$online_id[$i]);
     $hasonlinetime=$nowtime-$oldip[0];
 if($hasonlinetime<$onlinetime and $ip!=$oldip[1]) $nowonline[]=$online_id[$i];
                 }
     $nowonline[]=$nowtime."||".$ip."||";
     $total_online=count($nowonline);
     $fp=fopen($filename,"w");
     rewind($fp);
     for($i=0;$i<$total_online;$i  ){
     fputs($fp,$nowonline[$i]);
     fputs($fp,"n");
                 }
 fclose($fp);
   if($total_online==0)$total_online=1;
        $total_online = sprintf("d", $total_online);
///////////////////////////////////////////////////////
echo "document.write("·总IP浏览:".$total_c."");";
echo "document.write("<br>");";
echo "document.write("·昨天浏览:".$yesterday_c."");";
echo "document.write("<br>");";
echo "document.write("今天IP:".$today_c."");";
echo "document.write("&nbsp;");";
echo "document.write("·您 访 问:".$your_c."");";
echo "document.write("<br>");";
echo "document.write("当前在线:".$total_online."");";
exit;
?>

php实现统计IP数及在线人数的示例代码 在线人数 统计IP数 php PHP编程  第2张

启用

用JS启用文档count.php

在必须添加统计分析的的地区,加上:

<script src="/php/count.php"></script>

到此这篇有关php完成统计分析IP数及线上总数的实例编码的文章内容就详细介绍到这了,大量有关php 统计分析IP数及线上总数內容请搜索之前的文章内容或再次访问下边的类似文章期待大伙儿之后多多的适用!