PHP實現的記數器

來源:互聯網
上載者:User

//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);//釋放該影像檔
?>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.