IP信息卡

[![IP信息卡](https://nkx.moe/card.php?mode=1)](https://nkx.moe/)

访问次数统计

[![访问计数器](https://nkx.moe/counter.php?id=xxx)](https://nkx.moe/)

代码部分

IP信息卡

<?phpheader("Content-Type: image/svg+xml; charset=utf-8");header("Cache-Control: no-cache, no-store, must-revalidate, max-age=0");header("Pragma: no-cache");header("Expires: 0");function getClientIP() {    if (!empty($_SERVER["HTTP_CF_CONNECTING_IP"])) return $_SERVER["HTTP_CF_CONNECTING_IP"];    if (!empty($_SERVER["HTTP_X_FORWARDED_FOR"])) return trim(explode(",", $_SERVER["HTTP_X_FORWARDED_FOR"])[0]);    return $_SERVER["REMOTE_ADDR"] ?? "0.0.0.0";}$ip = getClientIP();// 读取 IPinfo$country = $region = "Unknown";$ch = curl_init();curl_setopt_array($ch, [    CURLOPT_URL => "https://ipinfo.io/{$ip}",    CURLOPT_RETURNTRANSFER => true,    CURLOPT_TIMEOUT => 1,    CURLOPT_CONNECTTIMEOUT => 1,    CURLOPT_SSL_VERIFYPEER => false,]);$res = curl_exec($ch);curl_close($ch);if ($res) {    $data = json_decode($res, true);    if ($data) {        $country = $data["country"] ?? "Unknown";        $region  = $data["region"] ?? "Unknown";    }}// 文本内容$title = "Wow~     Your IP is  ↓";$meta  = "{$country} · {$region}   ·   Powered by NKX";$ipEsc    = htmlspecialchars($ip, ENT_QUOTES | ENT_XML1);$titleEsc = htmlspecialchars($title, ENT_QUOTES | ENT_XML1);$metaEsc  = htmlspecialchars($meta, ENT_QUOTES | ENT_XML1);// IPv6 变小一点$isIPv6 = strpos($ip, ":") !== false;$ipFont = $isIPv6 ? 40 : 72;// 【关键】:自适应 SVG,不再使用固定宽高!echo <<<SVG<svg viewBox="0 0 900 260" width="100%" height="100%"     preserveAspectRatio="xMidYMid meet"     xmlns="http://www.w3.org/2000/svg">  <defs>    <filter id="shadow" x="-20%" y="-20%" width="140%" height="140%">      <feDropShadow dx="0" dy="8" stdDeviation="16" flood-color="#00000022"/>    </filter>  </defs>  <rect x="30" y="25" width="840" height="210" rx="24"        fill="#ffffff" filter="url(#shadow)" />  <style>    text {      font-family: -apple-system, BlinkMacSystemFont, "Microsoft YaHei", "PingFang SC", sans-serif;      letter-spacing: 0.4px;    }    .title { font-size: 36px; font-weight: 800; fill:#111; }    .ip    { font-size: {$ipFont}px; font-weight: 900; fill:#000; }    .meta  { font-size: 26px; font-weight: 600; fill:#555; }  </style>  <text x="450" y="80" text-anchor="middle" class="title">{$titleEsc}</text>  <text x="450" y="155" text-anchor="middle" class="ip">{$ipEsc}</text>  <text x="450" y="210" text-anchor="middle" class="meta">{$metaEsc}</text></svg>SVG;?>

访问次数统计

<?phperror_reporting(E_ALL & ~E_NOTICE);ini_set("display_errors", 0);// SQLite 文件$dbFile = __DIR__ . "/counter.db";$db = new SQLite3($dbFile);$db->exec("CREATE TABLE IF NOT EXISTS counter (    id TEXT PRIMARY KEY,    value INTEGER NOT NULL)");$id = isset($_GET["id"]) ? preg_replace("/[^a-zA-Z0-9_\-]/", "_", $_GET["id"]) : "default";// 读取当前值$stmt = $db->prepare("SELECT value FROM counter WHERE id = :id");$stmt->bindValue(":id", $id, SQLITE3_TEXT);$res = $stmt->execute()->fetchArray();$count = $res ? intval($res["value"]) : 0;// +1$count++;$stmt = $db->prepare("REPLACE INTO counter (id, value) VALUES (:id, :v)");$stmt->bindValue(":id", $id, SQLITE3_TEXT);$stmt->bindValue(":v", $count, SQLITE3_INTEGER);$stmt->execute();// 显示 7 位$digits = str_pad($count, 7, "0", STR_PAD_LEFT);// 5×7 像素点阵数字$matrix = [    "0" => ["01110","10001","10011","10101","11001","10001","01110"],    "1" => ["00100","01100","00100","00100","00100","00100","01110"],    "2" => ["01110","10001","00001","00010","00100","01000","11111"],    "3" => ["11110","00001","00001","01110","00001","00001","11110"],    "4" => ["00010","00110","01010","10010","11111","00010","00010"],    "5" => ["11111","10000","11110","00001","00001","10001","01110"],    "6" => ["01110","10000","11110","10001","10001","10001","01110"],    "7" => ["11111","00001","00010","00100","01000","10000","10000"],    "8" => ["01110","10001","10001","01110","10001","10001","01110"],    "9" => ["01110","10001","10001","01111","00001","00010","11100"],];// UI 参数$cellW = 70;      // 单格宽$cellH = 90;      // 单格高$gap   = 10;      // 间距$pixel = 10;      // 单个像素点大小(影响数字大小)$w = 7 * $cellW + 8 * $gap;$h = $cellH + 2 * $gap;$img = imagecreatetruecolor($w, $h);// 白底$white = imagecolorallocate($img, 255, 255, 255);$black = imagecolorallocate($img, 0, 0, 0);imagefill($img, 0, 0, $white);// 绘制数字框for ($i = 0; $i < 7; $i++) {    $d = $digits[$i];    $x1 = $gap + $i * ($cellW + $gap);    $y1 = $gap;    $x2 = $x1 + $cellW;    $y2 = $y1 + $cellH;    // 黑框    imagerectangle($img, $x1, $y1, $x2, $y2, $black);    // 点阵数据    $digit = $matrix[$d];    // 点阵尺寸    $dotW = 5 * $pixel;    $dotH = 7 * $pixel;    // 居中    $ox = $x1 + ($cellW - $dotW) / 2;    $oy = $y1 + ($cellH - $dotH) / 2;    // 绘制像素数字    for ($r = 0; $r < 7; $r++) {        for ($c = 0; $c < 5; $c++) {            if ($digit[$r][$c] == "1") {                imagefilledrectangle(                    $img,                    $ox + $c * $pixel,                    $oy + $r * $pixel,                    $ox + ($c + 1) * $pixel - 1,                    $oy + ($r + 1) * $pixel - 1,                    $black                );            }        }    }}// 输出 PNGheader("Content-Type: image/png");header("Cache-Control: no-cache, no-store, must-revalidate");imagepng($img);imagedestroy($img);

需要配合msyh.ttf 微软雅黑字体 放在网站根目录配合使用
使用PHP+SVG+SQLite组合