如何获取用户浏览器详情、IP地址和位置信息?
How to obtain user browser details, IP address and location information?
我需要帮助获取用户的浏览器信息、IP 地址和 GEO 位置。我们正在开发 asp.net 应用程序,我们需要上述信息来跟踪用户信息,其中 he/she 使用 browser/IP 信息访问应用程序。以下是我们需要并需要存储在应用程序数据库中的详细信息。
- Browser information/version
- Operating system
- Device (Desktop/laptop/Tablet/Mobile)
- IP address
- Country code/country name
- City
- Region
是否可以从一个来源获取所有信息?我用谷歌搜索了这些并建议使用第三方 APIs 来根据 IP 地址获取地理信息 APIs 是否可靠地使用应用程序。有没有什么最好的方法来构建自己的 API 来获取这些信息以及如何获取这些信息?请指教
尝试下面的 javascript 函数,这将 return 浏览器名称和浏览器版本。
function get_browser()
{
var ua = navigator.userAgent, tem,
M=ua.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) ||[];
if (/trident/i.test(M[1]))
{
tem = /\brv[ :]+(\d+)/g.exec(ua) || [];
return { name: 'IE', version: (tem[1] || '') };
}
if (M[1] === 'Chrome')
{
tem = ua.match(/\bOPR\/(\d+)/)
if (tem != null)
{
return { name: 'Opera', version: tem[1] };
}
}
M = M[2] ? [M[1], M[2]] : [navigator.appName, navigator.appVersion, '-?'];
if ((tem = ua.match(/version\/(\d+)/i)) != null)
{
M.splice(1, 1, tem[1]);
}
return {
name: M[0],
version: M[1]
};
}
万一其他人可能需要更短的方法来完成问题中提到的任务,他们可以使用下面的代码;
<div id="demo"></div>
<script type="text/javascript">
var txt = "";
txt += "<p> Browser CodeName: <strong>"+navigator.appCodeName+" </strong></p>";
txt += "<p> Browser Name: <strong>"+navigator.appName+"</strong></p>";
txt += "<p> Browser Version: <strong>"+navigator.appVersion+"</strong></p>";
txt += "<p> Cookies Enabled: <strong>"+navigator.cookieEnabled+"</strong></p>";
txt += "<p> Browser Online: <strong>"+navigator.onLine+"</strong></p>";
txt += "<p> Language: <strong>"+navigator.language+"</strong></p>";
txt += "<p> Platform: <strong>"+navigator.platform+"</strong></p>";
txt += "<p> User-agent Header: <strong>"+navigator.userAgent+"</strong></p>";
document.getElementById("demo").innerHTML=txt;
</script>
我需要帮助获取用户的浏览器信息、IP 地址和 GEO 位置。我们正在开发 asp.net 应用程序,我们需要上述信息来跟踪用户信息,其中 he/she 使用 browser/IP 信息访问应用程序。以下是我们需要并需要存储在应用程序数据库中的详细信息。
- Browser information/version
- Operating system
- Device (Desktop/laptop/Tablet/Mobile)
- IP address
- Country code/country name
- City
- Region
是否可以从一个来源获取所有信息?我用谷歌搜索了这些并建议使用第三方 APIs 来根据 IP 地址获取地理信息 APIs 是否可靠地使用应用程序。有没有什么最好的方法来构建自己的 API 来获取这些信息以及如何获取这些信息?请指教
尝试下面的 javascript 函数,这将 return 浏览器名称和浏览器版本。
function get_browser()
{
var ua = navigator.userAgent, tem,
M=ua.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) ||[];
if (/trident/i.test(M[1]))
{
tem = /\brv[ :]+(\d+)/g.exec(ua) || [];
return { name: 'IE', version: (tem[1] || '') };
}
if (M[1] === 'Chrome')
{
tem = ua.match(/\bOPR\/(\d+)/)
if (tem != null)
{
return { name: 'Opera', version: tem[1] };
}
}
M = M[2] ? [M[1], M[2]] : [navigator.appName, navigator.appVersion, '-?'];
if ((tem = ua.match(/version\/(\d+)/i)) != null)
{
M.splice(1, 1, tem[1]);
}
return {
name: M[0],
version: M[1]
};
}
万一其他人可能需要更短的方法来完成问题中提到的任务,他们可以使用下面的代码;
<div id="demo"></div>
<script type="text/javascript">
var txt = "";
txt += "<p> Browser CodeName: <strong>"+navigator.appCodeName+" </strong></p>";
txt += "<p> Browser Name: <strong>"+navigator.appName+"</strong></p>";
txt += "<p> Browser Version: <strong>"+navigator.appVersion+"</strong></p>";
txt += "<p> Cookies Enabled: <strong>"+navigator.cookieEnabled+"</strong></p>";
txt += "<p> Browser Online: <strong>"+navigator.onLine+"</strong></p>";
txt += "<p> Language: <strong>"+navigator.language+"</strong></p>";
txt += "<p> Platform: <strong>"+navigator.platform+"</strong></p>";
txt += "<p> User-agent Header: <strong>"+navigator.userAgent+"</strong></p>";
document.getElementById("demo").innerHTML=txt;
</script>