dongmeng1868 2014-08-31 22:21
浏览 55

$ _SERVER ['HTTP_USER_AGENT']导致查询UI无法加载

I'm having a strange problem. When I use this code to detect a mobile device, its causing my query dialog to stop working (on iPhone only - not tested on other mobile devices), but it works fine in my browser. Is there a way around this?

<?php

  $iphone = strpos($_SERVER['HTTP_USER_AGENT'],"iPhone");
  $android = strpos($_SERVER['HTTP_USER_AGENT'],"Android");
  $palmpre = strpos($_SERVER['HTTP_USER_AGENT'],"webOS");
  $berry = strpos($_SERVER['HTTP_USER_AGENT'],"BlackBerry");
  $ipod = strpos($_SERVER['HTTP_USER_AGENT'],"iPod");
  $ipad = strpos($_SERVER['HTTP_USER_AGENT'],"iPad");

  if ($iphone || $android || $palmpre || $ipod || $berry || $ipad == true) 
     { 
       //do something
     } else { ?>
        <div id="scrollerWrapper">
          <div id="left_shopping_cart">
            <?php include($_SERVER['DOCUMENT_ROOT']."/scripts/cart_side_products.php"); ?>
          </div>
        </div><!-- end#scrollerWrapper-->
        <?php 
      } ?>

If I take out the first block, query works fine on my iPhone.

  • 写回答

2条回答 默认 最新

  • duanmangxie7131 2014-08-31 22:41
    关注

    What's happening is that strpos() is returning an integer but what you're asking it to return is a boolean value.

    From: http://php.net/manual/en/function.strpos.php

    strpos

    (PHP 4, PHP 5) strpos — Find the position of the first occurrence of a substring in a string

    Description

    mixed strpos ( string $haystack , mixed $needle [, int $offset = 0 ] ) Find the numeric position of the first occurrence of needle in the haystack string.

    Also, for this part:

    if ($iphone || $android || $palmpre || $ipod || $berry || $ipad == true) 
    

    you have some syntax errors in your if conditions. These should written as:

    if ($iphone == [target value] || $android == [target value] || $palmpre == [target value] ... // and so on for each`
    

    There's also something special to note about strpos(): It will return 0 (i.e. int) if the search value is located at the starting position in the string, and it will return FALSE (i.e. boolean) if the target substring is not found in the string at all.

    Basically, the correct code you are looking for would be something like this:

    <?php
    $iphone = strpos($_SERVER['HTTP_USER_AGENT'],"iPhone");
    $android = strpos($_SERVER['HTTP_USER_AGENT'],"Android");
    $palmpre = strpos($_SERVER['HTTP_USER_AGENT'],"webOS");
    $berry = strpos($_SERVER['HTTP_USER_AGENT'],"BlackBerry");
    $ipod = strpos($_SERVER['HTTP_USER_AGENT'],"iPod");
    $ipad = strpos($_SERVER['HTTP_USER_AGENT'],"iPad");
    
    if ( $iphone !== FALSE 
         || $android !== FALSE 
         || $palmpre !== FALSE 
         || $ipod !== FALSE 
         || $berry !== FALSE 
         || $ipad !== FALSE ) { 
    
           //do something
    
    } else {
        ?>
        <div id="scrollerWrapper">
            <div id="left_shopping_cart">
                <?php
                include($_SERVER['DOCUMENT_ROOT']."/scripts/cart_side_products.php"); 
                ?>
            </div>
        </div>
        <!-- end#scrollerWrapper-->
    <?php 
    } 
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 Arcgis相交分析无法绘制一个或多个图形