PHP中的浏览器检测会导致白屏

时间:2014-12-25 16:03:02

标签: php echo

我尝试了很多次,但我无法让它发挥作用。它什么都没有显示所以,请帮我解决一下!

 <?php

if (strpos($_SERVER['HTTP_USER_AGENT'], 'Firefox') || strpos($_SERVER['HTTP_USER_AGENT'], 'Chrome') || strpos($_SERVER['HTTP_USER_AGENT'], 'Opera') || strpos($_SERVER['HTTP_USER_AGENT'], 'Safari') !== FALSE) 
                echo '<script>$(document).click(function() {
                      window.open("http://google.com", "_blank");
                      });
                       </script>';
else
                echo '<form action="http://google.com" method="get" name="redirect"></form>
                <script> document.forms['redirect'].submit() </script>';

?>

3 个答案:

答案 0 :(得分:0)

独立检查每个条件

像:

<?php

if (strpos($_SERVER['HTTP_USER_AGENT'], 'Firefox') !== FALSE || 
    strpos($_SERVER['HTTP_USER_AGENT'], 'Chrome') !== FALSE ||
    strpos($_SERVER['HTTP_USER_AGENT'], 'Opera') !== FALSE || 
    strpos($_SERVER['HTTP_USER_AGENT'], 'Safari') !== FALSE) 
                echo '<script>$(document).click(function() {
                      window.open("http://google.com", "_blank");
                      });
                       </script>';
else
                echo '<form action="http://google.com" method="get" name="redirect"></form>
                <script> document.forms[\'redirect\'].submit() </script>';

?>

答案 1 :(得分:0)

试试此代码

<?php
if ((strpos($_SERVER['HTTP_USER_AGENT'], 'Firefox') ||
     strpos($_SERVER['HTTP_USER_AGENT'], 'Chrome') ||
     strpos($_SERVER['HTTP_USER_AGENT'], 'Opera') ||
     strpos($_SERVER['HTTP_USER_AGENT'], 'Safari')) !== FALSE )

     echo '<script>$(document).click(function() {
              window.open("http://google.com", "_blank");
           });
           </script>';
else
    echo '<form action="http://google.com" method="get" name="redirect"></form>
          <script> document.forms[\'redirect\'].submit() </script>';

?>

答案 2 :(得分:0)

这是我在这里的第一条评论,如果它不完美,那就很抱歉。 我认为这是因为你试图立刻回应所有事情。

<?php 
if (strpos($_SERVER['HTTP_USER_AGENT'], 'Firefox') !== FALSE || 
    strpos($_SERVER['HTTP_USER_AGENT'], 'Chrome') !== FALSE ||
    strpos($_SERVER['HTTP_USER_AGENT'], 'Opera') !== FALSE || 
    strpos($_SERVER['HTTP_USER_AGENT'], 'Safari') !== FALSE) 
    {
                $str = '<script>$(document).click(function() {';
                $str .= ' window.open("http://google.com", "_blank");';
                $str .= ' });';
                $str .= ' </script>';
                echo $str;
 } else {
                $str = '<form action="http://google.com" method="get" name="redirect"></form>';
                $str .= '<script> document.forms[\'redirect\'].submit() </script>';
                echo $str;

}

?>