首页 > 编程 > PHP > 正文

基于PHP文件操作实现页面统计

2020-03-22 16:51:05
字体:
来源:转载
供稿:网友
  • 假如页面统计只需要记录统计数值,不记录其他具体的数据时,这时没有必要使用数据库进行记录,可以直接将统计数值存放到文本文件中。当然,这不是最好的方式,不过这是最简单的方式。
    index.html

    <!DOCTYPE html><html lang='zh-cn'><head>    <meta charset='utf-8'>    <meta content='width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=no' name='viewport'>    <meta content='yes' name='apple-mobile-web-app-capable'>    <meta content='black' name='apple-mobile-web-app-status-bar-style'>    <meta content='telephone=no' name='format-detection'>    <meta content='email=no' name='format-detection'>    <title>statistics</title>    <style>        html, body{            width: 100%;            margin: 0;            padding: 0;            overflow-x: hidden;            overflow-y: auto;            font-family: 'Microsoft Yahei';            text-align: center;        }        .btn{            width: 30%;            height: 30px;            line-height: 30px;            border-radius: 4px;            background-color: #428bca;            font-size: 18px;            display: inline-block;        }    </style></head><body>    <h3>页面统计</h3>    <div class='btn' id='btn_click'>点击</div>    <div class='btn' id='btn_forward'>转发</div>    <script>        var btn_click = document.querySelector('#btn_click'),            btn_forward = document.querySelector('#btn_forward'),            hasTouch = 'ontouchstart' in window;            tapend = hasTouch ? 'touchend' : 'mouseup';        btn_click.addEventListener(tapend, function(){            statistics('click');        }, false);        btn_forward.addEventListener(tapend, function(){            statistics('forward');        }, false);        function statistics(action){            var url = 'http://localhost/statistics/interfaces/statistics.php?action='+action,                script = document.createElement('script');            script.setAttribute('src', url);            document.querySelector('head').appendChild(script);        }    </script></body></html>

    statistics.php

    <?php$action = @$_GET['action'];function statistics($action){    $filename = $action.'_statistics.txt';    if(file_exists($filename)){        $count = file_get_contents($filename);        file_put_contents($filename, ++$count);    }else{        file_put_contents($filename, 1);    }}if($action === 'click'){    statistics('click');}else if($action === 'forward'){    statistics('forward');}

    PHP编程

    郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。

  • 发表评论 共有条评论
    用户名: 密码:
    验证码: 匿名发表