Linked by Eugenia Loli on Thu 24th Apr 2008 22:45 UTC
Internet & Networking 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).
Order by: Score:
A word of advice
by Adam S on Thu 24th Apr 2008 23:03 UTC
Adam S
Member since:
2005-04-01

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().

Reply Score: 2

RE: A word of advice
by Beta on Fri 25th Apr 2008 11:21 UTC in reply to "A word of advice"
Beta Member since:
2005-07-06

Open detect.php in vi/vim/gvim, and type:

:%s/istr("\([^"]*\)", $agent)/stristr($agent, "\1")/g

Reply Score: 3

RE[2]: A word of advice
by Adam S on Fri 25th Apr 2008 11:22 UTC in reply to "RE: A word of advice"
Adam S Member since:
2005-04-01

Thanks, but it's already updated on the live version of OSNews.

Reply Score: 1

...
by Hiev on Fri 25th Apr 2008 00:16 UTC
Hiev
Member since:
2005-09-27

The best part is the license.

Thank you.

Reply Score: 1

gift
by JrezIN on Fri 25th Apr 2008 01:35 UTC
JrezIN
Member since:
2005-06-29

It's very nice that this is begin released for everyone.

Thank you.
It's a really nice gift.

Reply Score: 4

Comment by deathshadow
by deathshadow on Fri 25th Apr 2008 02:29 UTC
deathshadow
Member since:
2005-07-12

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

Reply Score: 4

RE: Comment by deathshadow
by Eugenia on Fri 25th Apr 2008 02:35 UTC in reply to "Comment by deathshadow"
Eugenia Member since:
2005-06-28

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

Reply Score: 1

RE[2]: Comment by deathshadow
by prickett on Fri 25th Apr 2008 03:03 UTC in reply to "RE: Comment by deathshadow"
prickett Member since:
2007-04-03

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

Reply Score: 1

RE[3]: Comment by deathshadow
by Eugenia on Fri 25th Apr 2008 03:08 UTC in reply to "RE[2]: Comment by deathshadow"
Eugenia Member since:
2005-06-28

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

Reply Score: 1

RE[4]: Comment by deathshadow
by prickett on Fri 25th Apr 2008 03:43 UTC in reply to "RE[3]: Comment by deathshadow"
prickett Member since:
2007-04-03

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

Reply Score: 5

RE[5]: Comment by deathshadow
by CrazyDude1 on Sun 27th Apr 2008 03:49 UTC in reply to "RE[4]: Comment by deathshadow"
CrazyDude1 Member since:
2007-09-17

...

Edited 2008-04-27 03:51 UTC

Reply Score: 1

RE[2]: Comment by deathshadow
by ohbrilliance on Fri 25th Apr 2008 03:36 UTC in reply to "RE: Comment by deathshadow"
ohbrilliance Member since:
2005-07-07

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?

Reply Score: 2

RE[3]: Comment by deathshadow
by Eugenia on Fri 25th Apr 2008 03:43 UTC in reply to "RE[2]: Comment by deathshadow"
Eugenia Member since:
2005-06-28

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.

Reply Score: 1

RE[3]: Comment by deathshadow
by deathshadow on Fri 25th Apr 2008 03:49 UTC in reply to "RE[2]: Comment by deathshadow"
deathshadow Member since:
2005-07-12

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?

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.

Reply Score: 3

RE: Comment by deathshadow
by kamil_chatrnuch on Fri 25th Apr 2008 14:03 UTC in reply to "Comment by deathshadow"
kamil_chatrnuch Member since:
2005-07-07

just a question: would it be a problem to post the improved version?

Reply Score: 2

Ah... Nice gesture, **BAD** idea.
by rcsteiner on Fri 25th Apr 2008 02:39 UTC
rcsteiner
Member since:
2005-07-12

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

Reply Score: 4

deathshadow Member since:
2005-07-12

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.

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

Reply Score: 2

Square Member since:
2005-10-01

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

Reply Score: 1

Eugenia Member since:
2005-06-28

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... ;-)

Reply Score: 1

Square Member since:
2005-10-01

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

Reply Score: 1

Adam S Member since:
2005-04-01

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.

Reply Score: 2

Comment by Kroc
by Kroc on Fri 25th Apr 2008 06:56 UTC
Kroc
Member since:
2005-11-10

Thanks very much, really like the licence. I took the same route with much of my code.

Reply Score: 2

RE: Comment by Kroc
by ikeX on Fri 25th Apr 2008 15:12 UTC in reply to "Comment by Kroc"
ikeX Member since:
2007-03-28

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.

Reply Score: 1

RE[2]: Comment by Kroc
by Kroc on Fri 25th Apr 2008 15:47 UTC in reply to "RE: Comment by Kroc"
Kroc Member since:
2005-11-10

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 ;)

Reply Score: 2

RE[2]: Comment by Kroc
by unavowed on Sat 26th Apr 2008 14:40 UTC in reply to "RE: Comment by Kroc"
unavowed Member since:
2006-03-23

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

Reply Score: 1

RE[3]: Comment by Kroc
by Kroc on Sat 26th Apr 2008 17:34 UTC in reply to "RE[2]: Comment by Kroc"
Kroc Member since:
2005-11-10

I couldn't care less what Debian consider "free".
Their own misinterpretations are their own fault.

Reply Score: 3

A real treat
by Morgan on Fri 25th Apr 2008 07:09 UTC
Morgan
Member since:
2005-06-29

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!

Reply Score: 2

NetPositive ?
by mmu_man on Fri 25th Apr 2008 11:00 UTC
mmu_man
Member since:
2006-09-30

is it supported ? ;)

Reply Score: 3

SourceForge?
by markpeak on Sat 26th Apr 2008 18:43 UTC
markpeak
Member since:
2005-07-06

How about hosting this code in proper project site like Sourceforge or Google Code?

I think it'll help you on long term maintainance.

Reply Score: 1

RE: SourceForge?
by Eugenia on Sun 27th Apr 2008 08:45 UTC in reply to "SourceForge?"
Eugenia Member since:
2005-06-28

There won't be any real maintainance. Just the occasional user agent addition.

Reply Score: 1

A list of user agent substrings
by joshv on Sat 26th Apr 2008 22:17 UTC
joshv
Member since:
2006-03-18

This seems to be little more than a list of user agent strings - or am I missing something?

Reply Score: 1

RE: A list of user agent substrings
by Eugenia on Sun 27th Apr 2008 08:44 UTC in reply to "A list of user agent substrings"
Eugenia Member since:
2005-06-28

Yes, that's what took me 3 years to compile and even test with some of these browsers. It was a hunting experience.

Reply Score: 1