这里我做了一个统计插件,可以统计网站每日访问量多少,可以查看访问ip、访问地区、访问时间。
下面我介绍一下如何从零制作出一款插件
首先做一下插件的前台页面,在plus目录下创建一个tongji.php文件
tongji.php代码如下:
<?php
require_once(dirname(__FILE__)."/../include/common.inc.php");
$ip = getIp();
$diqu = findCityByIp($ip);
$time = time();
$row = $dsql->GetOne("Select * From dede_tongji where ip = '$ip'");
if(!$row){
$sql = "INSERT INTO `dede_tongji`(ip,diqu,time)
VALUES('$ip','$diqu',".$time.");";//插入记录数据库
$dsql->ExecuteNoneQuery($sql);//执行SQL操作
}else{
$upquery = "Update dede_tongji set time=".$time;
$dsql->ExecuteNoneQuery($upquery);
}
//获取访客ip
function getIp()
{
$ip=false;
if(!empty($_SERVER["HTTP_CLIENT_IP"])){
$ip = $_SERVER["HTTP_CLIENT_IP"];
}
if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ips = explode (", ", $_SERVER['HTTP_X_FORWARDED_FOR']);
if ($ip) { array_unshift($ips, $ip); $ip = FALSE; }
for ($i = 0; $i < count($ips); $i++) {
if (!eregi ("^(10│172.16│192.168).", $ips[$i])) {
$ip = $ips[$i];
break;
}
}
}
return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);
}
//根据ip获取城市、网络运营商等信息
function findCityByIp($ip){
// http://ip.taobao.com/ipSearch?ipAddr=220.177.141.155
// return file_get_contents("http://int.dpool.sina.com.cn/iplookup/iplookup.php?ip=".$ip);
$data = file_get_contents('http://api.map.baidu.com/location/ip?ak=ia6HfFL660Bvh43exmH9LrI6&ip='.$ip);
$res = json_decode($data,true);
if($res['status'] == 0){
return $res['content']['address'];
}else{
return '';
}
}
//获取用户浏览器类型
function getBrowser(){
$agent=$_SERVER["HTTP_USER_AGENT"];
if(strpos($agent,'MSIE')!==false || strpos($agent,'rv:11.0')) //ie11判断
return "ie";
else if(strpos($agent,'Firefox')!==false)
return "firefox";
else if(strpos($agent,'Chrome')!==false)
return "chrome";
else if(strpos($agent,'Opera')!==false)
return 'opera';
else if((strpos($agent,'Chrome')==false)&&strpos($agent,'Safari')!==false)
return 'safari';
else
return 'unknown';
}
//获取网站来源
function getFromPage(){
if(isset($_SERVER['HTTP_REFERER'])){
return $_SERVER['HTTP_REFERER'];
}else{
return '';
}
}
//获取渠道
function getQudao(){
if(!isset($_SERVER['HTTP_REFERER'])){
return '';
}
if(strpos($_SERVER['HTTP_REFERER'],"www.baidu.com")> -1){
return '百度';
}
if(strpos($_SERVER['HTTP_REFERER'],"www.google.com")> -1){
return '谷歌';
}
if(strpos($_SERVER['HTTP_REFERER'],"www.so.com")> -1){
return '360搜索';
}
if(strpos($_SERVER['HTTP_REFERER'],"www.sogou.com")> -1){
return '搜狗搜索';
}
if(strpos($_SERVER['HTTP_REFERER'],"cn.bing.com")> -1){
return '必应搜索';
}
return '';
}
//获取当前页网址
function getCurrentPage()
{
$protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || $_SERVER['SERVER_PORT'] == 443) ? "https://": "http://";//协议头
$url = $protocol . $_SERVER['HTTP_HOST']. $_SERVER['REQUEST_URI'];//完整的url地址
return $url;
}
require_once(dirname(__FILE__)."/../include/common.inc.php");
$ip = getIp();
$diqu = findCityByIp($ip);
$time = time();
$row = $dsql->GetOne("Select * From dede_tongji where ip = '$ip'");
if(!$row){
$sql = "INSERT INTO `dede_tongji`(ip,diqu,time)
VALUES('$ip','$diqu',".$time.");";//插入记录数据库
$dsql->ExecuteNoneQuery($sql);//执行SQL操作
}else{
$upquery = "Update dede_tongji set time=".$time;
$dsql->ExecuteNoneQuery($upquery);
}
//获取访客ip
function getIp()
{
$ip=false;
if(!empty($_SERVER["HTTP_CLIENT_IP"])){
$ip = $_SERVER["HTTP_CLIENT_IP"];
}
if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ips = explode (", ", $_SERVER['HTTP_X_FORWARDED_FOR']);
if ($ip) { array_unshift($ips, $ip); $ip = FALSE; }
for ($i = 0; $i < count($ips); $i++) {
if (!eregi ("^(10│172.16│192.168).", $ips[$i])) {
$ip = $ips[$i];
break;
}
}
}
return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);
}
//根据ip获取城市、网络运营商等信息
function findCityByIp($ip){
// http://ip.taobao.com/ipSearch?ipAddr=220.177.141.155
// return file_get_contents("http://int.dpool.sina.com.cn/iplookup/iplookup.php?ip=".$ip);
$data = file_get_contents('http://api.map.baidu.com/location/ip?ak=ia6HfFL660Bvh43exmH9LrI6&ip='.$ip);
$res = json_decode($data,true);
if($res['status'] == 0){
return $res['content']['address'];
}else{
return '';
}
}
//获取用户浏览器类型
function getBrowser(){
$agent=$_SERVER["HTTP_USER_AGENT"];
if(strpos($agent,'MSIE')!==false || strpos($agent,'rv:11.0')) //ie11判断
return "ie";
else if(strpos($agent,'Firefox')!==false)
return "firefox";
else if(strpos($agent,'Chrome')!==false)
return "chrome";
else if(strpos($agent,'Opera')!==false)
return 'opera';
else if((strpos($agent,'Chrome')==false)&&strpos($agent,'Safari')!==false)
return 'safari';
else
return 'unknown';
}
//获取网站来源
function getFromPage(){
if(isset($_SERVER['HTTP_REFERER'])){
return $_SERVER['HTTP_REFERER'];
}else{
return '';
}
}
//获取渠道
function getQudao(){
if(!isset($_SERVER['HTTP_REFERER'])){
return '';
}
if(strpos($_SERVER['HTTP_REFERER'],"www.baidu.com")> -1){
return '百度';
}
if(strpos($_SERVER['HTTP_REFERER'],"www.google.com")> -1){
return '谷歌';
}
if(strpos($_SERVER['HTTP_REFERER'],"www.so.com")> -1){
return '360搜索';
}
if(strpos($_SERVER['HTTP_REFERER'],"www.sogou.com")> -1){
return '搜狗搜索';
}
if(strpos($_SERVER['HTTP_REFERER'],"cn.bing.com")> -1){
return '必应搜索';
}
return '';
}
//获取当前页网址
function getCurrentPage()
{
$protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || $_SERVER['SERVER_PORT'] == 443) ? "https://": "http://";//协议头
$url = $protocol . $_SERVER['HTTP_HOST']. $_SERVER['REQUEST_URI'];//完整的url地址
return $url;
}
然后做一下后台管理页面,在dede目录下创建tongji_main.php和tongji_edit.php
tongji_main.php代码如下:
<?php
require_once(dirname(__FILE__).'/config.php');
require_once(DEDEINC.'/datalistcp.class.php');
require_once(DEDEINC.'/common.func.php');
setcookie('ENV_GOBACK_URL',$dedeNowurl,time()+3600,'/');
$sql = "Select * from `#@__tongji` order by time desc";
$start = strtotime(date('Y-m-d', time()));
$end = strtotime(date('Y-m-d', time()))+86400;
$row = $dsql->GetOne("select count(*) as total from `#@__tongji` where time BETWEEN $start AND $end");
$total = $row['total'];//输出总数
$dlist = new DataListCP();
$dlist->SetParameter('total', $total);
$dlist->SetTemplet(DEDEADMIN."/templets/tongji_main.htm");
$dlist->SetSource($sql);
$dlist->display();
function timeToDate($time)
{
return date('Y-m-d H:i:s',$time);
}
require_once(dirname(__FILE__).'/config.php');
require_once(DEDEINC.'/datalistcp.class.php');
require_once(DEDEINC.'/common.func.php');
setcookie('ENV_GOBACK_URL',$dedeNowurl,time()+3600,'/');
$sql = "Select * from `#@__tongji` order by time desc";
$start = strtotime(date('Y-m-d', time()));
$end = strtotime(date('Y-m-d', time()))+86400;
$row = $dsql->GetOne("select count(*) as total from `#@__tongji` where time BETWEEN $start AND $end");
$total = $row['total'];//输出总数
$dlist = new DataListCP();
$dlist->SetParameter('total', $total);
$dlist->SetTemplet(DEDEADMIN."/templets/tongji_main.htm");
$dlist->SetSource($sql);
$dlist->display();
function timeToDate($time)
{
return date('Y-m-d H:i:s',$time);
}
tongji_edit.php代码如下:
<?php
require(dirname(__FILE__)."/config.php");
if(empty($dopost))
{
$dopost = '';
}
$ENV_GOBACK_URL = empty($_COOKIE['ENV_GOBACK_URL']) ? "tongji_main.php" : $_COOKIE['ENV_GOBACK_URL'];
if($dopost=='delete')
{
$dsql->ExecuteNoneQuery("Delete From `#@__tongji` where id='$id' ");
ShowMsg("成功删除访问记录",$ENV_GOBACK_URL);
exit();
}
if($dopost=='delAll')
{
$dsql->ExecuteNoneQuery("Delete From `#@__tongji` where 1");
ShowMsg("成功删除访问记录",$ENV_GOBACK_URL);
exit();
}
?>
require(dirname(__FILE__)."/config.php");
if(empty($dopost))
{
$dopost = '';
}
$ENV_GOBACK_URL = empty($_COOKIE['ENV_GOBACK_URL']) ? "tongji_main.php" : $_COOKIE['ENV_GOBACK_URL'];
if($dopost=='delete')
{
$dsql->ExecuteNoneQuery("Delete From `#@__tongji` where id='$id' ");
ShowMsg("成功删除访问记录",$ENV_GOBACK_URL);
exit();
}
if($dopost=='delAll')
{
$dsql->ExecuteNoneQuery("Delete From `#@__tongji` where 1");
ShowMsg("成功删除访问记录",$ENV_GOBACK_URL);
exit();
}
?>
还要在dede/templets目录下创建一个tongji_main.htm
{dede:config.pagesize value='20' /}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo $cfg_soft_lang; ?>">
<title>统计管理</title>
<link href='css/base.css' rel='stylesheet' type='text/css'>
</head>
<body background='images/allbg.gif' leftmargin='8' topmargin='8'>
<table width="98%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#D1DDAA">
<tr>
<td height="28" background="images/tbg.gif" colspan="8">
<b>统计管理</b> <u><a href="tongji_edit.php?id={dede:field.id /}&dopost=delAll">删除所有</a></u> <span style="float:right;margin-right:50px;">今日访问总人数:{dede:global.total/}</span>
</td>
</tr>
<tr align="center" bgcolor="#F9FCEF" height="24">
<td width="20%">id</td>
<td width="20%">访问ip</td>
<td width="20%">访问地区</td>
<td width="20%">访问时间</td>
<td width="20%">管理</td>
</tr>
{dede:datalist}
<tr align="center" bgcolor="#FFFFFF" height="24" onMouseMove="javascript:this.bgColor='#f4fbf4';" onMouseOut="javascript:this.bgColor='#FFFFFF';">
<td>{dede:field.id /}</td>
<td>{dede:field.ip/}</td>
<td>{dede:field.diqu/}</td>
<td>{dede:field.time function='timeToDate(@me)'/}</td>
<td>
<!--<a href='tongji_edit.php?aid={dede:field.id /}&dopost=edit'>[更改]</a>-->
<a href='tongji_edit.php?id={dede:field.id /}&dopost=delete'>[删除]</a>
</td>
</tr>
{/dede:datalist}
<tr align="center" bgcolor="#F9FCEF" height="24">
<td colspan="8">
{dede:pagelist listsize=5/}
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo $cfg_soft_lang; ?>">
<title>统计管理</title>
<link href='css/base.css' rel='stylesheet' type='text/css'>
</head>
<body background='images/allbg.gif' leftmargin='8' topmargin='8'>
<table width="98%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#D1DDAA">
<tr>
<td height="28" background="images/tbg.gif" colspan="8">
<b>统计管理</b> <u><a href="tongji_edit.php?id={dede:field.id /}&dopost=delAll">删除所有</a></u> <span style="float:right;margin-right:50px;">今日访问总人数:{dede:global.total/}</span>
</td>
</tr>
<tr align="center" bgcolor="#F9FCEF" height="24">
<td width="20%">id</td>
<td width="20%">访问ip</td>
<td width="20%">访问地区</td>
<td width="20%">访问时间</td>
<td width="20%">管理</td>
</tr>
{dede:datalist}
<tr align="center" bgcolor="#FFFFFF" height="24" onMouseMove="javascript:this.bgColor='#f4fbf4';" onMouseOut="javascript:this.bgColor='#FFFFFF';">
<td>{dede:field.id /}</td>
<td>{dede:field.ip/}</td>
<td>{dede:field.diqu/}</td>
<td>{dede:field.time function='timeToDate(@me)'/}</td>
<td>
<!--<a href='tongji_edit.php?aid={dede:field.id /}&dopost=edit'>[更改]</a>-->
<a href='tongji_edit.php?id={dede:field.id /}&dopost=delete'>[删除]</a>
</td>
</tr>
{/dede:datalist}
<tr align="center" bgcolor="#F9FCEF" height="24">
<td colspan="8">
{dede:pagelist listsize=5/}
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
这样一个插件就做好了。
插件使用方法也很简单,在需要添加统计的前台页面