Javascript 如何检测移动设备并获取用户代理信息,将该信息发送并保存到服务器上的数据库中,仅一次?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6081346/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 20:08:54  来源:igfitidea点击:

How to detect mobile device and get user agent info send and save that information to database on server, only once?

javascriptjquerymobile-websitemobile-deviceswurfl

提问by Jitendra Vyas

Is there any script to check user agent then send and save that information to database on server?

是否有任何脚本可以检查用户代理,然后将该信息发送并保存到服务器上的数据库中?

I'm making mobile website with 2 versions like m.facebook.comand touch.facebook.comand I want to redirect to different mobiles. and I use WURFLdevice detection.

我正在制作具有 2 个版本的移动网站,例如m.facebook.comtouch.facebook.com,我想重定向到不同的手机。我使用WURFL设备检测。

So for example m.html5version.com should be delivered to all HTML5 supported devices and m.mobileversion.comshould deliver to all other mobiles.

因此,例如 m.html5version.com 应交付给所有支持 HTML5 的设备,而m.mobileversion.com 应交付给所有其他移动设备。

So i need a way to detect device and browser 1st time using any server-side or client-side method. when any user will open the website first time on device then an script should run once it should collect the information about device and browser and should save the information in my existing WURFL database , so next time when any user will come from same device then he will redirect to touch or mobile version of website depending on device.

所以我需要一种使用任何服务器端或客户端方法第一次检测设备和浏览器的方法。当任何用户第一次在设备上打开网站时,脚本应该运行一次,它应该收集有关设备和浏览器的信息,并将信息保存在我现有的 WURFL 数据库中,所以下次当任何用户来自同一设备时,他将根据设备重定向到触摸版或移动版网站。

So only 1st time on 1st device I want to detect device and save the info.

所以只有第一次在第一台设备上我想检测设备并保存信息。

Is there any way to do this?

有没有办法做到这一点?

For example

例如

Android 1.6 doesn't have HTML5 support and still in use. So i want to redirect all Android 1.6 devices to m.mobileversion.comnot to touch.mobileversion.com

Android 1.6 不支持 HTML5 并且仍在使用中。所以我想将所有 Android 1.6 设备重定向到m.mobileversion.com而不是touch.mobileversion.com

And in touch version of website I'm using HTML 5 tags like <header>, <footer>, <nav>etc which is not supported Android 1.6. So i want to detect the first device who will access the website from Android 1.6 device and because HTML5 is not supported in 1.6 so user agent should be saved in my database then I when next time any user will open the website in 1.6 it will redirect to lo-fi mobile version automatically, no detection should be needed 2nd time for any device.

而在网站的触摸版我使用HTML 5个标签,如<header><footer><nav>等,这是不支持的Android 1.6。所以我想检测第一个从 Android 1.6 设备访问网站的设备,因为 1.6 不支持 HTML5,所以用户代理应该保存在我的数据库中,然后我下次任何用户在 1.6 中打开网站时它会重定向自动到lo-fi移动版本,任何设备都不需要第二次检测。

回答by diEcho

see below link, this will perfectly suitable for your need

请参阅下面的链接,这将非常适合您的需要

http://detectmobilebrowser.com

http://detectmobilebrowser.com

below is one more script :

下面是另一个脚本:

http://detectmobilebrowsers.mobi

http://detectmobilebrowsers.mobi

回答by Denis de Bernardy

You might want to look into jQuery's take on this, since it's interesting.

您可能想了解 jQuery 对此的看法,因为它很有趣。

In addition to detecting the browser, they detect browser features:

除了检测浏览器外,它们还会检测浏览器功能:

http://api.jquery.com/jQuery.browser/

http://api.jquery.com/jQuery.browser/

http://api.jquery.com/jQuery.support/

http://api.jquery.com/jQuery.support/

Also, keep in mind that some mobile users don't want to be redirected to the mobile-optimized device. I, for one, positively hate it when a site detects my iPad as a mobile device and sends me (without any possibility to override it) to a sloppy mobile site that I wouldn't even want to browse with an iPhone.

此外,请记住,某些移动用户不希望被重定向到针对移动优化的设备。一方面,当一个网站将我的 iPad 检测为移动设备并将我(没有任何可能覆盖它)发送到一个我什至不想用 iPhone 浏览的草率移动网站时,我非常讨厌它。

iOS and Android devices, in general, will happily work with the standard site if it's coded properly. And other mobile phones are quickly dying.

通常,如果编码正确,iOS 和 Android 设备将很乐意与标准站点一起使用。其他手机正在迅速消亡。

回答by KOGI

As far as only detecting and saving the data once, you can set a cookie to record the fact that you've already done the detection. Then the next time a page is loaded, check to see if the cookie exists -- if it doesn't, then call your detection function and record to the database. If you do find the cookie, then you know you've already collected the data and you can skip doing it again.

至于只检测并保存一次数据,您可以设置一个cookie来记录您已经完成检测的事实。然后下次加载页面时,检查 cookie 是否存在——如果不存在,则调用您的检测函数并记录到数据库中。如果您确实找到了 cookie,那么您就知道您已经收集了数据,可以跳过再次收集。

http://www.quirksmode.org/js/cookies.html

http://www.quirksmode.org/js/cookies.html