分类目录归档:Asp.Net

网站流量统计

本程序采用.net平台和mysql数据库实现

mysql数据库中表:

CREATE TABLE `web_statistics` (
 `id` int(10) unsigned NOT NULL auto_increment,
 `Cur_url` varchar(500) NOT NULL,
 `Pre_url` varchar(500) NOT NULL default '未知',
 `Os` varchar(50) NOT NULL default '未知',
 `Ip` varchar(15) NOT NULL,
 `Browser` varchar(50) NOT NULL default '未知',
 `Access_Time` datetime NOT NULL,
 `Host_Name` varchar(45) NOT NULL default '未知',
 PRIMARY KEY  (`id`)
)

mysql存储过程:

CREATE  PROCEDURE `proc_statistics`(Cur_url varchar(500),Pre_url varchar(500),Os varchar(50),Brows varchar(50),Host_Name varchar(45),Ip varchar(15))
begin
insert into web_statistics(Cur_url,Pre_url,Os,Ip,Browser,Access_Time,Host_Name)
values(Cur_url,Pre_url,Os,Ip,Brows,now(),Host_Name);
end

ashx执行平台代码:

using System;
using System.Web;
using MySql.Data;
using MySql.Data.MySqlClient;
using MySql.Data.Types;
using System.Data;
public class Statistics : IHttpHandler {

 public void ProcessRequest (HttpContext context) {
 context.Response.ContentType = "image/jpg";
 string url_cur = "加载当前Url错误";
 string url_pre = "直接访问";
 if (context.Request.QueryString["d"] != null)
 {
 url_pre = System.Web.HttpContext.Current.Server.UrlDecode(context.Request.QueryString["d"].ToString()).Replace("`","&");
 if (url_pre == "")
 {
 url_pre = "直接访问";
 }
 }
 if (context.Request.QueryString["u"] != null)
 {
 url_cur = System.Web.HttpContext.Current.Server.UrlDecode(context.Request.QueryString["u"].ToString()).Replace("`", "&");
 }
 string[] data = GetData();
 string con_mysql = System.Configuration.ConfigurationManager.ConnectionStrings["mysql"].ToString();
 MySqlConnection conn;
 MySqlCommand cmd ;
 try
 {
 conn = new MySqlConnection(con_mysql);
 conn.Open();
 cmd = new MySqlCommand();
 cmd.Connection = conn;
 //存储过程实现

 cmd.CommandText = "proc_statistics";
 cmd.CommandType = CommandType.StoredProcedure;
 MySqlParameter para1 = new MySqlParameter("Cur_url", MySqlDbType.VarChar, 500);
 MySqlParameter para2 = new MySqlParameter("Pre_url", MySqlDbType.VarChar, 500);
 MySqlParameter para3 = new MySqlParameter("Os", MySqlDbType.VarChar, 50);
 MySqlParameter para4 = new MySqlParameter("Ip", MySqlDbType.VarChar, 15);
 MySqlParameter para5 = new MySqlParameter("Brows", MySqlDbType.VarChar, 50);
 MySqlParameter para7 = new MySqlParameter("Host_Name", MySqlDbType.VarChar, 45);
 para1.Value = url_cur;
 para2.Value = url_pre;
 para3.Value = data[1];
 para4.Value = data[3];
 para5.Value = data[2];
 para7.Value = data[0];
 cmd.Parameters.Add(para1);
 cmd.Parameters.Add(para2);
 cmd.Parameters.Add(para3);
 cmd.Parameters.Add(para4);
 cmd.Parameters.Add(para5);
 cmd.Parameters.Add(para7);

 //参数实现
 /*
 cmd.CommandType = CommandType.Text;
 //直接拼接字符串实现
 //string sql = "insert into web_statistics(Cur_url,Pre_url,Os,Ip,Browser,Access_Time,Host_Name)values('"+url_cur+"','"+url_pre+"','"+data[1]+"','"+data[3]+"','"+data[2]+"',now(),'"+data[0]+"')";
 //sql语句参数实现
 string sql = "insert into web_statistics(Cur_url,Pre_url,Os,Ip,Browser,Access_Time,Host_Name)values(?Cur_url,?Pre_url,?Os,?Ip,?Brows,now(),?Host_Name)";
 cmd.CommandText = sql;
 MySqlParameter para1 = new MySqlParameter("?Cur_url", MySqlDbType.VarChar,500);
 MySqlParameter para2 = new MySqlParameter("?Pre_url", MySqlDbType.VarChar, 500);
 MySqlParameter para3 = new MySqlParameter("?Os", MySqlDbType.VarChar, 50);
 MySqlParameter para4 = new MySqlParameter("?Ip", MySqlDbType.VarChar, 15);
 MySqlParameter para5 = new MySqlParameter("?Brows", MySqlDbType.VarChar, 50);
 MySqlParameter para7 = new MySqlParameter("?Host_Name", MySqlDbType.VarChar, 45);
 para1.Value = url_cur;
 para2.Value = url_pre;
 para3.Value = data[1];
 para4.Value = data[3];
 para5.Value=data[2];
 para7.Value = data[0];
 cmd.Parameters.Add(para1);
 cmd.Parameters.Add(para2);
 cmd.Parameters.Add(para3);
 cmd.Parameters.Add(para4);
 cmd.Parameters.Add(para5);
 cmd.Parameters.Add(para7);
 */

 cmd.ExecuteNonQuery();
 cmd.Dispose();
 conn.Close();
 }
 catch {}
 //输入图片
 string img_name = "statistics.jpg";
 //直接输出图片
 //  context.Response.WriteFile(context.Server.MapPath(img_name));

 //缓冲输出图片
 byte[] datas;
 if (HttpContext.Current.Cache["datacache"] != null)
 {
 datas = (byte[])HttpContext.Current.Cache["datacache"];
 }
 else
 {
 //记得修改文件图片名称
 datas = System.IO.File.ReadAllBytes(context.Server.MapPath(img_name));
 HttpContext.Current.Cache.Insert("datacache", datas, null, DateTime.MaxValue, TimeSpan.FromHours(12));
 }
 context.Response.OutputStream.Write(datas, 0, datas.Length);

 }

 public  string[] GetData()
 {
 string[] data = new string[4];
 string userAgent = System.Web.HttpContext.Current.Request.UserAgent == null ? "无" : System.Web.HttpContext.Current.Request.UserAgent;
 data[0] = System.Web.HttpContext.Current.Request.ServerVariables.Get("Remote_Host").ToString(); //主机名(暂时有问题)
 data[1] = System.Web.HttpContext.Current.Request.Browser.Platform.ToString() + ":" + GetOSNameByUserAgent(userAgent);//操作系统
 data[2] = System.Web.HttpContext.Current.Request.Browser.Browser.ToString() + ":" + System.Web.HttpContext.Current.Request.Browser.Version.ToString();//浏览器名称和版本
 string result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
 if (null == result || result == String.Empty)
 {
 result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
 }
 if (null == result || result == String.Empty)
 {
 result = HttpContext.Current.Request.UserHostAddress;
 }
 data[3] = result;//ip地址
 return data;
 }
 private  string GetOSNameByUserAgent(string userAgent)
 {
 string osVersion = "未知";
 if (userAgent.Contains("NT 6.1"))
 {
 osVersion = "Windows 7/Server 2008 R2";
 }
 else if (userAgent.Contains("NT 5.2"))
 {
 osVersion = "Windows Server 2003";
 }
 else if (userAgent.Contains("NT 5.1"))
 {
 osVersion = "Windows XP";
 }
 else if (userAgent.Contains("NT 6.0"))
 {
 osVersion = "Windows Vista/Server 2008";
 }
 else if (userAgent.Contains("NT 5"))
 {
 osVersion = "Windows 2000";
 }
 else if (userAgent.Contains("Linux"))
 {
 osVersion = "Linux";
 }
 else if (userAgent.Contains("NT 4"))
 {
 osVersion = "Windows NT4";
 }
 else if (userAgent.Contains("Me"))
 {
 osVersion = "Windows Me";
 }
 else if (userAgent.Contains("98"))
 {
 osVersion = "Windows 98";
 }
 else if (userAgent.Contains("95"))
 {
 osVersion = "Windows 95";
 }
 else if (userAgent.Contains("Mac"))
 {
 osVersion = "Mac";
 }
 else if (userAgent.Contains("Unix"))
 {
 osVersion = "UNIX";
 }

 else if (userAgent.Contains("SunOS"))
 {
 osVersion = "SunOS";
 }
 return osVersion;
 }
 public bool IsReusable {
 get {
 return false;
 }
 }

}

实现调用:
因为我把本程序部署在dag.wzu.edu.cn域名的服务器上,所以可以直接通过html+js代码就可以实现客户端的访问统计功能

<img  style="width:0px;height:0px;border:0px" id="statistics_id_fei" /><script type="text/javascript"> window.onload = function () { document.getElementById("statistics_id_fei").src = "http://dag.wzu.edu.cn/statistics.ashx?d="+ encodeURI(document.referrer).replace('&', '`') + "&u=" + encodeURI(document.location.href).replace('&', '`') + "&x=" + Math.random(); }</script>

说明:

1、在执行文件ashx上,采用了多种可选方式,其中数据库方面有直接拼接sql,sql参数方法,存储过程调用三种方法;
2、为了能够是js中的onload事件能够执行,加载完ashx文件后,生成一个对应的图片文件,采用了直接加载cache缓存方式实现;
3、只要直接把这里的html和js代码复制到你网址的直接的任何地方就可以实现自己站点的访问量统计。

发表在 Asp.Net, MySQL | 评论关闭

dropdownlist 省市县三级联动

因为开发环境是asp.net所以采用服务器端控件实现三级联动

HTML代码:


<asp:DropDownList ID="ddls" runat="server"></asp:DropDownList>

<asp:DropDownList ID="ddlc" runat="server"></asp:DropDownList>

<asp:DropDownList ID="ddlx" runat="server"></asp:DropDownList>

<input id="h_s" type="hidden"  runat="server"/>
 <input id="h_c" type="hidden" runat="server"/>
 <input id="h_a" type="hidden" runat="server"/>

note:hidden 是为了以后在c#代码中能够取到dropdownlist选中的值

js代码:

$(document).ready(function () {
 //选择省操作
 $("#ddls").change(function () {
 var selects_v = $("#ddls").val();
 $("#h_s").val(selects_v);
 if (selects_v == "0") {
 $("#ddlc option:first").attr("selected", "selected");
 $("#ddlx option:first").attr("selected", "selected");
 $("#ddlc").attr("disabled", "disabled");
 $("#ddlx").attr("disabled", "disabled");
 }
 else {
 $("#ddlc option").remove();
 $("#ddlc").attr("disabled", "");
 $("#ddlx option:first").attr("selected", "selected");
 $("#ddlx").attr("disabled", "disabled");
 $("#ddlc").append("<option value='0'>请选择市</option>");
 $.getJSON("./ashx/select_province.ashx?s=" + selects_v + "&a" + Math.random(), function (data) {
 $.each(data.root, function (id, item) {
 $("#ddlc").append("<option value='" + item.code + "'>" + item.name + "</option>");
 });
 });
 }
 });
 //ddlx选择市
 $("#ddlc").change(function () {
 var s_c = $("#ddlc").val();
 $("#h_c").val(s_c);
 if (s_c == "0") {
 $("#ddlx")[0].selectedIndex = 0;
 $("#ddlx").attr("disabled", "disabled");
 }
 else {
 $("#ddlx option").remove();
 $("#ddlx").attr("disabled", "");
 $("#ddlx").append("<option value='0'>请选择县</option>");
 $.getJSON("./ashx/select_city.ashx?s=" + s_c + "&a" + Math.random(), function (data) {
 $.each(data.root, function (id, item) {
 $("#ddlx").append("<option value='" + item.code + "'>" + item.name + "</option>");
 });
 });
 }
 });
 //选择对应的县
 $("#ddlx").change(function () {
 var s_a = $("#ddlx").val();
 $("#h_a").val(s_a);
 });
});

c#初始化dropdownlist数据

protected void Bindddls()
 {
 SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.StoredProcedure, "get_province");
 ddls.DataTextField = "name";
 ddls.DataValueField = "code";
 ddls.DataSource = dr;
 ddls.DataBind();
 dr.Close();
 ddls.Items.Insert(0,new ListItem("请选择省", "0"));
 }
 protected void Bindddlc()
 {
 string sValue = ddls.SelectedValue;
 if (sValue == "0")
 {
 ddlc.Items.Add(new ListItem("请选择市", "0"));
 ddlc.Enabled = false;
 }
 else
 {
 SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.StoredProcedure, "get_city");
 ddlc.DataTextField = "name";
 ddlc.DataValueField = "code";
 ddlc.DataSource = dr;
 ddlc.DataBind();
 dr.Close();
 ddlc.Items.Insert(0,new ListItem("请选择市", "0"));
 }
 }
 protected void Bindddla()
 {
 string cValue = ddlc.SelectedValue;
 if (cValue == "0")
 {
 ddlx.Items.Add(new ListItem("请选择县", "0"));
 ddlx.Enabled = false;
 }
 else
 {
 SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.StoredProcedure, "get_area");
 ddlx.DataTextField = "name";
 ddlx.DataValueField = "code";
 ddlx.DataSource = dr;
 ddlx.DataBind();
 dr.Close();
 ddlx.Items.Insert(0, new ListItem("请选择县", "0"));
 }
 }

ashx文件代码

select_province.ashx文件


if (HttpContext.Current.Request["s"] != null)
 {
 string sv = HttpContext.Current.Request["s"].ToString();
 SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.StoredProcedure
 , "get_city", new SqlParameter("@sid", sv));
 string json = Object_Json.ToJson(dr);
 dr.Close();
 HttpContext.Current.Response.Write(json);
 HttpContext.Current.Response.End();
 }
 else
 {
 HttpContext.Current.Response.Write("error");
 HttpContext.Current.Response.End();
 }

select_city.ashx文件

if (HttpContext.Current.Request["s"] != null)
 {
 string sv = HttpContext.Current.Request["s"].ToString();
 System.Data.SqlClient.SqlDataReader dr = xifenfei.mssql.SqlHelper.ExecuteReader(xifenfei.mssql.SqlHelper.ConnectionStringLocalTransaction, System.Data.CommandType.StoredProcedure
 , "get_area", new System.Data.SqlClient.SqlParameter("@sid", sv));
 string json = Object_Json.ToJson(dr);
 dr.Close();
 HttpContext.Current.Response.Write(json);
 HttpContext.Current.Response.End();
 }
 else
 {
 HttpContext.Current.Response.Write("error");
 HttpContext.Current.Response.End();
 }

效果:

发表在 Asp.Net, JavaScript | 评论关闭

Google Map 使用

因项目中要调用google地图,最近花了点时间阅读了下google  map 的api,google map的使用大致如下:

1、注册使用google地图api

地址为:http://code.google.com/intl/zh-CN/apis/maps/signup.html

2、阅读google 地图api

地址为:http://code.google.com/intl/zh-CN/apis/maps/documentation/javascript/basics.html

3、测试google 地图api

(1)一个地点定位并标明位置

  • 1添加JavaScript应用
<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=true;key=ABQIAAAAEBGBYRn2zjKnkDk35kXn-BT2yXp_ZAY8_ufC3CFXhHIE1NvwkxSQ7sVn7B1Wp1t33kMiQlXQKL-t7g"  type="text/javascript"></script>

note:我的key是localhost,如果其他测试请重新申请

  • 主要的js函数
function load(comName, comAddress, comPhone, dimensionality, longitude) {     //加载地图
 if (GBrowserIsCompatible()) {
 var map = new GMap2(document.getElementById("map"));
 map.addControl(new GSmallMapControl());    //放大缩小
 map.addControl(new GMapTypeControl());     //地图种类
 map.enableScrollWheelZoom();    //启用鼠标滚轮
 var point = new GLatLng(dimensionality, longitude);     // 位置
 map.setCenter(point, 14);   //地图坐标 三个参数分别为 "纬度" "经度" "比例尺"

 function createMarker(point, address, name, tel) {  //创建标记内容及标记的鼠标事件
 var marker = new GMarker(point);
 var html = '<div style="font-size:10px;">' +
 '<a >公司名称:' + name + '</a><br/>' +
 '<a >公司地址:' + address + '</a><br/>' +
 '<a >联系电话:' + tel + '</a>' +
 '</div>';
 GEvent.addListener(marker, "mouseover", function () {
 marker.openInfoWindowHtml(html);
 });
 GEvent.addListener(marker, "mouseout", function () {
 marker.closeInfoWindow();
 });
 GEvent.addListener(marker, "click", function () {
 map.setCenter(point, 16);
 //marker.openInfoWindowHtml(html);
 });
 return marker;
 }

 map.addOverlay(createMarker(point, comAddress, comName, comPhone)); //加入标记
 }
 else {
 alert("您的浏览器不支持Google地图,请升级到最新版再尝试");
 }
}

参数说明:

公司名称comName
公司地址comAddress
公司电话comPhone
公司维度 dimensionality
公司经度 longitude

  • 放置地图的html位置
<div id="map" style="width: 700px; height: 300px"></div>
  • js调用
  • $(document).ready(function () {
     load('飞飞','温州大学','13736325515',27.924806,120.699492);
     });

2、多个地点定位并标明

  • js代码
function load(dimensionality, longitude) {     //加载地图
 if (GBrowserIsCompatible()) {
 var map = new GMap2(document.getElementById("map"));
 map.addControl(new GSmallMapControl());    //放大缩小
 map.addControl(new GMapTypeControl());     //地图种类
 map.enableScrollWheelZoom();    //启用鼠标滚轮
 var point = new GLatLng(dimensionality, longitude);     // 位置
 map.setCenter(point, 14);   //地图坐标 三个参数分别为 "纬度" "经度" "比例尺"
 return map;
 }
}

function createMarker(map,point, address, name, tel) {  //创建标记内容及标记的鼠标事件
 var marker = new GMarker(point);
 var html = '<div style="font-size:12px;">' +
 '<a >公司名称:' + name + '</a><br/>' +
 '<a >公司地址:' + address + '</a><br/>' +
 '<a >联系电话:' + tel + '</a>' +
 '</div>';
 GEvent.addListener(marker, "mouseover", function () {
 marker.openInfoWindowHtml(html);
 });
 GEvent.addListener(marker, "mouseout", function () {
 marker.closeInfoWindow();
 });
 GEvent.addListener(marker, "click", function () {
 map.setCenter(point, 16);
 });
 return marker;
}

function Add_Marker(map,comName, comAddress, comPhone, dimensionality, longitude) {
 var point = new GLatLng(dimensionality, longitude);     // 设置标记
 map.addOverlay(createMarker(map,point, comAddress, comName, comPhone)); //加入标记
}
  • c#调用

string a = @" var map = load(27.924806, 120.699492);
 Add_Marker(map,'温州大学', '茶山', '13736325515', 27.924806, 120.699492);
 Add_Marker(map,'温州大学111', '茶山22', '13736325515',27.925526, 120.700479);";
 Page.ClientScript.RegisterStartupScript(this.GetType(), "ab", a, true);

note:

在一个定位的js中,也可以采用添加多个

var point1 = new GLatLng(27.925526, 120.700479);
 map.addOverlay(createMarker(point1, 'comAddress', 'comName', 'comPhone')); 

来实现多个地点的定位和标明,但是这样可控制性太差

发表在 Asp.Net, JavaScript | 评论关闭