首 页   · 站长博客 · 用户注册 · 会员登陆  · 会员排行  ·最新主题  ·最近回复  精华区  版权声明  ·论坛管理
  当前登录身份:游客,请先登录。  笔名: 口令: 验证码:   
楼 主  index »  PHP与模板与代码加密/优化 » [转帖]计数器程序  


  作者:pnrj7999
  注册时间:2005-04-04
  主题/回复:73/43
  积分:813
  等级:★★★(六级)
  称号:声名鹊起

用户联系方式已设置为保密

 

 发表:2005-06-06 00:10:40 阅读 2026 次 回复 1 次 得分3  |   字号 字色
[转帖]计数器程序
<? 
///////////////////////////////////////////////////////////////////////// 
// 
// counter.php - 实现一个很简单的计数器
//  
// 作者: Claus Radloff 
//  
// 描述:
// 这个计数器可以变成多重的计数器。因此,每一个 计数器可以自己得到自己
// 的身份证明。 
//
// 前面的部分——是背景色以及数字的定义,你可能自行修改。
// 当然,如果背景色是透明的那就更好了。 
// 
// 以下是一些在 URL 中的参数: 
// 例如: 
// Simple Counter:
//   <img src="counter.php?Identifier=Test"> 
//    
// 红色背景的计数器: 
//   <img src="counter.php?Identifier=Test&BGColor=255+0+0"> 
// 或者是: 
//   <img src="counter.php?Identifier=Test&BGColor=red"> 
//    
// 红色背景,绿色前景,只有4位数的计数器
//   <img src="counter.php?Identifier=Test&BGColor=255+0+0&FGColor=0+255+0&Length=4"> 
// 或是: 
//   <img src="counter.php?Identifier=Test&BGColor=red&FGColor=green&Length=4"> 
//    
// 透明背景和红色前景的样式:
//   <img src="counter.php?Identifier=Test&BGColor=transparent&FGColor=red"> 
//  

 4/14/2000    Mouse Chen( litmouse@km169.net   )    Translate it to Chinese


    Header( "Content-type: image/gif"); 
 
    require( "colors.php"); 
 
 // 装载GD-Library 
 // Windows下是这条语句:dl("php3_gd.dll"); 
 // 而 UNIX 就应该是下面这条:
    dl( "php3_gd.so"); 

 // 一些初始设定
    $Font = 5; 
    $BGColor  = GetColor( "black"); 
    $BGTrans  = False; 
    $FGColor  = GetColor( "white"); 
    $FGTrans  = False; 
    $Length   = 7; 

 // 取得输入参数 
    $query_string = getenv( "QUERY_STRING"); 
 
 // 分析输入的参数
 // 并分闻参数 
    $env_array = split( "&", $query_string); 
 
 // 把参数及其值分开,并把值转成 %XX的形式 
    while (list($key, $val) = each($env_array)) 
    { 
    list($name, $wert) = split( "=", $val); 
     
    $name = urldecode($name); 
    $wert = urldecode($wert); 
     
     // 写入 $cgivars 
    $CGIVars[$name] = $wert; 
    } 
 
 // 用输入的参数重新设定初始值 
    if ($CGIVars[ "BGColor"]) 
    { 
    if (ereg( "([0-9]*) ([0-9]*) ([0-9]*)", $CGIVars[ "BGColor"], $tmp)) 
    { 
    $BGColor[ "red"]   = $tmp[1]; 
    $BGColor[ "green"] = $tmp[2]; 
    $BGColor[ "blue"]  = $tmp[3]; 
    } 
    else if (eregi( "transparent", $CGIVars[ "BGColor"])) 
    { 
    $BGTrans = True; 
    } 
    else 
    { 
    $BGColor = GetColor($CGIVars[ "BGColor"]); 
    } 
    } 

    if ($CGIVars[ "FGColor"]) 
    { 
    if (ereg( "([0-9]*) ([0-9]*) ([0-9]*)", $CGIVars[ "FGColor"], $tmp)) 
    { 
    $FGColor[ "red"]   = $tmp[1]; 
    $FGColor[ "green"] = $tmp[2]; 
    $FGColor[ "blue"]  = $tmp[3]; 
    } 
    else if (eregi( "transparent", $CGIVars[ "FGColor"])) 
    { 
    $FGTrans = True; 
    } 
    else 
    { 
    $FGColor = GetColor($CGIVars[ "FGColor"]); 
    } 
    } 

    if ($CGIVars[ "Length"]) 
    { 
    $Length = $CGIVars[ "Length"]; 
    } 

    $SizeX = $Length * 13; 
    $SizeY = 16; 
 
 // 读入 counter-文件 
    if (file_exists( "counter.txt")) 
    { 
    $fp = fopen( "counter.txt",  "rt"); 
    while ($Line = fgets($fp, 999)) 
    { 
    if (ereg( "([^ ]*) *([0-9]*)", $Line, $tmp)) 
    { 
    $CArr[ "$tmp[1]"] = $tmp[2]; 
    } 
    } 
     
    fclose($fp); 
     
     // 得到计数器的标志 
    $Counter = $CArr[$CGIVars[ "Identifier"]]; 
    $Counter += 1; 
    $CArr[$CGIVars[ "Identifier"]] = $Counter; 
    } 
    else 
    { 
     // 新的计数器初值为1 
    $CArr[$CGIVars[ "Identifier"]] = 1; 
    $Counter = 1; 
    } 
 
 // 写入计数器文件中
    $fp = fopen( "counter.txt",  "wt"); 
 
    reset($CArr); 
    while (list($Key, $Value) = each($CArr)) 
    { 
    $tmp = sprintf( "%s %d\n", $Key, $Value); 
    fwrite($fp, $tmp); 
    } 
 
    fclose($fp); 
 
    $Counter = sprintf( "%0".$Length. "d", $Counter); 
 
 // 创建 image 
    $img = ImageCreate($SizeX + 4, $SizeY + 4); 
 
    ImageInterlace($img, 1); 
 
 // 透明颜色 
    $trans = ImageColorAllocate($img, 1, 1, 1); 
    ImageColorTransparent($img, $trans); 

 // 填充背景色
    if ($BGTrans) 
    { 
    ImageFill($img, 1, 1, $trans); 
    } 
    else 
    { 
    $col = ImageColorAllocate($img, $BGColor[ "red"], $BGColor[ "green"], 
          $BGColor[ "blue"]); 
    ImageFill($img, 1, 1, $col); 
    } 
 
 // 输出数字 
    if ($FGTrans) 
    { 
    $col = $trans; 
    } 
    else 
    { 
    $col = ImageColorAllocate($img, $FGColor[ "red"], $FGColor[ "green"], 
          $FGColor[ "blue"]); 
    } 

    $PosX = 0; 
    for ($i = 1; $i <= strlen($Counter); $i++) 
    { 
    ImageString($img, $Font, $PosX + 3, 2 + $i % 2,  
    substr($Counter, $i - 1, 1), $col); 
 
    if ($i != 1) 
    { 
    ImageLine($img, $PosX, 0, $PosX, $SizeY + 4, $trans); 
    } 
 
    $PosX += 13; 
    } 
 
 // 输出图片
    ImageGif($img); 
    ImageDestroy($img); 
?>
 
 1#楼  
 
  回复人:jsh204
  注册时间:2004-09-21
  主题/回复:7/37
  积分:494
  等级:★★☆(五级)
  称号:略有小成

   
 1#楼 发表于2005-07-29 01:31:44  评分:3 

很好,借鉴一下!
  页数1/1首页 « 1 » 末页
  发表回复:您还没有登陆,无法发表回复。请先[登陆]

一起PHP技术联盟 主办:一起PHP 联系方式:站长QQ:4304410 QQ群:8423742 20159565 站长博客 E-mail: nqp@nqp.me 执行时间:0.012sec
SimsmaBBS 2008 (v6.0) Developed by 17php.com,Copyright(C)2003-2010 All rights reserved. 副本授权:一起PHP官方专用版