惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
人人都是产品经理
人人都是产品经理
Cisco Talos Blog
Cisco Talos Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
V
V2EX
博客园 - 三生石上(FineUI控件)
Martin Fowler
Martin Fowler
WordPress大学
WordPress大学
D
Docker
S
SegmentFault 最新的问题
博客园 - 聂微东
美团技术团队
Apple Machine Learning Research
Apple Machine Learning Research
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Last Week in AI
Last Week in AI
M
MIT News - Artificial intelligence
F
Fortinet All Blogs
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
The GitHub Blog
The GitHub Blog
GbyAI
GbyAI
L
LangChain Blog
Vercel News
Vercel News
博客园 - 叶小钗
MongoDB | Blog
MongoDB | Blog
Stack Overflow Blog
Stack Overflow Blog
H
Help Net Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The Cloudflare Blog
Engineering at Meta
Engineering at Meta
T
Threat Research - Cisco Blogs
T
Threatpost
Scott Helme
Scott Helme
T
Tailwind CSS Blog
Latest news
Latest news
Stack Overflow Blog
Stack Overflow Blog
Blog — PlanetScale
Blog — PlanetScale
The Register - Security
The Register - Security
罗磊的独立博客
P
Proofpoint News Feed
腾讯CDC
S
Schneier on Security
雷峰网
雷峰网
A
About on SuperTechFans
T
Tenable Blog
F
Full Disclosure
Cyberwarzone
Cyberwarzone
博客园_首页
有赞技术团队
有赞技术团队
K
Kaspersky official blog

文章列表

分享会回馈,时间会回响。 - 哈喽!林墨白 2025年对我的总结 - 哈喽!林墨白 展示BlogsClub一周年纪念币 - 哈喽!林墨白 已获得iQOO IoT新品众测资格 - 哈喽!林墨白 享有XYZ顶级域名仅需4.77元/年? - 哈喽!林墨白 推荐几个好用到爆的手机APP - 哈喽!林墨白 0成本在Vercel部署Umami网站分析工具 - 哈喽!林墨白 已抢到BlogsClub一周年纪念币 - 哈喽!林墨白 分享我的现在与未来 - 哈喽!林墨白 使用开源项目AutoTask实现逃离广告 - 哈喽!林墨白 记录博客一坤年,启用新域名 - 哈喽!林墨白 利用猫抓下载直播回放 - 哈喽!林墨白 欢迎加入BlogsClub博客俱乐部 「信息泄露」学姐“慕名”加我? - 哈喽!林墨白 具体使用unlock-music项目解锁各大音乐平台的加密格式 - 哈喽!林墨白 成功解锁QQ音乐mgg、mflac加密格式过程 - 哈喽!林墨白 《无处遁形:全网公敌》主线已通关 - 哈喽!林墨白 Brave「Lv-1.5.6」- Typecho情侣主题魔改版 - 哈喽!林墨白 对现代爱情的看法 - 哈喽!林墨白 幻想十年后自己的爱情 - 哈喽!林墨白 喜欢樱花🌸吗?加个飘落特效吧 - 哈喽!林墨白 在Woiden白嫖理论上的永久NAT VPS - 哈喽!林墨白 《查理九世》——谨以此书,纪念我的童年 - 哈喽!林墨白 二次元个人主页引导页(林墨白优化版) - 哈喽!林墨白 注册Telegram(电报)收不到短信?手把手教你如何搞定 - 哈喽!林墨白 免费白嫖Dewlance永久虚拟主机 - 哈喽!林墨白 2024年快到了,加个灯笼和飘雪特效吧 - 哈喽!林墨白 搭建基于UptimeRobot接口的站点监测网站 - 哈喽!林墨白 本人真实事件:把一个骗我的骗子给骗了 - 哈喽!林墨白
给BearSimple主题加一个友链随机跳转 - 哈喽!林墨白
林墨白 · 2024-05-07 · via

林墨白 2024年 05月 07 日

话说

因为BearSimple主题功能太多,昨天才发现有一个幻灯片的功能没使用,但是我好像没有什么东西可以放上去,思来想去好像可以做一个友链随机跳转。
本站随机跳转链接(点击幻灯片一样的效果):https://blog.lmb.blue/other/links-go.php

实现

BearSimple有独立的友链数据库表,所以可以直接用php连接数据库获取友链链接,进行随机访问。中间过渡页面是使用开往的plain简洁版页面(作者是:Lifeni)
具体代码如下:

<?php
// 数据库配置
$config = [
    'host'     => getenv('DB_HOST') ?: '127.0.0.1',
    'user'     => getenv('DB_USER') ?: 'lmb520',
    'password' => getenv('DB_PASS') ?: 'lmb520lmb520',
    'dbname'   => getenv('DB_NAME') ?: 'lmb520'
];

// 初始化变量
$hasValidLink = false;
$randomLink = ['url' => '', 'name' => ''];
$errorMessage = '';

// 连接数据库并获取随机友链
try {
    // 创建连接(启用异常模式)
    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
    $conn = new mysqli($config['host'], $config['user'], $config['password'], $config['dbname']);
    $conn->set_charset('utf8mb4');

    // 查询所有已批准的友链
    $stmt = $conn->prepare("SELECT friendurl, friendname FROM typecho_bscore_friendlinks WHERE status = 'approved'");
    $stmt->execute();
    $result = $stmt->get_result();

    $links = [];
    while ($row = $result->fetch_assoc()) {
        $url = $row['friendurl'];
        $name = $row['friendname'];

        // 严格验证URL:必须包含协议且为http/https,防止危险协议
        if (filter_var($url, FILTER_VALIDATE_URL)) {
            $scheme = parse_url($url, PHP_URL_SCHEME);
            if ($scheme === 'http' || $scheme === 'https') {
                $links[] = ['url' => $url, 'name' => $name];
            }
        }
    }

    $stmt->close();
    $conn->close();

    // 随机选取一个有效友链
    if (!empty($links)) {
        $hasValidLink = true;
        $randomLink = $links[array_rand($links)];
    } else {
        $errorMessage = '暂无可用的友情链接,请稍后再试或申请加入友链。';
    }
} catch (mysqli_sql_exception $e) {
    // 数据库错误:记录日志,但不暴露敏感信息给用户
    error_log('Database error in random link page: ' . $e->getMessage());
    $errorMessage = '系统繁忙,请稍后再试。';
} catch (Exception $e) {
    error_log('Unexpected error: ' . $e->getMessage());
    $errorMessage = '发生未知错误,请联系管理员。';
}

// 准备跳转相关数据(安全转义)
$redirectUrl = $hasValidLink ? $randomLink['url'] : '';
$redirectName = $hasValidLink ? htmlspecialchars($randomLink['name'], ENT_QUOTES, 'UTF-8') : '';
$jsonUrl = json_encode($redirectUrl, JSON_UNESCAPED_SLASHES | JSON_HEX_TAG);
?>
<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="沉墨满纸,一笑若白。——林墨白">
    <title>星际穿梭中 - 哈喽!林墨白</title>
    <link rel="shortcut icon" href="https://files.blog.lmb520.cn/assets/img/logo/pink-black.png">
    <link rel="stylesheet" href="https://files.blog.lmb520.cn/assets/css/links-go.css">
    <?php if ($hasValidLink): ?>
    <script>
        // 安全跳转:3秒后自动跳转
        setTimeout(function() {
            window.location.replace(<?php echo $jsonUrl; ?>);
        }, 3000);
    </script>
    <?php endif; ?>
</head>
<body>
<main>
    <h1>
        <span>哈喽!林墨白</span>
        <?php if ($hasValidLink): ?>
        <span>正在驶入“<?php echo $redirectName; ?>”星云</span>
        <?php else: ?>
        <span>星云导航暂时中断</span>
        <?php endif; ?>
    </h1>

    <?php if ($hasValidLink): ?>
    <!-- Material Design 风格加载动画 -->
    <svg class="spinner" width="32px" height="32px" viewBox="0 0 66 66" xmlns="http://www.w3.org/2000/svg">
        <circle class="path" fill="none" stroke-width="6" stroke-linecap="round" cx="33" cy="33" r="30"></circle>
    </svg>
    <?php else: ?>
    <div class="error-message" style="margin: 2rem auto; text-align: center; color: #ff6b6b;">
        <p><?php echo htmlspecialchars($errorMessage, ENT_QUOTES, 'UTF-8'); ?></p>
        <p>您可以选择返回首页或申请加入友链。</p>
    </div>
    <?php endif; ?>
</main>

<footer>
    <a href="https://blog.lmb520.cn/" target="_blank" rel="noopener noreferrer">返回首页</a>
    <a href="https://blog.lmb520.cn/links.html" target="_blank" rel="noopener noreferrer">加入友链</a>
    <a href="https://github.com/Lifeni" target="_blank" rel="noopener noreferrer">模板作者</a>
    <span></span>
    <a href="https://beian.miit.gov.cn/" target="_blank" rel="noopener noreferrer">蜀ICP备-2023019525号-1</a>
</footer>
</body>
</html>

里面的css调用代码可以将

<link rel="stylesheet" href="https://files.blog.lmb520.cn/assets/css/links-go.css">

改为

<style>
      /* 色板:https://www.materialui.co/colors */
      * {
        box-sizing: border-box;
        margin: 0;
        padding: 0;
      }

      body {
        font-family: Inter, -apple-system, HarmonyOS Sans SC, MiSans,
          Source Han Sans SC, Noto Sans SC, system-ui, Roboto, emoji, sans-serif;
        color: black;
        background: white;
        font-weight: 400;
        font-size: 1rem;
        min-height: 100vh;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
      }

      main {
        padding: 2rem;
        flex: 1;
        display: flex;
        gap: 2rem;
        flex-direction: column;
        align-items: center;
        justify-content: center;
      }

      main h1 {
        margin-top: 4rem;
        font-size: 1.75rem;
        font-weight: 700;
        line-height: 1.75;
        text-align: center;
      }

      main h1 span {
        white-space: nowrap;
      }

      footer {
        width: 100%;
        display: flex;
        flex-wrap: wrap;
        gap: 0.5rem 1.5rem;
        align-items: center;
        justify-content: center;
        padding: 1.75rem 2rem;
      }

      footer a {
        font-size: 0.875rem;
        text-decoration: none;
        color: #757575;
        transition: all 0.2s;
      }

      footer span {
        flex: 1;
      }

      footer a:hover {
        color: #3f51b5;
        text-decoration: underline;
        text-underline-offset: 0.25rem;
      }

      @media (prefers-color-scheme: dark) {
        body {
          color: #eeeeee;
          background: #212121;
        }

        footer a {
          color: #bdbdbd;
          transition: all 0.2s;
        }

        footer a:hover {
          color: #5c6bc0;
        }
      }

      @media screen and (max-width: 768px) {
        main {
          margin-top: 4.5rem;
        }

        main h1 {
          font-size: 1.5rem;
        }

        footer {
          gap: 0.5rem 1rem;
        }

        footer span {
          display: none;
        }
      }

      /* Material Design 风格的加载动画 https://codepen.io/mrrocks/pen/ExLovj  */
      .spinner {
        -webkit-animation: rotator 1.4s linear infinite;
        animation: rotator 1.4s linear infinite;
      }

      @-webkit-keyframes rotator {
        0% {
          transform: rotate(0deg);
        }
        100% {
          transform: rotate(270deg);
        }
      }

      @keyframes rotator {
        0% {
          transform: rotate(0deg);
        }
        100% {
          transform: rotate(270deg);
        }
      }

      .path {
        stroke-dasharray: 187;
        stroke-dashoffset: 0;
        transform-origin: center;
        -webkit-animation: dash 1.4s ease-in-out infinite,
          colors 5.6s ease-in-out infinite;
        animation: dash 1.4s ease-in-out infinite,
          colors 5.6s ease-in-out infinite;
      }

      @-webkit-keyframes colors {
        0% {
          stroke: #4285f4;
        }
        25% {
          stroke: #de3e35;
        }
        50% {
          stroke: #f7c223;
        }
        75% {
          stroke: #1b9a59;
        }
        100% {
          stroke: #4285f4;
        }
      }

      @keyframes colors {
        0% {
          stroke: #4285f4;
        }
        25% {
          stroke: #de3e35;
        }
        50% {
          stroke: #f7c223;
        }
        75% {
          stroke: #1b9a59;
        }
        100% {
          stroke: #4285f4;
        }
      }
      @-webkit-keyframes dash {
        0% {
          stroke-dashoffset: 187;
        }
        50% {
          stroke-dashoffset: 46.75;
          transform: rotate(135deg);
        }
        100% {
          stroke-dashoffset: 187;
          transform: rotate(450deg);
        }
      }
      @keyframes dash {
        0% {
          stroke-dashoffset: 187;
        }
        50% {
          stroke-dashoffset: 46.75;
          transform: rotate(135deg);
        }
        100% {
          stroke-dashoffset: 187;
          transform: rotate(450deg);
        }
      }
</style>

因为有可能我会移动样式文件,但是我移动后也会及时更新除非我忘了,所以建议自己放在代码里或者找个地方储存调用

注意

代码里面有很多需要自己按情况修改,比如数据库信息、网页内容、网站图标等等

黑暗模式

全球语言