論壇簽名檔、博客側邊欄、聯盟推廣圖:一張動態 PNG 圖片,實時顯示訪客 IP、反解主機名、本站訪問統計、自定義提示語、廣告圖片。
Counter 文本文件存儲 24 小時分時訪問量,每次請求遞增當前小時計數。getenv('REMOTE_ADDR') 取 IP,gethostbyaddr() 反解主機名。getdate() 判斷周末/工作日,輸出不同提示。header("Content-type: image/PNG") + ImagePNG()。<?php
header("Content-type: image/PNG");
// === 1. 簡易文件計數器(24小時分時統計) ===
$Visited = array();
if (!file_exists("Counter")) {
for ($i = 0; $i < 24; $i++) $Visited[$i] = 0;
$fp = fopen("Counter", "w");
fputs($fp, implode(",", $Visited));
fclose($fp);
} else {
$fp = fopen("Counter", "r");
$Visited = explode(",", trim(fgets($fp, 100)));
fclose($fp);
}
$hour = date("G");
$Visited[$hour] = intval($Visited[$hour]) + 1;
$fp = fopen("Counter", "w");
fputs($fp, implode(",", $Visited));
fclose($fp);
$sum = array_sum($Visited);
$max = max($Visited);
// === 2. 訪客信息 ===
$ip = getenv('REMOTE_ADDR');
$host = gethostbyaddr($ip);
$today = getdate();
$wday = $today['wday'];
$tips = ($wday == 0 || $wday == 6) ? "enjoy the wonderful weekend!" : "you'd better go to school.";
$week = array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
// === 3. GD 繪圖 ===
$im = imagecreate(400, 100);
$black = ImageColorAllocate($im, 0, 0, 0);
$white = ImageColorAllocate($im, 255, 255, 255);
$red = ImageColorAllocate($im, 255, 0, 0);
$blue = ImageColorAllocate($im, 0, 0, 255);
// 背景填充
imagefill($im, 0, 0, $black);
imagefilledrectangle($im, 1, 1, 398, 98, $white);
// 畫筆風格虛線(白白白白白白白白白紅)
$style = array($white,$white,$white,$white,$white,$white,$white,$white,$white,$red);
imagesetstyle($im, $style);
// 外部圖片作畫筆/貼圖
$brush = imagecreatefrompng("https://example.com/smile.png"); // 表情
$brush1 = imagecreatefrompng("https://example.com/ads.png"); // 廣告
$w2 = imagecolorallocate($brush, 255, 255, 255);
imagecolortransparent($brush, $w2); // 設白色透明
imagesetbrush($im, $brush);
imageline($im, 350, 20, 360, 20, IMG_COLOR_STYLEDBRUSHED); // 畫筆效果線
imagecopy($im, $brush1, 270, 40, 0, 0, 119, 48); // 貼廣告圖
// 輸出文本
$font = 5; $mx = 20; $my = 20;
imagestring($im, $font, $mx, $my+20, "Your computer's named: {$host}", $red);
imagestring($im, $font, $mx, $my, "Your IP Address is: {$ip}", $red);
imagestring($im, 2, 2, 2, "Today is {$week[$wday]} and {$tips}", $blue);
imagestring($im, 3, 10, $my+40, "Total Refresh Times Today: {$sum}", $blue);
// 輸出
ImagePNG($im);
ImageDestroy($im);
?>
Counter 文件(建議改為 SQLite/Redis 避免併發丟計數)。smile.png、ads.png 需可公開訪問,建議本地化存放。gethostbyaddr 較慢),高流量下應緩存生成圖片或異步更新。HINCRBY counter:hourly {hour} 1。imagecreatetruecolor + imagettftext (TTF 字體) 替代內置點陣字體。Intervention/Image 或 GdText 簡化繪圖代碼。Cache-Control: max-age=60 允許瀏覽器/CDN 緩存 1 分鐘。?uid=xxx 參數生成個性化簽名圖,而非全站通用。