有时要想做一个纯粹的计数流量器,可以采用XML来替代数据库,速度特快.
counter.aspx
counter.xml
counter.aspx
<%@ Page Language="C#" Debug="true" codepage="936"%>
<%@ Import Namespace="System"%>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Xml" %>
<script language="c#" runat="server">
//*****************************************************
// 调用方法
// 计数: counter.aspx?count=yes
// 显示: counter.aspx
//*****************************************************
public string Exec = string.Empty;
private void Page_Load(object sender, System.EventArgs e)
{
string fileName = "counter.xml";
string filePath = Server.MapPath(fileName);
if ( File.Exists(filePath) )
{
Exec = Request.QueryString["count"];
if ( Exec != null && Exec != string.Empty )
{
XmlDocument xdoc = new XmlDocument();
try
{
xdoc.Load(filePath);
XmlNodeList xnl = xdoc.GetElementsByTagName("Datas");
XmlNode xn = xnl[0];
XmlNode SettingNode = xn.FirstChild;
int Counts = int.Parse(SettingNode.InnerText) + 1;
SettingNode.InnerText = Counts.ToString();
xdoc.Save(filePath);
}
finally
{
xdoc = null;
}
}
else
{
XmlDocument xdoc = new XmlDocument();
try
{
xdoc.Load(filePath);
XmlNodeList xnl = xdoc.GetElementsByTagName("Datas");
XmlNode xn = xnl[0];
XmlNode SettingNode = xn.FirstChild;
int Counts = int.Parse(SettingNode.InnerText);
Response.Write(Counts.ToString());
}
finally
{
xdoc = null;
}
}
}
else
Response.Write("NOT");
}
</script>
<%@ Import Namespace="System"%>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Xml" %>
<script language="c#" runat="server">
//*****************************************************
// 调用方法
// 计数: counter.aspx?count=yes
// 显示: counter.aspx
//*****************************************************
public string Exec = string.Empty;
private void Page_Load(object sender, System.EventArgs e)
{
string fileName = "counter.xml";
string filePath = Server.MapPath(fileName);
if ( File.Exists(filePath) )
{
Exec = Request.QueryString["count"];
if ( Exec != null && Exec != string.Empty )
{
XmlDocument xdoc = new XmlDocument();
try
{
xdoc.Load(filePath);
XmlNodeList xnl = xdoc.GetElementsByTagName("Datas");
XmlNode xn = xnl[0];
XmlNode SettingNode = xn.FirstChild;
int Counts = int.Parse(SettingNode.InnerText) + 1;
SettingNode.InnerText = Counts.ToString();
xdoc.Save(filePath);
}
finally
{
xdoc = null;
}
}
else
{
XmlDocument xdoc = new XmlDocument();
try
{
xdoc.Load(filePath);
XmlNodeList xnl = xdoc.GetElementsByTagName("Datas");
XmlNode xn = xnl[0];
XmlNode SettingNode = xn.FirstChild;
int Counts = int.Parse(SettingNode.InnerText);
Response.Write(Counts.ToString());
}
finally
{
xdoc = null;
}
}
}
else
Response.Write("NOT");
}
</script>
counter.xml
<?xml version="1.0" encoding="utf-8"?>
<DataList>
<Datas>
<Count>0</Count>
</Datas>
</DataList>
<DataList>
<Datas>
<Count>0</Count>
</Datas>
</DataList>