×î¼òµ¥µÄPHP³ÌÐò–¼ÇÊýÆ÷

php图片计数器代码,他的实现方法是先用程序把数字保存到一个txt文件或数据库,然后再读取,当然数字我们要做0-9的图片啊.好了下面我们来看看统计代码吧.

php
文件计数据器+mysql双保护,这是上次升级版了,上次流量大太经常出问题今天修改过来了.

Ô­Àí£º  
1.µÚһλʹÓÃÕßä¯ÀÀijҳ¡£
2.ËÅ·þÆ÷³Ìʽ´Ó×ÊÁÏ¿â»òµµ°¸ÖжÁÈ¡¸ÃÒ³±»ä¯ÀÀ´ÎÊý¡£
3.½«´ÎÊý¼ÓÒ»´¢´æ£¬²¢½«ËüËͻصÚһλʹÓÃÕß¡£
4.µÚ¶þλʹÓÃÕßä¯ÀÀijҳ¡£
5.ËÅ·þÆ÷³Ìʽ´Ó×ÊÁÏ¿â»òµµ°¸ÖжÁÈ¡¸ÃÒ³±»ä¯ÀÀ´ÎÊý¡£
6.½«´ÎÊýÔÙ¼ÓÒ»´¢´æ£¬²¢½«ËüËͻصڶþλʹÓÃÕß¡£   
ÐèÒªÁ˽âµÄº¯Êý:  
fopen()´ò¿ªÎļþ
filesize()»ñµÃÎļþ´óС
fseek()Òƶ¯ÎļþÖ¸Õë
fgets()µÃµ½ÎļþÖ¸ÕëËùÔÚÐÐÄÚÈÝ
fputs()½«×Ö´®Ð´ÈçÎļþÖ¸ÕëËùÔÚλÖÃ
fclose()¹Ø±ÕÎļþ
file_exists()ÅжÏÎļþÊÇ·ñ´æÔÚ
exec()Ö´ÐÐÍⲿ³ÌÐò  

?phprequire_once(../inc/connect.php);Class Sit_count{ public $FileName
= ‘count.txt’; function Count_add(){if(file_exists($this-FileName)
){$Temp = file_get_contents($this-FileName);$Array =
explode(=,$Temp);if(count($Array) != 2){$Tmp
=’mytext=’.$this-Tcount();}else{$Total = $Array[1]+1;$Tmp
=$Array[0].’=’.$Total;}$T =
@fopen($this-FileName,’w’);if($T){fwrite($T,$Tmp);fclose($T);}mysql_query(update
wk_count set count_bak =count_bak+1 where id=1);}else{echo ‘!
file_exists’;} } function Tcount(){ $result =mysql_query(Select *
from wk_count where id =1 ) or die(mysql_error());$rs
=mysql_fetch_array($result);return $rs[‘count_bak’]; }} $tp = new
Sit_count; $tp-Count_add();?好了下面我们来看看show.php输出文件.

<?php
 require_once(“../inc/connect.php”);
 Class Sit_count{
   public $FileName = ‘count.txt’; 
  function Count_add(){
   if(file_exists($this->FileName) ){
    $Temp = file_get_contents($this->FileName);    
    $Array = explode(“=”,$Temp); 
    if(count($Array) != 2){   
     $Tmp =’mytext=’.$this->Tcount();
    }else{
     $Total = $Array[1]+1;  
     $Tmp =$Array[0].’=’.$Total;      
    }
    $T = @fopen($this->FileName,’w’);
    if($T){
     fwrite($T,$Tmp);
     fclose($T);
    }
    mysql_query(“update wk_count set count_bak =count_bak+1 where
id=1”);
   }else{
    echo ‘! file_exists’;
   }
   
  }
 
  function Tcount(){
    $result =mysql_query(“Select * from wk_count where id =1 “) or
die(mysql_error());
   $rs =mysql_fetch_array($result);
   return $rs[‘count_bak’];
   
  }
 }
  $tp = new Sit_count;
  $tp->Count_add();
?>
本站原创转载注明www.111cn.net/phper/php.html

×î¼òµ¥µÄ¼ÇÊýÆ÷:  
<html>  
<head>  
<title>·Ã¿Í¼ÆÊýÆ÷ Ô­ÐÍ</title>  
</head>  
<body>  
<?php  
/*  
(c)1998 David W. Bettis  
ÕâÀïÊÇ°æȨÐÅÏ¢  
*/  

?php$count = substr(file_get_contents(‘count.txt’),7);$con_len
=strlen($count);for($i=$con_len-1;$i=0;$i–){echodocument.write(‘img
src=../counter/counter/.substr($count,$i,1)..jpg /’);; }

$counterFile = “counter.txt”;  
#ÕâÀïÊǶ¨Òå¼ÇÊýÆ÷Îļþ

?

function displayCounter($counterFile) {  
$fp = fopen($counterFile,”rw”);
#´ò¿ªÎļþ£¬ÓöÁд·½Ê½

这样我们的输片计数器就出来了,注意哦.echodocument.write(‘img
src=../counter/counter/.substr($count,$i,1)..jpg
/’);;必须以document.write输出哦,因为我们下面要用js形式调用哦.

$num = fgets($fp,5);
#È¡µÃµ±Ç°Êý×Ö

下面在txt.html这样调用show.php文件就行了如下.

$num += 1;
#¼Ó1

script src=’show.php’/script

print “ÄúÊÇµÚ “.”$num”.” λÎÞÁÄ·Ý×Ó”;  
exec( “rm -rf $counterFile”);  
exec( “echo $num > $counterFile”);  
#͵ÀÁµÄ·½Ê½Å¶£¬²»Ê¹ÓÃfputsдÈë  
}  

等吧,我们教程就OK了.

if (!file_exists($counterFile)) {  
exec( “echo 0 > $counterFile”);  
}#Èç¹û¼ÇÊýÆ÷Îļþ²»´æÔÚ£¬Ð½¨Ëü²¢ÉèÖÃÄÚÈÝΪ0

本站原创转请注明:

displayCounter($counterFile);  

?>  
</body>  
</html>   
PHP¼ÇÊýÆ÷±È½Ï¼òµ¥°æ£º  
<?
#°æȨûÓÐÀ²£¬Õâô¼òµ¥  

$fp=fopen(“counter.txt”,”r+”);
flock($fp,3);  
#´ò¿ª¼ÇÊýÆ÷Îļþ²¢Ëøס

$fsize=filesize(“count.txt”);
$count=fgets($fp,$fsize+1);
$count++;  
#È¡µÃÊýÂë²¢¼ÓÒ»

fseek($fp,0);
fputs($fp,$count);
fclose($fp);
#½«ÐÂÊýÂëдÈëÎļþ

echo “ÄãÊÇµÚ $count λ·ÃÎÊÕß”;
?>  

PHP¼ÇÊýÆ÷ͼÐΰ棺  
ÖÆ×÷10¸öͼƬ£¬½«Êý×Ö´®ÓÃͼƬ×éÆðÀ´£¬ÎҾͲ»Ï¸ËµÁË
¼ÙÉèͼƬΪ0.gif ~ 9.gif

<?
….$countΪȡµÃµÄÊýÖµ
$strcount=strval($count);
$strcount=chop($strcount);
$countlen=$strlen($strcount);
$shtml=””;
for ($i=0; $i<$countlen; $i++) {
$shtml.=”<img src='”;
$shtml.=$strcount[$i];
$shtml.=”.gif’>”;
}
echo $shtml;
?>  

PHP¼ÇÊýÆ÷Êý¾Ý¿â°æ£º  
ʹÓÃSQL¼ÇÊýÆ÷£¬ÏȽ¨ºÃ±í
CREATE TABLE counter
(
counter int not null,
id ÿÿÿÿÿint not null
)
INSERT INTO counter(counter,id) VALUE(0,1)

<?
$conn=mysql_connect(…, …, …);
#MySQLÊý¾Ý¿âÁ¬½Ó

$sql=”select * from counter”;
$result=mysql_query($sql,$conn);
$objresult=mysql_fetch_object($result);
$count=$objresult->counter;
$count++;  

$sql=”update counter set counter=”.$count.”where id=1″;
mysql_query($sql,$conn);
mysql_close($conn);

echo “ÄãÊǵÚ$countλ·Ã¿Í”;
?> 

发表评论

电子邮件地址不会被公开。 必填项已用*标注