Jump to content

Need help creating an array of mobile useragents


kimosiris

Recommended Posts

I have a simple detection for mobile devices and it works fine but it has gotten a bit bloated so I tried to put the list in an array. I don't get errors but it doesn't work. Here is what I tried

 

$useragents = array("iPhone","iPod","incognito","webmate","Android","dream","CUPCAKE","blackberry9500","blackberry9530","blackberry9520","blackberry9550","blackberry 9800","webOS","s8000","bada","Googlebot-Mobile");

 

if (in_array(1, $useragents)) {

$phones = strpos($_SERVER['HTTP_USER_AGENT'], $useragents);

}

 

if ($phones == true) { include_once './template/phones.php';}

 

Please assist

Link to comment
Share on other sites

darkfreaks that additional = didn't make a difference. Note that I do not get any errors with the scripting above, it just does not execute and load the file.

 

If I do it like this below it works fine, which is how I have it now but it has gotten quite long.

 

$iphone = strpos($_SERVER['HTTP_USER_AGENT'],"iPhone");

$bb = strpos($_SERVER['HTTP_USER_AGENT'],"blackberry");

$droid = strpos($_SERVER['HTTP_USER_AGENT'],"Android");

 

if ($iphone == true or $bb == true or $droid == true) { echo 'phone page';} else {echo 'big monitor page'; }

 

@gristoi

WURFL does work but as you said it is quite an overkill as it attempts to detect all possible user agents though it does allow for specific isolated queries.

 

Thanks for the suggestions and since I already went through the process of setting up WURFL I will keep it running but I'd still like to find the solution as to why the simple array does not work. If anyone knows please advise.

Link to comment
Share on other sites

...and here I am foolishly thinking this forum was here to assist 'anyone' needing php help. creata.physics don't you think if I knew 'exactly' the correct thing to do I would have done it? I am merely presenting my concept with the hope that someone 'knowledgeable' will point me in the right direction.

 

If you do not have a solution or method suggestion then there's no need to post in this string!

Link to comment
Share on other sites

I usually use a stripos_inarray function for these kind of purposes. Thus you can write all your mobile strings in lowercase.

 

function stripos_inarray($haystack, $needles) {
   if (!is_array($needles))
      $needles = array($needles);

   foreach($needles as $needle) {
      if (($pos = stripos($haystack, $needle)) !== false)
         return $pos;
   }

   return false;
}

$useragents = array('iphone', 'ipod', 'incognito', 'webmate', 'android', 'dream', 'cupcake', 'blackberry', 'webos', 's8000', 'bada', 'googlebot-mobile');

if (stripos_inarray($_SERVER['HTTP_USER_AGENT'], $useragents) !== false)
   include_once 'template/phones.php';

Link to comment
Share on other sites

  • 3 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.