kimosiris Posted April 27, 2012 Share Posted April 27, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/261682-need-help-creating-an-array-of-mobile-useragents/ Share on other sites More sharing options...
darkfreaks Posted April 27, 2012 Share Posted April 27, 2012 change == to === will solve some of your errors. Quote Link to comment https://forums.phpfreaks.com/topic/261682-need-help-creating-an-array-of-mobile-useragents/#findComment-1341003 Share on other sites More sharing options...
gristoi Posted April 27, 2012 Share Posted April 27, 2012 might be a bit of overkill, but have a look at wurfl: http://wurfl.sourceforge.net/php_index.php Quote Link to comment https://forums.phpfreaks.com/topic/261682-need-help-creating-an-array-of-mobile-useragents/#findComment-1341049 Share on other sites More sharing options...
kimosiris Posted April 28, 2012 Author Share Posted April 28, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/261682-need-help-creating-an-array-of-mobile-useragents/#findComment-1341311 Share on other sites More sharing options...
creata.physics Posted April 28, 2012 Share Posted April 28, 2012 You aren't using in_array correctly. Please read in_array Once you do, fix your code and then get back to us please. If you correctly used in_array you'd be able to get your initial code to work properly. Quote Link to comment https://forums.phpfreaks.com/topic/261682-need-help-creating-an-array-of-mobile-useragents/#findComment-1341314 Share on other sites More sharing options...
kimosiris Posted April 28, 2012 Author Share Posted April 28, 2012 ...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! Quote Link to comment https://forums.phpfreaks.com/topic/261682-need-help-creating-an-array-of-mobile-useragents/#findComment-1341442 Share on other sites More sharing options...
silkfire Posted April 28, 2012 Share Posted April 28, 2012 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'; Quote Link to comment https://forums.phpfreaks.com/topic/261682-need-help-creating-an-array-of-mobile-useragents/#findComment-1341455 Share on other sites More sharing options...
kimosiris Posted May 17, 2012 Author Share Posted May 17, 2012 silkfire thanks for the suggestion. I have been busy so I have not yet returned to my project. I will apply the code and let you know if I have errors Quote Link to comment https://forums.phpfreaks.com/topic/261682-need-help-creating-an-array-of-mobile-useragents/#findComment-1346445 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.