由于某个功能,需要用到统计功能,由于功能蛮简单,用别的写有点累赘。PHP成为不二选择,可是。。问题出现

了,我对PHP不熟悉,郁闷,平时折腾Wp也是硬着头皮上阵,琢磨着弄,这次的功能是需要一个统计,远程服务

器还得拿到这个统计值来使用。上吧,google之~

还是找到一个demo,可做点参考,可是~  这个存的是本地文件来记录,先做个备忘。

index.php

< ?php
class SeekClickDownCounter {

        var $downloadData;
        var $downloadDataFile = 'data.php';

        function oError($n) {

                switch($n) {
                        case 1:
                                $msg = '没有指定文件';
                                break;
                        case 2:
                                $msg = '无法识别';
                                break;
                        default:
                                $msg = 'Unknow';
                                break;
                }

                exit("<font sizee='2'>{$msg}");
        }

        function sData() {
                if (is_array($this->downloadData)) {
                        foreach($this->downloadData as $k => $v) {
                                echo "<a href='$_SERVER[PHP_SELF]?sk=download&f=$k'>[下载]</a> - 文件名 : {$v[0]} - 下载次数 : {$v[1]}<br />";
                        }
                }
        }

        function sDownload() {
                if (emptyempty($_GET['f'])) { $this->oError(1); }
                if (!$this->downloadData[$_GET['f']]) { $this->oError(2); }

                $this->downloadData[$_GET['f']][1] += 1;

                $fp = fopen($this->downloadDataFile,'r+');
                flock($fp, LOCK_EX);
                fseek($fp, 0, 0);
                fwrite($fp, serialize($this->downloadData));
                flock($fp, LOCK_UN);
                fclose($fp);

                header("Location: {$this->downloadData[$_GET['f']][0]}");
        }

        function sProccess() {

                $fp = fopen($this->downloadDataFile, 'r');
                flock($fp, LOCK_EX);
                $downloadData = fread($fp, filesize($this->downloadDataFile));
                flock($fp, LOCK_UN);
                fclose($fp);

                $this->downloadData = unserialize($downloadData);
                switch($_GET['sk']) {
                        case 'download':
                                $this->sDownload();
                                break;
                        default:
                                $this->sData();
                                break;
                }
        }

}

$o = new SeekClickDownCounter;
$o ->sProccess();

$url = $_GET['url'];

count_dl($url);
//记录读取次数
function count_dl($fileurl, $countfile=""){
 $downnum_arrs = array();
 if(!$countfile){
  $countfile = "count.txt";
 }
 $countfile = dirname(__FILE__) . "/" .$countfile;
 if(file_exists($countfile)){
  $downnum = file_get_contents($countfile);
  $downnum_arrs = unserialize($downnum);

 }
 $downnum_arrs[$fileurl]+=1;


 $content = serialize($downnum_arrs);

 $fp = fopen("count.txt", "w");
 fwrite($fp, $content);
 fclose($fp);

 return $downnum_arrs;
}
?>

data.php

a:3:{i:1;a:2:{i:0;s:7:"def.rar";i:1;i:0;}i:2;a:2:{i:0;s:7:"def.rar";i:1;i:2;}i:3;a:2:{i:0;s:7:"ghi.zip";i:1;i:0;}}

源码下载:

温馨提示:本文发布已久,站内下载链接已失效!

题外话,自从主机不支持copy()函数后,速度上很受影响,尤其加载评论的时候,gvaratar的速度相当考验人。

非常杯具!