基于PHP文件操作实现页面统计
2015-07-28 11:23
711 查看
假如页面统计只需要记录统计数值,不记录其他具体的数据时,这时没有必要使用数据库进行记录,可以直接将统计数值存放到文本文件中。当然,这不是最好的方式,不过这是最简单的方式。
index.html
statistics.php
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> <h1>页面统计</h1> <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获取中文首字母函数
- php 配置本地自定义域名
- js调用php和php调用js的方法举例
- thinkphp3.1的验证码
- PHPEXCEL扩展类库应用说明
- 看PHP在内部迭代的动作
- thinkphp 学习问题
- PHPEXCEL扩展类库应用说明
- Laravel在XAMPP环境下,render生成分页链接404错误
- PHPEXCEL扩展类库应用说明
- Mac zend studio 12.0.2 破解汉化
- rtpengine源码结构分析及交互流程
- php写入文件,换行符
- Laravel中generate命令不存在
- 命令行DOS下无法使用ping等命令
- php操作文件(读取写入文件)
- 用php实现在文件指定行插入数据的方法
- php 利用socket发送GET,POST请求
- 分享PHP函数实现数字与文字分页代码
- PHP实现简单汉字验证码