doumu4032 2018-08-25 19:07
浏览 335
已采纳

未定义的索引:HTTP_USER_AGENT

I am trying to perform an action when a specific user agent visits a link.

So i have my code like this:

//if browser is not Mozilla/4.2, then do something. 
//but if its Mozilla/4.2, do another thing.

if(strlen(strstr($_SERVER['HTTP_USER_AGENT'],"Mozilla/4.2")) <= 0 ){ 
    // Do something
} else {
    //Else do another thing code follows.
}

The above code is working but it keeps giving this warning in the error log "Undefined index: HTTP_USER_AGENT"

The solution i saw used pregmatch, but am targeting only a single user agent.

Any help will be appreciated.

  • 写回答

1条回答 默认 最新

  • duanjiu1003 2018-08-25 19:16
    关注

    You simply need to check the existence of the index (HTTP_USER_AGENT) on $_SERVER and if it's not set then set it to empty string.

    This can be achieved by doing;

    $userAgent = ! empty($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
    
    if(strlen(strstr($userAgent,"Mozilla/4.2")) <= 0 ) { 
        // Do something
    } else { 
        // Do something else.
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 学校优化算法sbo和蚁群算法怎么结合
  • ¥21 matlab怎么求时域信号的二阶导数
  • ¥15 判断两个表是否完全相同
  • ¥15 java map类型数据格式,如何快速通过前缀匹配元素
  • ¥15 stc12c5a60s2、QMC5883L、LCD1602组合测量磁场所需程序
  • ¥20 Win11测试yolov4,“找不到nvcuda.dll”怎么办?
  • ¥15 simulink绘制bode图
  • ¥15 php_network_getaddresses: getaddrinfo failed: Name or service not known
  • ¥15 用msg发消息出现的问题
  • ¥15 unity3d机械臂