php ajax網(wǎng)站瀏覽統(tǒng)計功能的簡單實現(xiàn)
更新時間:2008年09月27日 17:56:13 作者:
這個功能應(yīng)該是很多網(wǎng)站都需要的,這里僅僅實現(xiàn)了一個基于文件的簡易版本,數(shù)據(jù)庫的版本請自行參考實現(xiàn),我這里實現(xiàn)的功能很不完善,比如未過濾是否為同一訪客,是否為同一IP等等,這里僅僅是給大家提供一個參考.
對于這個文件中使用的連接設(shè)置ctl參數(shù)見[ajax實時任務(wù)提示功能的實現(xiàn)]中的/ucren/taskofpig/appConfig.php
文件的controllerAccessor 設(shè)置.
//各位注意目錄使用Serv這是這個它是一個插件,(*^__^*) 嘻嘻……
/ucren/taskofpig/Serv/VisiterService.php
<?php
class Serv_VisiterService
{
var $log_file ;
function Serv_VisiterService($log_file) //必須傳遞日志文件路徑進(jìn)來
{
$this->log_file = $log_file ;
}
function addVisiter()
{
$newVisiter = array(
'guest_ip' => $_SERVER["REMOTE_ADDR"] ,
'time' => date('Y-m-d H:i:s') ,
'guest_port' => $_SERVER["REMOTE_PORT"] ,
'request_uri' => $_SERVER["REQUEST_URI"] ,
'accept_lang' => $_SERVER["HTTP_ACCEPT_LANGUAGE"] ,
'os_info' => $_SERVER["HTTP_USER_AGENT"]
);
//$fp = fopen("{$this->prj_dir}/_log/visiter.dat","a+b");
$fp = fopen($this->log_file,"a+b");
fwrite($fp,serialize($newVisiter));
//寫入換行符--LINUX是\n windows是 \r\n ,這里要求注意 單引號與雙引號的區(qū)別
fwrite($fp,"\r\n");//單引號不轉(zhuǎn)義
fclose($fp);
}
function getVisiters()
{
if (!file_exists($this->log_file))
return null ;
$visiterArr_tmp = file($this->log_file) ;//將文件讀入數(shù)組中
foreach($visiterArr_tmp as $visiter)
{
$visiterArr[] = unserialize($visiter) ;
}
return $visiterArr ;
}
}
?>
這里在上文中新建的/ucren/taskofpig/Controller/Default.php文件中添加如下代碼,完整代碼如下
<?php
FLEA::loadFile('Serv_VisiterService.php',true) ;//加載訪客統(tǒng)計插件代碼到程序中
class Controller_Default extends FLEA_Controller_Action
{
var $prj_dir ;var $visiters ;var $smarty ;
function actionIndex(){
$this->prj_dir = '.' ;
$this->smarty = $this->_getView(); //獲取smarty模板對象,在/ucren/taskofpig/appConfig.php中配置
$this->visiters = new Serv_VisiterService("{$this->prj_dir}/log/visiter.dat") ;
//添加訪客
$this->visiters->addVisiter();
redirect(url('TaskOfPig'),0); //停頓0秒后,重定向到index.php?ctl=TaskOfPig
}
function actionVisiters() //訪客統(tǒng)計列表
{
$this->smarty->assign('sitename','任務(wù)計劃表 -- 生氣豬') ;
$this->smarty->assign('opname','訪客統(tǒng)計') ;
$rows = $this->visiters->getVisiters();
$this->smarty->assign('rowSet',$rows);
$this->_showPage('taskofpig.visiterlist.html');
}
}
?>
/ucren/taskofpig/tpl/taskofpig.visiterlist.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><%$sitename%> -- <%$opname%></title>
</head>
<body>
<hr>
<table width="100%" border="1" cellspacing="1" bgcolor="#cfdadc">
<tr bgcolor="#e8edec" align="center">
<td><b>訪問次序</b></td>
<td><b>IP地址</b></td>
<td><b>日期/時間</b></td>
<td><b>客戶機(jī)信息</b></td>
</tr>
<%section name=rowIndex loop=$rowSet%>
<tr align="center">
<%*注意怎么獲取rowIndex的語法*%>
<td><%$smarty.section.rowIndex.index%></td>
<td><%$rowSet[rowIndex].guest_ip%></td>
<td><%$rowSet[rowIndex].time%></td>
<td><%$rowSet[rowIndex].os_info%></td>
</tr>
<%/section%>
</table>
</body>
</html>
這樣就可以在瀏覽器中敲入
http://localhost/ucren/taskofpig 進(jìn)入,缺省將在后臺加入訪問者信息,如果你敲入
http://localhost/ucren/taskofpig/index.php?act=visiters
可以進(jìn)入來訪者信息查看頁面,例子截圖如下
相關(guān)文章
基于bootstrap的上傳插件fileinput實現(xiàn)ajax異步上傳功能(支持多文件上傳預(yù)覽拖拽)
這篇文章主要介紹了基于bootstrap的上傳插件fileinput 的ajax異步上傳功能(支持多文件上傳預(yù)覽拖拽),非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-03-03淺析json與jsonp區(qū)別及通過ajax獲得json數(shù)據(jù)后格式的轉(zhuǎn)換
一言以蔽之,json返回的是一串?dāng)?shù)據(jù);而jsonp返回的是腳本代碼(包含一個函數(shù)調(diào)用);接下來通過本文給大家介紹json與jsonp區(qū)別及通過ajax獲得json數(shù)據(jù)后格式的轉(zhuǎn)換,需要的朋友參考下2016-03-03Ajax向后臺傳json格式的數(shù)據(jù)出現(xiàn)415錯誤的原因分析及解決方法
ajax往后臺傳json格式數(shù)據(jù)報415錯誤,什么原因?qū)е碌哪兀撛趺唇鉀Q呢?下面腳本之家小編給大家?guī)砹薃jax向后臺傳json格式的數(shù)據(jù)出現(xiàn)415錯誤的原因分析及解決方法感興趣的朋友一起看看吧2016-10-10通過Ajax請求動態(tài)填充頁面數(shù)據(jù)的實例
今天小編就為大家分享一篇通過Ajax請求動態(tài)填充頁面數(shù)據(jù)的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08