zoukankan      html  css  js  c++  java
  • 网站访问量统计

    网站访问量的详细分析包括网站总的访问量、总的IP访问量、网站当月访问量、网站当月IP访问量、网站当日访问量、网站当日IP访问量和网站指定月份的访问量分析图

    这里两个数据库的使用没有太大的区别,只要将其中使用的MySQL数据库函数替换为SQL Server数据库函数即可。

    下面仍然以使用MySQL数据库的程序为例进行介绍。网站访问量统计分析的运行结果如图1所示。

    首先,连接数据库,调用指定的数据库连接文件。以及统计代码实现

    <?php 
      $id=@mysql_connect('localhost','root','');  //连接数据库服务器
      mysql_select_db('db_counter',$id);        //连接db_counter数据库
      mysql_query("set names gb2312");        //指定数据库中字符的编码格式
      // session_start(); 
      if(!isset($_SESSION)){session_start();}
      $data1=date("Y-m-d");       //获取当前访问时间
      $data2=substr(date("Y-m-d"),0,7);
      $ip=getenv('REMOTE_ADDR');
      $_SESSION['temp']=1;       //登录以后,$_SESSION[temp]的值不为空,给$_SESSION[temp]赋一个值1
      if(isset($_SESSION['temp'])){     //判断$_SESSION[temp]==""的值是否为空,其中的temp为自定义的变量
      // 统计来访ip并将ip写入文件
        $fp=@fopen("count.txt", "r");
        $count=@fgets($fp,1024);
        $count=$ip.'/';
        $fp=fopen("count.txt", "w");
        fputs($fp,$count);
        fclose($fp);
      //使用数据库存储数据
        $select=mysql_query("select * from tb_count10 where data1='$data1' and ip='$ip'",$id);
        $res=mysql_num_rows($select);
        if($res>0){
          $query1="update tb_count10 set counts=counts+1 where data1='$data1' and ip='$ip'";  
          $result1=mysql_query($query1);
        }else{
          $query="insert into tb_count10(counts,data1,data2,ip)values('1','$data1','$data2','$ip')";
              $result=mysql_query($query,$id); 
        }
      }
    ?>
    

    然后,输出网站总的访问量以及总的IP访问量。步骤如下。

    (1)应用SELECT语句和sum()函数,从数据表中读取counts字段总的数据,获取网站总的访问量。

    <?php $query_1="select sum(counts) as countes from tb_count10 ";//查询数据库中总的访问量
    $result_1=mysql_query($query_1);
    $countes_1=mysql_result($result_1,0,'countes');
    echo "<p class='STYLE1'>网站总的访问量:$countes_1</p>";
    

    (2)获取网站总的IP访问量。从数据表中读取数据,应用mysql_fetch_array()函数获取ip字段对应的IP地址,并将获取到的值赋给一个数组,通过array_unique()函数去除数组中重复的值,最终获取到网站总的IP访问量。代码如下:

    //查询数据库中总的IP访问量
    $query_2="select * from tb_count10 ";
    $result_2=mysql_query($query_2);
    while($myrow=mysql_fetch_array($result_2)){
      $counts_2[]=$myrow[ip];                        //将获取的IP的值赋给变量
    }
    $results_2=array_unique($counts_2);                    //去除数组中重复的值
    $countes_2=count($results_2);                        //获取数组中值的数量,即总的IP访问量
    echo "<p class='STYLE1'>网站总的IP访问量:$countes_2</p>";
    ?> 
    

    接着,获取网站当月的访问量和当月IP的访问量。使用的方法与获取网站总的访问量和总的IP访问量的方法相同。区别之处是SELECT语句中增加了一个查询条件data2='".substr (date("Y-m-d"),0,7)."',即查询数据库中符合当前月份的数据。代码如下:

    <?php 
    //查询数据库中当月总的访问量
    $query_3="select sum(counts) as countes from tb_count10 where data2='".substr(date("Y-m-d"),0,7)."'";
    $result_3=mysql_query($query_3);
    $countes_3=mysql_result($result_3,0,'countes');
    echo "<p class='STYLE2'>网站当月的访问量:$countes_3</p>";
    //查询数据库中当月的IP访问量
    $query_4="select * from tb_count10 where data2='".substr(date("Y-m-d"),0,7)."'";
    $result_4=mysql_query($query_4);
    $counts_4=array();
    while($myrow_4=mysql_fetch_array($result_4)){
    $counts_4[]=$myrow_4[ip];                          //将获取的IP的值赋给变量
    }
    $results_4=array_unique($counts_4);                       //去除数组中重复的值
        // 统计来访ip并写入文件
        foreach($results_4 as $key=>$value){
          $fp=@fopen("$data2".".txt", "r");
          $count=@fgets($fp,1024);
          $count=$value.'/';
          $fp=fopen("$data2".".txt", "w");
          fputs($fp,$count);
          fclose($fp);      
        }
    $countes_4=count($results_4);                        //获取数组中值的数量,即总的IP访问量
    echo "<p class='STYLE2'>网站当月IP的访问量:$countes_4</p>";
    ?>
    

    获取网站当日的访问量和当日的IP访问量。其使用的方法这里不再赘述,指定的查询条件是data1='".date("Y-m-d")."'。程序关键代码如下:

    <?php 
    //查询数据库中当日总的访问量
    $query_5="select sum(counts) as countes from tb_count10 where data1='".date("Y-m-d")."' ";
    $result_5=mysql_query($query_5);
    $countes_5=mysql_result($result_5,0,'countes');
    echo "<p class='STYLE3'>网站当日的访问量:$countes_5</p>";
    //查询数据库中当日的IP访问量
    $query_6="select * from tb_count10 where data1='".date("Y-m-d")."'";
    $result_6=mysql_query($query_6);
    $counts_6=array();
    while($myrow_6=mysql_fetch_array($result_6)){
      $counts_6[]=$myrow_6[‘ip’];                       //将获取的IP的值赋给变量
    }
    if(is_array($counts_6)){
      $results_6=array_unique($counts_6);                //去除数组中重复的值
        // 统计来访ip并写入文件
        foreach($results_6 as $key=>$value){
          $fp=@fopen("$data1".".txt", "r");
          $count=@fgets($fp,1024);
          $count=$value.'/';
          $fp=fopen("$data1".".txt", "w");
          fputs($fp,$count);
          fclose($fp);      
        }
      $countes_6=count($results_6);                 //获取数组中值的数量,即总的IP访问量
      echo "<p class='STYLE3'>网站当日IP的访问量:$countes_6</p>";
    }
    ?>
    

    最后,创建一个form表单,实现对指定月份的网站访问量进行折线图分析。步骤如下。

    (1)创建一个form表单,用于提交月份信息。将数据提交到本页,设置变量的名称为select。代码如下:

    <form name="form1" method="post" action="index.php">
        <select name="select">
      <?php 
        $que="select distinct data2 from tb_count10";
        $res=mysql_query($que,$id);
        while($myrow=mysql_fetch_array($res)){
        ?>
            <option value="<?php echo $myrow['data2'];?>"><?php echo $myrow['data2'];?></option>
            <?php 
        }
            ?>
        </select>  
        <input type="submit" name="Submit" value="提交">
    </form>
    

    2)编写PHP语句,从数据库中读取出指定月份内网站访问量的数据,并且对该数据以逗号(“,”)进行分隔,存储到变量中。代码如下:

    <?php
    if(isset($_POST['select'])){            //判断提交的数据是否为真
        $query="select counts,data1 from tb_count10 where data2='".$_POST['select']."' order by data1 ";    //定义SQL语句
        $result=mysql_query($query);        //执行查询语句
        $results=array();                    //定义数组
        $datas=array();                        //定义数组
        while($myrow=mysql_fetch_array($result)){    //循环输出查询结果
            $results[]=$myrow['counts'];            //将查询结果存储到数组中
            $lmbs=implode(",",$results);            //将数组重新生成一个字符串
            $datas[]=substr($myrow['data1'],8,2);    //截取时间数据,并存储到数组中
            $das=implode(",",$datas);                //将数组重新生成一个字符串
        }
    ?>

    (3)接着,插入一个图片,指定图片的路径为stat.php文件,设置链接标识为从数据库中读取出的指定月份内网站访问量的数据($lmbs)。代码如下:

    <table width="902" border="1" cellpadding="1" cellspacing="1" bordercolor="#FFFFFF" bgcolor="#D5D5D5">
        <tr>
            <td height="18" background="images/mysql_9.jpg" bgcolor="#FFFFFF" class="STYLE4">      <?php echo $_POST['select'];?>月网站访问量走势图</td>
        </tr>
        <tr>
            <td bgcolor="#FFFFFF">
                <p><img src="stat.php?lmbs=<?php echo $lmbs; ?>&das=<?php echo $das;?>"/></p>
                <p align="center" class="STYLE2"><?php echo $_POST['select'];?>月网站访问量走势图</p>    
            </td>
          </tr>
    </table>
    <?php
    }
    ?>
    

    树状图生成用Jpgraph,只要了解它的一些内置函数,可以轻松得画出折线图、柱形图、饼状图等图表。

    1.首先要保证PHP打开了Gd2的扩展:

    2.打开PHP.ini,定位到extension=php_gd2.dll,把前面的分号删掉。

    3.然后下载Jpgraph,download.chinaunix.net/download.php?id=35443&ResourceID=5098,解压到一个文件夹中。如 E:Softwarewebwwwjpgraph。

    4.最后附上生成代码

    <?php
    include ("src/jpgraph.php");                        //载入类库文件
    include ("src/jpgraph_bar.php");
    $lmbs=$_GET['lmbs'];                                //获取超级链接传递的参数值
    $das=$_GET['das'];
    $datay=explode(",",$lmbs);                                 //按照","分隔字符串 $data为获取的数据变量
    $datax=explode(",",$das);                                 //按照","分隔字符串 $data为获取的数据变量
    $graph = new Graph(800,200,"auto");        //创建图像    
    $graph->img->SetMargin(60,20,30,50);    //设置图像边框间距
    $graph->SetScale("textlin");            //定义坐标刻度类型
    $graph->SetMarginColor("lightblue");    //定义图像颜色
    $graph->title->Set('counter');        //定义标题
    $graph->title->SetFont(FF_SIMSUN, FS_BOLD);     //设置标题字体
    $graph->xaxis->SetFont(FF_VERDANA,FS_NORMAL,10);    //设置X轴的字体
    $graph->yaxis->SetFont(FF_VERDANA,FS_NORMAL,10);    //设置Y轴的字体
    $graph->xaxis->SetTickLabels($datax);        //设置X轴输出的数据
    $graph->xaxis->SetLabelAngle(50);            //设置输出文字大小
    $bplot = new BarPlot($datay);        //实例化图像创建类
    $bplot->SetWidth(0.3);            //设置柱形图的输出大小
    $bplot->SetFillGradient("navy","#FFFF00",GRAD_LEFT_REFLECTION);    //设置图像的类型和填充颜色
    $bplot->SetColor("white");    //设置图像边框颜色
    $graph->Add($bplot);        //添加数据
    $graph->Stroke();            //生成图像
    

    源代码打包:网站统计代码

    报错Notice: Undefined variable: 的解决方法

  • 相关阅读:
    iOS UITableView的cell重用标识
    iOS SDWebImage清理缓存数据
    iOS UITextView 根据输入text自适应高度
    iOS 网络请求 NSURLSession 的上传文件方法
    iOS开发之tintColor属性详解
    iOS SDWEBImage和collectionView的组合,以及collectionView的随意间距设置
    iOS9 Xcode7 设置Launch Image 启动图片
    iOS
    iOS 浅谈AFNetworking网络请求
    贝塞尔曲线
  • 原文地址:https://www.cnblogs.com/aten/p/8687832.html
Copyright © 2011-2022 走看看