search
Homephp教程php手册PHP实现的记数器

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




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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.