WordPress 外链跳转插件——安全增强优化

前言

之前看到广然大佬的一篇文章适配 WPOPT 插件的外链跳转模板

原文地址:https://www.grbj.cn/1106.html ,自己也试了一下,装上以后点跳转有一些提示:

按照果核的官方文档:https://www.yuque.com/applek/wpopt/link (也问了豆包☺)

详细的安装步骤就不讲了,广然大佬和果核的文章里都有,我自己就加了个5秒跳转的,加完后浏览器会弹窗

折腾了半天原来是`window.open()` 的弹窗,浏览器安全策略会自动拦截弹出的新标签页,就出现了截图里【已拦截弹出式窗口】提示。

改成倒计时结束当前页面直接跳转,不新开窗口,就不会触发浏览器弹窗拦截。O(∩_∩)O~~

点击【继续访问】按钮依旧可以新开标签,都是自己瞎折腾。

问题原因

变量没有获取 GET 参数:原代码直接写 global $url;,不会自动读取地址栏 ?url=https://xxx 参数,导致$url为空。

多余的$link变量:代码 <?php echo htmlspecialchars($link); ?><?php echo $url ?>$link不存在,拼接错误。

缺少空值判断:如果没有 url 参数,页面直接报错。

XSS 安全问题:输出 URL 没有正确转义。

样式

代码

<?php
/**
 * name:wlmb
 * Template Name: wlmb 外链中转页
 */
// 获取base64加密后的参数
$base64_str = isset($_GET['url']) ? $_GET['url'] : '';
$url = '';
if(!empty($base64_str)){
    // base64解密
    $decoded = base64_decode($base64_str);
    // 校验解密后的链接是否以http/https开头
    if(preg_match('/^https?:\/\//i', $decoded)){
        $url = $decoded;
    }
}
?>
<!doctype html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="icon" href="https://000nw.com/wp-content/uploads/2026/08/3ba11aecf8540f53e700afd0879b3e68.webp" type="image/x-icon"/>
    <title>电脑技术吧 - 安全中心</title>
<style>
    body { margin: 0; font-family: 'PingFangSC', sans-serif; background-color: #EFF4FA; }
.go-wild-box { display: flex; justify-content: center; align-items: center; height: 100vh; }
.go-wild-container { width: 770px; height: 330px; max-width: 90%; padding: 90px 15px; background-color: #fff; border-radius: 12px; box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1); text-align: center; }
.logo-img { width: 120px; height: auto; }
.tips-title { margin: 20px 0; font-size: 22px; color: #2a3c59; font-weight: 500; }
.address { margin-bottom: 20px; padding: 15px; background-color: #EFF4FA; border-radius: 8px; color: #2a3c59; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; width: 80%; max-width: 600px; margin: 0 auto; }
.tips-subtitle { font-size: 14px; color: #2a3c59; margin-bottom: 20px;margin-top: 20px; }
.countdown-text {
    font-size:16px;
    color:#07C160;
    margin-bottom:20px;
}
.btn-groups { display: flex; justify-content: center; gap: 10px; margin-top: 30px; }
.ant-btn { width: 152px; height: 44px; line-height: 44px; border-radius: 20px; border: none; cursor: pointer; font-size: 14px; transition: all 0.3s ease; }
.ant-btn-primary { background: linear-gradient(152deg, #07C160 0%, #07c183 100%); color: #fff; }
.ant-btn-default { background-color: #fff; color: #2a3c59; border: 1px solid #ccc; }
.ant-btn-default:hover { background-color: #fff; border-color: #07C160; color: #07C160; }
</style>
</head>

<body>
  <div class="go-wild-box">
    <div class="go-wild-container">
      <a href="https://www.grbj.cn/">
        <img alt="电脑技术吧" src="https://000nw.com/wp-content/uploads/2026/08/3ba11aecf8540f53e700afd0879b3e68.webp" class="logo-img" />
      </a>
      <div class="tips-title">您即将从电脑技术吧跳转到外部网站</div>
      <div class="address"><?php echo htmlspecialchars($url, ENT_QUOTES, 'UTF-8'); ?></div>
      <div class="tips-subtitle">请注意您的账号和财产安全</div>
      <?php if(!empty($url)): ?>
        <div class="countdown-text">
            <span id="timer">5</span> 秒后自动跳转,也可点击下方按钮立即访问
        </div>
        <div class="btn-groups">
          <button onclick="stopTimer(); try { window.close(); } catch(e) { alert('请手动关闭此页面'); }" type="button" class="ant-btn ant-btn-default">取消访问</button>
          <a href="<?php echo htmlspecialchars($url, ENT_QUOTES, 'UTF-8'); ?>" rel="nofollow noopener noreferrer" target="_blank">
            <button type="button" class="ant-btn ant-btn-primary">继续访问</button>
          </a>
        </div>
      <?php else: ?>
        <div class="countdown-text" style="color:red;">链接参数无效,无法跳转</div>
        <div class="btn-groups">
          <button disabled type="button" class="ant-btn ant-btn-primary">链接无效</button>
        </div>
      <?php endif; ?>
    </div>
  </div>

<?php if(!empty($url)): ?>
<script>
let count = 5;
let timer = null;
const timerEl = document.getElementById('timer');
const targetUrl = "<?php echo htmlspecialchars($url, ENT_QUOTES, 'UTF-8'); ?>";

timer = setInterval(function(){
    count--;
    timerEl.innerText = count;
    if(count <= 0){
        clearInterval(timer);
        // 修改这里:location.href 当前窗口跳转,不会触发浏览器弹窗拦截
        location.href = targetUrl;
    }
},1000);

function stopTimer(){
    if(timer){
        clearInterval(timer);
    }
}
</script>
<?php endif; ?>
</body>
</html>

喜欢的小伙伴也可以折腾折腾。有什么问题群里多交流哈~

 

上一篇 WPS2026 专业增强版下载:集成 VBA 组件,一键安装永久激活教程
下一篇 护眼小工具下载:多显示器独立调节,4 种场景模式 + 定时休息提醒