XF 2.0 Undefined index: HTTP_USER_AGENT

hemant_bhardwaj

Well-known member
getting this error in one of my client forum with with HTTP_USER_AGENT but not able to reproduce on my end but
Code:
if(preg_match("/(android|avantgo|blackberry|bolt|boost|cricket|docomo|fone|hiptop|mini|mobi|palm|phone|pie|tablet|up\.browser|up\.link|webos|wos)/i", $_SERVER["HTTP_USER_AGENT"])){
      ---- MYcode------
        }else {
      ---- MYcode------
        }

does it related to xenforo or hosting ?
 
The array $_SERVER in this client's case does not contain a HTTP_USER_AGENT key. Your code needs to protect against that. And that pretty much needs to apply in every single bit of code that you write when you're handling an array which may or may not have the keys you expect. That's a fairly fundamental PHP principle.
 
@Chris D is there any way in xenforo to detect the device
like is it desktop or mobile ?
Because it seems you try to apply mobile detection in PHP code, you can use this

It is pretty robust :)

But if you only want to detect in template, using template system, you can use this add-on.
 
Top Bottom