Forum Moderators: coopster

Message Too Old, No Replies

Creating a dynamic value in an array?

Need to detect IE on OSX in browser agent array.

         

JAB Creations

4:21 pm on Jul 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have a user agent array that is a list of browsers that do not support AJAX. Unfortunately IE on OSX does not support AJAX and it's user agent is not something I'm aware of how to add dynamically since 'MSIE' and 'Mac' are separated by IE's version string which of has from what I can guess 23 different versions! Here is an example of the user agent string for 5.23...

Mozilla/4.0 (Compatible; MSIE 5.23; Mac_PowerPC)

A friend was trying to help me achieve this though it kept breaking the array. I need to match 'MSIE' and 'Mac' in the array string in order to have content served statically as IE on Mac does not support AJAX.

- John

whoisgregg

4:32 pm on Jul 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How are you using the array of user agent strings to check against the provided value? Are you using an in_array() [php.net]? If so, you'll need to populate the array with every possible variation of the Mac IE user agent string.

Or, just write a regular expression and do a preg_match() [php.net].

And, I'm sure I've mentioned this to you before John, but as a reminder to anyone else reading this thread, Mac IE is dead [webmasterworld.com] and only certain niches still actively use it. So, before you decide to support it for your website, make sure your target market is using it. :)

JAB Creations

1:29 am on Jul 7, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I know IE on OSX is dead Jim, still I can animate the corpse good enough to emulate older versions of Opera. The new version of my site supports most browsers including IE on OSX and older browsers such as Opera 4.

This is how I process the array...

$checkit = '';
$answer = '';
foreach ($useragents['noajax'] as $ua) {
$checkit = stristr($useragent,$ua);
if ($checkit === false) {$answer = false;}
else
{$answer = true; break;}
}

- John

whoisgregg

1:46 pm on Jul 9, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



So you can either change your stristr check into a preg_match (turning your ['noajax'] into an array of regular expressions as well of course) and add that overhead for each check or you can add a separate check just for Mac IE. I suppose a separate check would be best to avoid adding that overhead, so I've provided an example of how to do it below. It should be pretty fast. :)

<?php
$useragent = 'Mozilla/4.0 (Compatible; MSIE 5.23; Mac_PowerPC)';
//$useragent = $_SERVER['HTTP_USER_AGENT'];
echo '<p>Checking UA: '.$useragent.'</p>';
$useragents['noajax'] = array('WebTV', 'CyberDog');
$answer = false;
foreach ($useragents['noajax'] as $ua) {
if (stristr($useragent,$ua) !== false) {
$answer = true; break;
}
}
if(!$answer && (strpos(strtolower($useragent), 'mac') !== false) && preg_match('/msie ([0-9]+)\.([0-9]+)/i', $useragent) != 0){
$answer = true;
}
echo '<p>'.(($answer)? 'No AJAX Support! Oh Noes!' : 'AJAX is in da\' house').'</p>';
?>