Home >Backend Development >PHP Tutorial >[原创]最简单的文本计数器--(好象也不简单,呵呵夸大了~)_PHP

[原创]最简单的文本计数器--(好象也不简单,呵呵夸大了~)_PHP

WBOY
WBOYOriginal
2016-06-01 12:36:08837browse

计数器

$file="count.dat";
if(@file_exists($file)){
    $fp=@file($file);
    $num=$fp[0];
    $f=fopen($file,"w+");
    $number=$num+1;
    fputs($f,$number);
}else{
    $f=fopen($file,"a+");
    fputs($fp,"0");
}
@fclose($f);
?>
您好=$_SERVER["REMOTE_ADDR"]?>,您是本站的第[=$number?>]位客人

封装为
class Counter{
   function addnumber(){
//多用户   function addnumber($user){    //提示:您可以使用ID来进行多用户(多页面)计数例如:$c=new Counter;$c->addnumber($id);
    $file="count.dat";
//多用户(页面)    $file=$user.".dat";
        if(@file_exists($file)){
            $fp=@file($file);
            $num=$fp[0];
            $f=fopen($file,"w+");
            $number=$num+1;
            fputs($f,$number);
        }else{
            $f=fopen($file,"a+");
            fputs($fp,"0");
        }
        @fclose($f);
print("您好$_SERVER["REMOTE_ADDR"],您是本站的第[$number]位客人");
   }
}

调用
eg.php

$co=new Conter;
$co->addnumber();    //单用户计数
$co->addnumber($id);    //多用户(页面)计数,这里的ID可以为您数据库内符合记录的用户ID,也可以为你剥除了目录的PHP_SELF,呵呵,单独放个counter目录,里面每个页面(用户)都有他自己同样名称的.dat文件来记录他的
页面的访问量~,简单的也可以做不简单的事情哦~
?>


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:初识PEAR_PHPNext article:一个分页导航类_PHP