PHP 随机获取可用域名并进行微信和QQ访问检测
以下是实现代码:
$j46 = $conf['j46'];
$j47 = $conf['j47'];
if ($j47 == 1) {
$domains = explode(',', '$j46');
$chosen_domain = $domains[array_rand($domains)]; // 随机获取$j46里的一个值
// 检测微信和QQ能否访问该域名
$wx_check_url = 'https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login';
$qq_check_url = 'https://qzs.qq.com/qzone/v6/portal/proxy.html';
$wx_check_res = http_status_code($wx_check_url, $chosen_domain);
$qq_check_res = http_status_code($qq_check_url, $chosen_domain);
if ($wx_check_res == 200 && $qq_check_res == 200) {
// 可以访问,获取该域名
$usable_domain = $chosen_domain;
} else {
// 不可访问,尝试其他域名
foreach ($domains as $domain) {
if ($domain == $chosen_domain) {
continue;
}
$wx_check_res = http_status_code($wx_check_url, $domain);
$qq_check_res = http_status_code($qq_check_url, $domain);
if ($wx_check_res == 200 && $qq_check_res == 200) {
$usable_domain = $domain;
break;
}
}
if (!isset($usable_domain)) {
// 所有域名都不可用
echo '无可用域名';
exit();
}
}
if (!isset($_COOKIE['redirected'])) {
setcookie('redirected', 'true', time()+10);
$current_url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$new_url = 'http://' . $usable_domain . $_SERVER['REQUEST_URI'];
header('Refresh: 2; URL=$new_url');
echo '<title>跳转中,请稍等 </title>嘿嘿';
exit();
} else {
// 已经跳转过,不再跳转
}
}
/**
* 检测指定域名的指定URL的HTTP状态码
* @param string $url 检测的URL
* @param string $domain 检测的域名
* @return int HTTP状态码
*/
function http_status_code($url, $domain) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host: ' . $domain));
curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return $http_code;
}
代码说明:
- 首先获取变量$j46和$j47的值,如果$j47为1,则执行后续操作。
- 从$j46中获取域名列表,并随机选择一个域名。
- 使用curl库分别向微信和QQ的检测链接发送请求,并获取响应状态码。
- 判断响应状态码是否为200,如果是则表示域名可以访问,否则尝试其他域名。
- 如果所有域名都不可访问,则输出提示信息。
- 如果域名可以访问,则设置cookie并进行跳转。
**注意:**代码中使用了curl库,需要确保服务器已经安装curl扩展。
原文地址: https://www.cveoy.top/t/topic/nPNV 著作权归作者所有。请勿转载和采集!