Home > php教程 > php手册 > PHP实现的记数器

PHP实现的记数器

WBOY
Release: 2016-06-21 09:06:20
Original
1229 people have browsed it

//counter_simple.php: 简单记数器

                                                                 
                                                                       

                                                                 
                                                                       
                                                                 <br>文本计数器                                                              <br>                                                                
                                                                       
                                                                
                                                                 
                                                                       
                                                                     
                                                                       
$count_num=0;                                                          
                                                                       
// 如果存放计数器文件已经存在,读取其中的内容                          
if(file_exists("counter.txt"))                                         
{                                                                      
   /******************************                                     
   以只读方式打开counter.txt文件                                       
   counter.txt用来存放计数器的值                                       
   *******************************/                                    
   $fp = fopen("counter.txt", "r");                                    
   //读取计数器的前8位数字                                             
   $count_num = fgets($fp,9);                                          
   //浏览次数加一                                                      
   $count_num++;                                                       
   //关闭文件                                                          
   fclose($fp);                                                        
}                                                                      
                                                                       
/***************************                                           
以只写的方式打开counter.txt文件                                        
把最新的计数值放入该文件中                                             
****************************/                                          
$fp = fopen("counter.txt", "w");                                       
                                                                       
//写入最新的值                                                         
fputs($fp, $count_num);                                                
                                                                       
//关闭文件                                                             
fclose($fp);                                                           
                                                                       
for($i=1;$i{                                                                      
  echo "

 

\n";//显示空行                                     
}                                                                      
                                                                       
//浏览器输出浏览次数                                                   
echo "

您好!第 $count_num 位顾客!

";
                                                                       
?>                                                                     
                                                                       
                                                                
                                                                       
   

//counter_graph.php:图象记数器

                                                   
/*********************************                   
定义本程序的输出是一幅图象                           
而且这副图象是gif格式的                              
浏览器使用本程序产生的图象                           
*********************************/                   
Header("Content-type: image/gif");                   
                                                     
                                                     
//变量$count_length是需显示的位数                    
$count_length=8;                                     
                                                     
//$str是需要显示的计数值                             
$str=0;                                              
                                                     
// 如果存放计数器文件已经存在,读取其中的内容        
if ( file_exists("counter.txt") )                    
{                                                    
   /******************************                   
   以只读方式打开counter.txt文件                     
   counter.txt用来存放计数器的值                     
   *******************************/                  
  $fp = fopen("counter.txt", "r");                   
  $str = fgets($fp,$count_length+1);                 
  fclose($fp);                                       
}                                                    
                                                     
$str++;                                              
                                                     
/***************************                         
以只写的方式打开counter.txt文件                      
把最新的计数值放入该文件中                           
****************************/                        
$fp = fopen("counter.txt", "w");                     
fputs($fp, $str);                                    
fclose($fp);                                         
                                                     
$str_0 = $str;//$str_0存放计数值前面补0后的字符串    
                                                     
$len_old = strlen($str);//$len_old存放原有计数值的位数
                                                     
/****************************                        
如果原有计数值的位数不足,                            
则在它的前面加0补齐                                  
****************************/                        
for ($i=$len_old+1;$i{                                                    
  $str_0 = "0".$str_0;                               
};                                                   
                                                     
$font = 3;//定义字号                                 
                                                     
$im = imagecreate($count_length*11-1,16);            
//新建图象                                           
                                                     
$black = ImageColorAllocate($im, 0,0,0);//黑色       
$white = ImageColorAllocate($im, 255,255,255);//白色 
//定义颜色                                           
                                                     
//把计数器的底色设置成黑色                           
imagefill($im, 0,0,$black);                          
                                                     
/**********************                              
用白色显示计数器的值,                                
在每个数字之间都用线分隔                             
***********************/                             
ImageString($im,$font,1,0,$str_0[0],$white);         
for ($i=1;$iimageline($im, $i*11-1,0,$i*11-1,16, $white);        
ImageString($im,$font,$i*11+1,0,$str_0[$i],$white);  
};                                                   
                                                     
ImageGif($im);//输出gif图像文件                      
                                                     
ImageDestroy($im);//释放该图像文件                   
?>                                       




Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template