首页 > 开发 > PHP > 正文

PHP实现的记数器

2024-05-04 22:59:29
字体:
来源:转载
供稿:网友
  • 本文来源于网页设计爱好者web开发社区http://www.html.org.cn收集整理,欢迎访问。
  • //counter_simple.php: 简单记数器

    <html>                                                                 
                                                                           
    <head>                                                                 
                                                                           
    <title>                                                                
    文本计数器                                                             
    </title>                                                               
                                                                           
    </head>                                                                
    <body>                                                                 
                                                                           
    <?                                                                     
                                                                           
    $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<6;$i++)                                                    
    {                                                                      
      echo "<p>&nbsp</p>/n";//显示空行                                     
    }                                                                      
                                                                           
    //浏览器输出浏览次数                                                   
    echo "<h2 align=center>您好!第&nbsp<i>$count_num</i>&nbsp位顾客!</h2>";
                                                                           
    ?>                                                                     
                                                                           
    </body>                                                                
                                                                           
    </html>   

    //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<=$count_length;$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;$i<=$count_length-1;$i++) {                
    imageline($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);//释放该图像文件                   
    ?>                                       


    发表评论 共有条评论
    用户名: 密码:
    验证码: 匿名发表