Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
227 views
in Technique[技术] by (71.8m points)

php - Mobile browser detection?

We are looking for a way to determine if a user is using a mobile browser. We need to do that in PHP, by parsing the user agent string. I know this method has got many caveats, but we really need to do it that way.

Do you have any suggestion? A good (even if not perfect) updated code?

I know about WURFL, and I believe it's great, but it's not free to use anymore for non open source projects. By googling a bit, I also found this code: http://mobiforge.com/developing/story/lightweight-device-detection-php (and some variations), but I'm not sure about it. Looks like it's written really bad (look, for example, when they use $mobile_browser = '0' with the quotes around an integer...).

Can you recommend something?

Thank you,

Alessandro

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I am using this one:

$isMobile = (bool)preg_match('#(ip(hone|od)|android.+mobile|opera m(ob|in)i|windows (phone|ce)|blackberry'.
                    '|s(ymbian|eries60|amsung)|p(alm|rofile/midp|laystation portable)|nokia|fennec|htc[-_]'.
                    '|up.browser|[1-4][0-9]{2}x[1-4][0-9]{2})#i', $_SERVER['HTTP_USER_AGENT'] );

It's short and does detect most mobile users (or rather smartphones). iPad and Android-Tablets won't be classified as 'mobile' since they have bigger screen sizes.

If you want to catch Tablets as well, you can use this:

$isMobile = (bool)preg_match('#(ip(hone|od|ad)|android|opera m(ob|in)i|windows (phone|ce)|blackberry|tablet'.
                    '|s(ymbian|eries60|amsung)|p(laybook|alm|rofile/midp|laystation portable)|nokia|fennec|htc[-_]'.
                    '|mobile|up.browser|[1-4][0-9]{2}x[1-4][0-9]{2})#i', $_SERVER['HTTP_USER_AGENT'] );

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...