我们要统计在一段时间内访问站点的人数,有多种解决方案,你可以使用cookie,session结合文本或者数据库来记录用户访问数。本文将使用PHP,结合Mysql以及jQuery,展示一个统计在线人数以及访客地区分布的示例。
通常,当访客访问网站时,页面记录用户的cookie信息,当cookie过期即认为用户不在线。本文中我们使用PHP记录访客IP,并在客户端记录cookie及过期时间,同时通过新浪IP地址接口,获取访客的地理位置(本例只记录省份),一并写入mysql表中,即可统计一段时间内的访客总数,也可以查看访客的地区分布。
HTML
我们在页面上放置一个显示当前在线人数的div#total以及一个用于展示访客地区分布的列表#onlinelist,默认我们在列表中放置一张与加载动画图片,后面我们用jQuery控制当鼠标滑向时展示详细列表。
代码如下 | 复制代码 |
当前在线:
|
CSS
我们用CSS来渲染显示效果,为了就是不让我们的示例很难看,下面的代码中,我们使用了CSS3,时代在进步啊,所以建议使用现代浏览器预览效果。
.
代码如下 | 复制代码 |
demo{width:150px; margin:20px auto; font-size:14px} #total{padding:6px 10px; background:#090 url(http://pic1.phprm.com/2013/05/02/arr.jpg) no-repeat right top; color:#fff; cursor:pointer; -moz-border-radius:5px; -webkit-border-radius:5px; border-radius:5px; -moz-box-shadow:0 0 3px #ccc; -webkit-box-shadow:0 0 3px #ccc;box-shadow:0 0 3px #ccc;} #onlinelist{background:#f7f7f7; border:1px solid #d3d3d3; display:none; -moz-border-radius:5px; -webkit-border-radius:5px; border-radius:5px; -moz-box-shadow:0 0 3px #ccc; -webkit-box-shadow:0 0 3px #ccc;box-shadow:0 0 3px #ccc;} #onlinelist li{height:20px; line-height:20px;padding:4px 6px;border-bottom:1px dotted #d9d9d9} #onlinelist li span{float:right} #onlinelist li:hover{background:#fff} |
Mysql
我们要准备一张数据表online,用来记录访客IP、地区及访问时间。整个示例统计过程都依赖这张表,其结构如下:
代码如下 | 复制代码 |
CREATE TABLE IF NOT EXISTS `online` (
|
PHP
online.php用来记录访客信息,包括IP地址和地区。首先检测数据表中是否有访客IP记录,如果有,则只更新访问时间,否则,获取用户省份区域,并将用户IP即省份区域插入到表中。在此,可以判断是否存在访客的cookie记录,如果不存在则向新浪IP地址库请求获取访客的区域信息,并设置cookie值和过期时间。最后,我们删除表中已经过期的记录,统计总记录数并输出,详细请看代码注释。
代码如下 | 复制代码 |
include_once('connect.php'); //连接数据库
|
关于新浪IP地址库的调用,您也可以参阅helloweba.com文章根据IP定位用户所在城市信息的介绍。
函数get_client_ip()用来获取用户真实IP。
代码如下 | 复制代码 |
function get_client_ip() {
|
geo.php用来统计各省份(区域)访客人数分布。通过查询数据库,并按省份分组排序即可,注意我们将最终的数据集以JSON的形式输出,便于前端ajax交互。
代码如下 | 复制代码 |
include_once('connect.php');//连接数据库
|
jQuery
前端页面需要做的是,页面加载时展示访客总数,即使用ajax请求online.php即可。然后当鼠标滑向统计箭头时,通过ajax请求geo.php获取各区域省份的在线人数,并以下拉的方式展现效果。
代码如下 | 复制代码 |
$(function(){ }); $("#onlinelist").html(str); }); },function(){ $("#onlinelist").slideUp("fast"); }); }); |
完整实例
代码如下 | 复制代码 |
|
文章链接:
随便收藏,请保留本文地址!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6
Visual web development tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.