首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 綜合知識

PHP 動態生成簽名圖片:顯示訪客 IP、主機名、計數器、廣告圖

     編輯:PHP 綜合知識

應用場景

論壇簽名檔、博客側邊欄、聯盟推廣圖:一張動態 PNG 圖片,實時顯示訪客 IP、反解主機名、本站訪問統計、自定義提示語、廣告圖片。

核心功能模塊

  1. 文件計數器:Counter 文本文件存儲 24 小時分時訪問量,每次請求遞增當前小時計數。
  2. 訪客信息獲取:getenv('REMOTE_ADDR') 取 IP,gethostbyaddr() 反解主機名。
  3. 時間與問候語:getdate() 判斷周末/工作日,輸出不同提示。
  4. GD 繪圖:創建 400x100 畫布,繪製背景、邊框、畫筆風格線條、貼圖(廣告/表情)、輸出文本。
  5. 輸出 PNG: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 需可公開訪問,建議本地化存放。
  • 性能:每次請求都執行 GD 繪圖、DNS 反解(gethostbyaddr 較慢),高流量下應緩存生成圖片或異步更新。
  • 安全:直接暴露訪客 IP/主機名屬隱私敏感,實際使用請脫敏或征得同意。

現代化改造建議

  1. 計數器遷移至 Redis HINCRBY counter:hourly {hour} 1。
  2. 使用 imagecreatetruecolor + imagettftext (TTF 字體) 替代內置點陣字體。
  3. 引入 Intervention/Image 或 GdText 簡化繪圖代碼。
  4. 輸出 Cache-Control: max-age=60 允許瀏覽器/CDN 緩存 1 分鐘。
  5. 通過 ?uid=xxx 參數生成個性化簽名圖,而非全站通用。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved