首页 > php教程 > php手册 > 正文

PHP实现的记数器

WBOY
发布: 2016-06-21 09:06:20
原创
1194 人浏览过

//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);//释放该图像文件                   
?>                                       




相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门推荐
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板