使用MongoDB存储访问者信息
来源:互联网 发布:那个软件看电视免费 编辑:程序博客网 时间:2024/06/09 23:59
网站的访问者信息的存储一般都是海量的,通常使用关系数据库,现在NoSQL运动火热,满足这样的需求使用NoSQL数据库会更好,网站访问者信息主要是两个功能:
1、记录下网站的访问者信息
2、查询访问者信息和做相关的数据分析
本文采用MongoDB来记录访问者的信息的示例:
在asp.net中记录访问者信息的方法可以通过一个HttpHandler,在页面上放一个1像素的图片来请求这个HttpHandler,把他放到MasterPage页面就可以了。
下面给出ashx的代码
1: public class a : IHttpHandler
2: {
3: public void ProcessRequest(HttpContext ctx)
4: {
5: HttpBrowserCapabilities bc = ctx.Request.Browser;
6: Stat stat = new Stat();
7: stat._id = Guid.NewGuid();
8: stat.Browser = bc.Browser;
9: stat.Type = bc.Type;
10: stat.Version = bc.Version;
11: stat.Platform = bc.Platform;
12: stat.UrlReferrer = ctx.Request.UrlReferrer.ToString();
13: stat.UserHostAddress = ctx.Request.UserHostAddress;
14: stat.HttpMethod = ctx.Request.HttpMethod;
15: stat.IsAuthenticated = ctx.Request.IsAuthenticated;
16: stat.LogDateTime = DateTime.Now.ToLocalTime();
17:
18: WebClient wc=new WebClient();
19: try
20: {
21: string s =
22: wc.DownloadString("http://ipinfodb.com/ip_query.php?ip=" + stat.UserHostAddress + "&output=xml");
23: XmlDocument doc = new XmlDocument();
24: doc.LoadXml(s);
25: stat.Country = doc.DocumentElement.SelectNodes("CountryCode")[0].InnerText;
26: stat.State = doc.DocumentElement.SelectNodes("RegionName")[0].InnerText;
27:
28: stat.City = doc.DocumentElement.SelectNodes("City")[0].InnerText;
29: stat.Latitude = doc.DocumentElement.SelectNodes("Latitude")[0].InnerText;
30: stat.Longitude = doc.DocumentElement.SelectNodes("Longitude")[0].InnerText;
31:
32: }
33: catch(Exception ex)
34: {
35: System.Diagnostics.Debug.WriteLine(ex.Message+ ex.StackTrace );
36: }
37: finally
38: {
39: wc.Dispose();
40: }
41: using (Mongo mongo = Mongo.Create(Helper.ConnectionString() ))
42: {
43: MongoCollection<Stat> coll = (MongoCollection<Stat>)mongo.GetCollection<Stat>();
44: coll.Save(stat);
45: }
46:
47: string sFileName = String.Empty;
48: string sPath = ctx.Server.MapPath(".");
49: try
50: {
51: sFileName = ctx.Request["name"].ToString().Trim();
52: if (sFileName.Length < 5) { return; } // must be at least "1.gif" (5 chars)
53: // serve the image that was requested:
54: ctx.Response.WriteFile(sPath + @"/" + sFileName);
55: }
56: catch (Exception e)
57: {
58:
59: ctx.Response.Write(e.Message);
60: }
61: }
62: public bool IsReusable { get { return true; } }
63: }
上面代码使用到了HttpBrowserCapabilities,这里可以得到客户端的浏览器信息。还有客户端ip的来源使用到了ipinfodb.com这个服务,IPinfoDB网站非常的慷慨,慷慨到让人惊讶的程度,除了提供给你XML API和JSON API调用外,还提供了实现这些API的source code和所有的IP数据库,也就是说你只要下载这份code和database你也可以架设一个和IPinfoDB一样的网站,一样能够提供API服务,不过国外做的IP数据库对国内来说肯定不是很全很准,不过先将就着用吧。
访问访问者信息的MongoDB的信息记录:
1: [Serializable]
2: public class Stat
3: {
4: [MongoIdentifier]
5: public Guid _id {get;set;}
6: public string Type {get;set;}
7: public string Browser {get;set;}
8: public string Version {get;set;}
9: public string Platform {get;set;}
10: public string UrlReferrer {get;set;}
11: public string UserHostAddress {get;set;}
12: public bool IsAuthenticated {get;set;}
13: public string HttpMethod {get;set;}
14: public DateTime LogDateTime { get; set; }
15: public string City { get; set; }
16: public string State { get; set;}
17: public string Country { get; set; }
18: public string Latitude { get; set; }
19: public string Longitude { get; set; }
20: }
然后利用MongoDB的C# NORm驱动记录到MongoDB。
相关文章:
http://www.cnblogs.com/shanyou/archive/2010/06/04/1751734.html
http://www.eggheadcafe.com/tutorials/aspnet/63de8012-127a-4478-8725-3e1c27969596/nosql-mongodb-install-lotus-notes-and-couchdb.aspx
http://www.eggheadcafe.com/tutorials/aspnet/51d3ae19-d6f9-4807-ac0a-0baab2964b03/mongodb-install-as-service-and-use-net-drivers.aspx
http://www.eggheadcafe.com/tutorials/aspnet/93206c89-09c9-40fc-9296-7d74bb7996ad/a-mongodb-cache-utility.aspx
http://www.eggheadcafe.com/tutorials/aspnet/3a73c6de-82a1-4690-a7aa-d0eda58203f7/store-aspnet-site-visitor-stats-in-mongodb.aspx
http://www.eggheadcafe.com/tutorials/aspnet/27f836b7-2c9e-4942-9712-1c7b901cadcc/aspnet-providerless-custom-forms-authentication-roles-and-profile-with-mongodb.aspx
- 使用MongoDB存储访问者信息
- MongoDB应用案例:使用 MongoDB 存储商品分类信息
- Mongodb存储过程使用
- 使用SharedPreference存储信息
- 记录访问者的信息
- MongoDB GridFS 使用 存储特点
- 使用plist文件存储信息
- 使用keychain存储重要信息
- 记录页面访问者的信息
- asp.net 取得访问者信息
- MongoDB应用案例:使用 MongoDB 存储日志数据
- 分布式文档存储数据库 MongoDB 2.0 使用
- Docker Registry + docker_auth 使用mongodb 存储
- python平台使用mongodb存储大文件
- Node.js--使用MongoDB存储数据
- 访问者模式使用反射
- 使用Forms验证存储用户自定义信息
- 使用Forms验证存储用户自定义信息
- 自己收集的网络安全知识
- Solr笔记(2)_Schema.xml和solrconfig.xml分析
- 思考mysql内核之初级系列3---办理业务的流程
- 8086下十六位贪吃蛇小程序 (仅1k)
- ASP.NET MVC2 数据模型验证类库:MVC Foolproof Validation
- 使用MongoDB存储访问者信息
- pku1026Cipher 置换群
- js encode ,java decode,virtools 页面播放~
- 安装和管理MySql数据库(MySql数据库初探一)
- 今年想做几件事
- 运行heritrix1.14.4报错 thread-10 org.archive.util.ArchiveUtils.() TLD list unavailable
- 差分信号
- sigprocmask
- Lightinthebox模版快速修改