在我的网站上用 php 检测到浏览器后如何阻止浏览器?

How to Block a browser after detecting it with php on my website?

<?php
function get_the_browser()    //browser detection
{
if(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false)
   return 'Internet explorer';
 elseif(strpos($_SERVER['HTTP_USER_AGENT'], 'Trident') !== false)
    return 'Internet explorer';
 elseif(strpos($_SERVER['HTTP_USER_AGENT'], 'Firefox') !== false)
   return 'Mozilla Firefox';
 elseif(strpos($_SERVER['HTTP_USER_AGENT'], 'Chrome') !== false)   //the only browser that i want to allow
   return 'Google Chrome';
 elseif(strpos($_SERVER['HTTP_USER_AGENT'], 'Opera Mini') !== false)
   return "Opera Mini";
 elseif(strpos($_SERVER['HTTP_USER_AGENT'], 'Opera') !== false)
   return "Opera";
 elseif(strpos($_SERVER['HTTP_USER_AGENT'], 'Safari') !== false)
   return "Safari";
 else
   return 'Other';
}
?>

我想知道我是否可以在 php 中使用此代码阻止浏览器,你能帮我吗?

我用这个解决了

    <?php

class Browser
{
    public static function detect()
    {
        $userAgent = strtolower($_SERVER['HTTP_USER_AGENT']);
        if ((substr($_SERVER['HTTP_USER_AGENT'], 0, 6) == "Opera/") || (strpos($userAgent, 'opera')) != false) {
            $name = 'opera';
        } elseif ((strpos($userAgent, 'chrome')) != false) {
            $name = 'chrome';
        } elseif ((strpos($userAgent, 'safari')) != false && (strpos($userAgent, 'chrome')) == false && (strpos($userAgent, 'chrome')) == false) {
            $name = 'safari';
        } elseif (preg_match('/msie/', $userAgent)) {
            $name = 'msie';
        } elseif ((strpos($userAgent, 'firefox')) != false) {
            $name = 'firefox';
        } else {
            $name = 'unrecognized';
        }
        if (preg_match('/.+(?:me|ox|it|ra|ie)[\/: ]([\d.]+)/', $userAgent, $matches) && $browser['name'] == 'safari') {
            $version = $matches[1];
        }
        if (preg_match('/.+(?:me|ox|it|on|ra|ie)[\/: ]([\d.]+)/', $userAgent, $matches) && $browser['name'] != 'safari') {
            $version = $matches[1];
        } else {
            $version = 'unknown';
        }

        return array(
            'name' => $name,
            'version' => $version,
        );
    }
}

$browser = Browser::detect();
echo 'You browser is ' . $browser['name'] . ' version ' . $browser['version'];
echo "<br />";
?>

我在 this thread 中找到了解决方案。

此外,您可以添加条件并使用 die()。