We were always proud of OSNews’ (and Gnomefiles.org’s) mobile capabilities. We spent years collecting keywords to be able to automatically redirect or serve a mobile-formatted or WAP-formatted (wap.osnews.com) page to less capable browsers. We believe that this script can recognize 99% of the world’s non-desktop browsers. We gave special care to not only phones, but PDAs, gaming devices, text browsers, even weird embedded systems browsers that most users have never heard of. Now, it’s time to open source our PHP detection script so others can use it on their sites too. Download here, and read the included readme.txt file too before using. It explains what is what, and what its difference is compared to similar solutions found elsewhere. You can see the work this detection script does in our mobile statistics (OSNews serves about 1500 pageviews per day on non-desktop browsers via this script).
The istr() function was a quick hack written into a version of our detection file because it was originally written using the preg_* functions. I wanted to convert to strstr() for performance, but the arguments are reversed, and it was going to take some time to either change them or write a good regex to do it for me. Since I was lazy, I just used the istr() function to switch needle and haystack.
In this case, the speed gain from strstr was likely negated by the istr() wrapper, but it does work. I highly suggest that if you use this on any high traffic website, you take the time to convert to using strstr().
Open detect.php in vi/vim/gvim, and type:
:%s/istr(“\([^”]*\)”, $agent)/stristr($agent, “\1”)/g
Thanks, but it’s already updated on the live version of OSNews.
The best part is the license.
Thank you.
It’s very nice that this is begin released for everyone.
Thank you.
It’s a really nice gift.
Sweet to see all of these documented – that part is going to be handy…
But the code – mein gott. You’ve got no short-circuit eval there, so if the first condition is true all the rest will still run even if you KNOW they aren’t needed. Likewise the ‘traps’ at the end should be done FIRST with an exit to reduce the execution time. The key to good code is to use as FEW if’s as possible, not to chain them end to end like that…
The function wrapper ‘istr’ is also a bad thing – since it’s a one to one wrap. Those handful of bytes you saved by not typing stristr each and every time is absurdly offset by the overhead of stack manipulation and ‘calls’ to the intermediate function.
Or just the simple wasted overhead of using ‘parsing model’ double quotes instead of the faster/cleaner single quotes. (remember in languages like php there’s a difference, with one using more overhead and more complex parsing code)
Since you’re only setting one value, wouldn’t it make more sense to put this all into an array, putting the LONGER ones that would be false positives FIRST in the list, followed by a short-circuit eval.
This probably should also be a function in a Library returning the value instead of using a global. Remember programming 101 – globals bad. (though a necessary evil at times)
Other optimizations could also be possible – for example you could speed it up even more by putting the most likely/common results first.
Edited 2008-04-25 02:45 UTC
No, there was a reason why I wanted each keyword on its own line: so I can identify keywords and add a set of special variables if a specific keyword is set, and then later, modify my HTML code based on these variables. Because you see, there were some phones or some versions of browsers that required special cHTML writing because of bugs, so I had to exclude them like that. You don’t see that in the version of the code I provided, but the original we used on osnews has those exceptions in. It was much more readable for me to do it like this instead of putting everything into arrays.
Edited 2008-04-25 02:36 UTC
Please show us the code your talking about, because whilst the gesture of offering this work is a great thing, the programming itself is extremely amateurish and we could help you improve and at the end of the day everybody wins.
Edited 2008-04-25 03:04 UTC
I explained what the additional code does. For example, if you hit Nokia Services and opera mini 2.0, you set an additional variable. Then, in your code, you say something like this:
if nokia services version 3.01 and opera mini 2.0, width 95% instead of 100%.
Stuff like that. You simply create special variables to identify some bugs of some versions/browsers and then you code the cHTML in a different way. I have some such code for nokia communicator, links etc.
If you really want to know about all these bugs these browsers have, you will have to experiment yourselves, get devices, emulators etc. I had to do all that.
Edited 2008-04-25 03:09 UTC
Thats still not a good way to go about it, hence here is a quick example I slapped together which demonstrates how this can be done better.
http://pastebin.com/f16ca0de5
…
Edited 2008-04-27 03:51 UTC
I understand that there’s a historical reason to separate the ifs rather than running the more efficient ‘if/else’, but since the code no longer needs to function in that manner, why not convert all but the first clause into an ‘else’ statement? Really seems baffling that you’ve left it as-is. Or have I misunderstood something?
Feel free to put the keywords on arrays. I just put together on the zip file a cleaned up version of what we use, I didn’t refactor it. Besides, you will need to change the istr() too, so some work will have to be done if some people want to use it anyway.
I don’t do osnews much anymore, neither I code mobile sites anymore, so I didn’t put lots of love on this version.
That was my first reaction looking at the code – Even WITH overloading, you overload the ones you NEED TO and not overload the others.
I can ‘kind of’ see why it’s not done, but in that case you just reverse the orders. Right now EVERY run of this code would execute all 267 if statements – when most of the time there should be a way to run a LOT less of those.
For example, how many of those also return ‘internet explorer’ in their user agent strings? Separate those off so that you aren’t testing those unless you need to. One extra IF saving you several dozen unneccessary ones… How many of those return width and height with their string? If that’s overloading, test those first and short-circuit testing the rest…
And if you need to add special conditions, wouldn’t a case statement AFTER the if’s be just as good?
I also notice in the comments that it used to use preg_match – Wouldn’t that cut down on false positives and remove the need for a lot of the extra checks?
Silly question, do you have a list formed of the user agent strings on which you made this code? I’m thinking there has to be a faster/easier/cleaner way of handling this, but I can’t entirely make assumptions because I’m not certain what conditions would overload others and what takes precedence.
just a question: would it be a problem to post the improved version?
If this is the code that forces me to see the crippled site on my Nokia 770 even though Minimo works just fine when the browserID is altered, then I like the idea, but it can REALLY be annoying at times too.
I’ve never understood why this disussion site feels like it always has to push the envelope when so many other sites seem to just work with no apparent need for such browser detection.
Edited 2008-04-25 02:40 UTC
Well, to their credit they were making the attempt before ANY of this stuff was standardized, in an age where CSS2 and much of CSS1 was undeployable, NOBODY was talking about semantic markup, separation of presentation from content, or the dozens of other things that one expects from a MODERN site with VALID markup that done properly negates the need (or even the expectation) of this sort of ‘sniffing’.
Which is exactly what we’re talking here – it’s browser sniffing, something that in web design circles is generally considered to be made of /FAIL/… even though on WML/WAP it’s a neccessary evil given the endless hordes of legacy devices out there. (If we were talking about doing this for IE/Opera/FF/Safari on the desktop, THEN it’s made of endless /FAIL/)
Edited 2008-04-25 03:58 UTC
I personally like the format of the main page in mobile view,its easy on the eyes and does the job well. However the comments section is almost unusable and needs better formatting for browsers that can do more then the basics
Erm, these pages are made for browsers that can’t do more than the basics, so the current format is the best that can be… Trust me, I spent years optimizing for so many devices that I was an avid collector of. You said that because you think too much with the “i will use the mobile site to avoid ads” hat on… 😉
Actually I was referring more to my Nintendo Wii and my Recently purchased iPod Touch (that I ended up using as more of a web browser then a media player)
On my (crappy)cell phone however both the main page and the comments look/work fine
Scroll to the bottom of the page, choose “view desktop version,” and you’ll be served the real version if you prefer.
Next time, just ask before you rant.
Thanks very much, really like the licence. I took the same route with much of my code.
Well. CC is not intended to apply to software.
http://wiki.creativecommons.org/FAQ#Can_I_use_a_Creative_Commons_li…
Better use BSD license.
Just my 0.2 cents.
Whilst that might be true, CC says “Use as you please, just please credit me.”. The fact that it doesn’t get into any specifics about code is why I like it. Code is an art, and it should be treated treated that way
Additionally, CC (I don’t remember if it applies to all the options) is incompatible with the DFSG[1], so software released under CC may not even be consider Free by Debian standards.
[1] http://people.debian.org/~evan/ccsummary.html
Edited 2008-04-26 14:41 UTC
I couldn’t care less what Debian consider “free”.
Their own misinterpretations are their own fault.
I may not ever use this for myself, but I do have friends with personal web sites who might be interested in making their pages more accessible to family and friends. Thanks!
is it supported ?
How about hosting this code in proper project site like Sourceforge or Google Code?
I think it’ll help you on long term maintainance.
There won’t be any real maintainance. Just the occasional user agent addition.
This seems to be little more than a list of user agent strings – or am I missing something?
Yes, that’s what took me 3 years to compile and even test with some of these browsers. It was a hunting experience.