using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Text.RegularExpressions;namespace test
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string RefPage = "";
if (Request.UrlReferrer != null)
{
RefPage = Request.UrlReferrer.ToString();//获取来源页面
}
else
{
RefPage = "";
}
HttpBrowserCapabilities browser = Request.Browser;
Guid strid = Guid.NewGuid();
string strName = strid.ToString("N");//随机产生用户名
if (GetCookie("cookieName") == null)
{
SetCookie("cookieName", strName, 7);
}
else
{
strName = GetCookie("cookieName");
} string strCurrent = Request.ServerVariables["HTTP_REFERER"];//获取访问页面
if (strCurrent == null)
{
strCurrent = HttpContext.Current.Request.Url.ToString();
}
else
{
strCurrent = Request.ServerVariables["HTTP_REFERER"];//获取访问页面
}
string strBrowser = browser.Browser;//获取浏览器名称
string strOS = SystemCheck();//browser.Platform;
string strVisiton = browser.Version;//获取浏览器版本
string strIP = Request.UserHostAddress;//获取用户ip
string strLang = Request.UserLanguages[0].ToString();//获取语言
showdata(strName,strCurrent,strOS,strBrowser,strVisiton,strIP,strLang,RefPage);
}
#region 插入SQL语句
public void showdata(string strUserName,string strCurrent,string strOS,string strBrowser,string strVisit,string strIP,string strLang,string RefPage)
{
string strText = "insert into Allvisit values('"+strUserName+"','"+strCurrent+"','"+strOS+"','"+strBrowser+"','"+strVisit+"','"+strIP+"','"+strLang+"','"+RefPage+"','"+DateTime.Now+"')";
SqlConnection conn = new SqlConnection("server=ec.100to.com,8899;database=BQStatistics;uid=BQS;pwd=bqs_2010@123;");
//SqlConnection conn = new SqlConnection("server=.;database=Log;uid=sa;pwd=sa");
conn.Open();
SqlCommand comm = new SqlCommand(strText,conn);
comm.ExecuteNonQuery();
conn.Close();
}
#endregion
/// <summary>
/// 设置COOKIE值
/// </summary>
/// <param name="cookieName"></param>
/// <param name="cookieValue"></param>
/// <param name="day"></param>
public static void SetCookie(string cookieName, string cookieValue, int day)
{
HttpCookie cookie = new HttpCookie(cookieName);
cookie.Expires = DateTime.Now.AddDays(day);
cookie.Value = cookieValue;
HttpContext.Current.Response.Cookies.Add(cookie);
} /// <summary>
/// 读取COOKIE值
/// </summary>
/// <param name="cookieName"></param>
/// <returns></returns>
public static string GetCookie(string cookieName)
{
string str = ""; HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];
if (cookie != null)
str = cookie.Value.ToString();
else
str = null; return str;
}
public static string SystemCheck()
{
string Agent = HttpContext.Current.Request.ServerVariables["HTTP_USER_AGENT"];
if (Agent.IndexOf("NT 4.0") > 0)
{
return "Windows NT ";
}
else if (Agent.IndexOf("NT 5.0") > 0)
{
return "Windows 2000";
}
else if (Agent.IndexOf("NT 5.1") > 0)
{
return "Windows XP";
}
else if (Agent.IndexOf("NT 5.2") > 0)
{
return "Windows 2003";
}
else if (Agent.IndexOf("NT 6.0") > 0)
{
return "Windows Vista";
}
else if (Agent.IndexOf("WindowsCE") > 0)
{
return "Windows CE";
}
else if (Agent.IndexOf("NT") > 0)
{
return "Windows 7 ";
}
else if (Agent.IndexOf("9x") > 0)
{
return "Windows ME";
}
else if (Agent.IndexOf("98") > 0)
{
return "Windows 98";
}
else if (Agent.IndexOf("95") > 0)
{
return "Windows 95";
}
else if (Agent.IndexOf("Win32") > 0)
{
return "Win32";
}
else if (Agent.IndexOf("Linux") > 0)
{
return "Linux";
}
else if (Agent.IndexOf("SunOS") > 0)
{
return "SunOS";
}
else if (Agent.IndexOf("Mac") > 0)
{
return "Mac";
}
else if (Agent.IndexOf("Linux") > 0)
{
return "Linux";
}
else if (Agent.IndexOf("Windows") > 0)
{
return "Windows";
}
return "未知类型";
}
}
}为什么得出来的数据不正确?
用户打开这个页面的时候保存到Cookie(针对本电脑),再次打开时候判断是否是同一台电脑,和判断是否过了有效期(7天),没过则提取Cookie,否则从新生成一个用户名!
解决方案 »
- C#中已知一任意点坐标是否在已知直线上的算法???
- 求一个正则表达式
- 有没有什么方法,不通过throw Excption 的方法获得程序当前栈信息。
- 在指定目录下如何创建一个文件夹?谢谢!
- 初学,使用DataReder,为什么只能使用其中一个显示,要么就是DataGrid能显示,要么就是TextBox1显示数据
- 调用DLL的问题,高手请进!
- 我想请问一下: Visual C# MVP 和 Asp.net MVP 有什么区别?? C#不就是用来开发Asp.net的吗?
- 各位兄弟,我这样写为什么不对?
- 项目管理时提高工作效率的初步想法
- 求助正则表达式类的具体思路
- 怎么实现根据应用程序加载时间的进度更新进度条
- 大家帮我看看这个Cookie有没问题?
下次Cookie没读出来?