试用EF开发WEB应用程序(12): 网页计数器
题记:用“易语言.飞扬”(EF)开发WEB应用程序,此前还没有先例。但因为EF本地开发包(EFNDK)已经发布,用C/C++开发一个EF类库,使其支持EF开发WEB应用程序,应该并非难事。当然也可想而知,其中必有诸多难点有待解决。此系列文章,为本人探索过程之记录,对外人未必有多大价值。如有网友乐观其事,还请理性待之。作者:liigo。转载请务必注明出处:http://blog.csdn.net/liigo/。在线留言。
试用EF开发WEB应用程序(12): 网页计数器
又一个 EFCGI 应用实例,网页计数器。
网页计数器,在目前网络上应用十分普遍,其功能是,对网页的被浏览次数进行统计,网页每被浏览一次,该计数值就被加一。
我实现的这个网页计数器,是一个通用网页计数器。即,它可以给任意人、任意网页提供计数服务,只要你事先申请得到一个ID。在线申请计数器。
以下是本网页计数器的“易语言.飞扬”(EF)源代码:
引入 fastcgi, sqlite;
公开 类 启动类
{
静态 Sqlite数据库 _db;
公开 静态 启动()
{
_db = new Sqlite数据库();
_db.打开("../db/pagecounter.file", true);
if(_db.表是否存在("pagecounter") == false)
createTable();
FCGI fcgi = new FCGI;
while(fcgi.Accept() >= 0)
{
if(fcgi.REQUEST_METHOD.到小写() == "post")
fcgi.ReadContentAsQueryString();
文本 html = html_template.替换全部("$(title)", "网页计数器(由EF开发)");
string pathinfo = fcgi.PATH_INFO();
string message;
//处理提交的表单(form)
if(pathinfo == "/submit")
{
int id;
if(fcgi.QUERY_STRING("username") != "" || fcgi.QUERY_STRING("remark") != "")
id = registerID(fcgi);
fcgi.Output("Location: /efcgi/pagecounter.efcgi/id" + id.到文本() + " ");
continue;
}
//显示处理结果
if(pathinfo.左边(3) == "/id")
{
string id = pathinfo.右边(pathinfo.长度 - 3); // "/id123" -> "123"
if(id == "0")
{
message += "<p><b>对不起,操作失败。请尝试重新申请。</b></p>";
}
else
{
message += "<p><b>恭喜,注册成功,id为 " + id + "(请务必记住)。</b></p>";
message += "<p>欲使用此计数器,请将以下代码放到HTML中任意位置:</p>";
message += "<p><pre><script src="http://liigo.com/efcgi/pagecounter.efcgi?id=" + id + ""></script></pre></p>";
}
}
//根据id查计数器, 并返回相应的js代码
if(fcgi.QUERY_STRING("id") != "")
{
通用型 count = _db.读字段值("pagecounter", "count", Sqlite字段类型.整数, "id=" + fcgi.QUERY_STRING("id"));
count = count.取整数() + 1;
_db.执行SQL("update pagecounter set count = " + count + " where id=" + fcgi.QUERY_STRING("id"));
fcgi.Output("Content-type: application/x-javascript document.write("" + count.取整数().到文本() + "");");
continue;
}
html = html.替换("$(message)", message);
fcgi.Output(html.到UTF8());
}
_db.关闭();
}
private static createTable()
{
Sqlite表结构 tdef = new Sqlite表结构();
tdef.添加字段("id", Sqlite字段类型.主键整数);
tdef.添加字段("count", Sqlite字段类型.整数);
tdef.添加字段("username", Sqlite字段类型.文本);
tdef.添加字段("remark", Sqlite字段类型.文本);
_db.创建表("pagecounter", tdef);
}
//注册计数器,返回id
private static int registerID(FCGI fcgi)
{
string username = fcgi.QUERY_STRING("username");
if(username == "") username = "EF爱好者";
string initcount = fcgi.QUERY_STRING("initcount");
string sql = "insert into pagecounter(username, remark, count) values('" + username + "','" + fcgi.QUERY_STRING("remark") + "'," + initcount + ")";
if(_db.执行SQL(sql))
return (int)_db.取最新插入ID();
else
return 0;
}
常量 文本 html_template = ["Content-type: text/html
<html><head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>$(title)</title>
</head>
<body>
<h1>$(title)</h1>
<p>网页记数器:为单个网页提供流量统计。</p>
$(message)
<hr></hr>
<h3>您还没有计数器?请先申请:</h3>
<form method="post" action="/efcgi/pagecounter.efcgi/submit">
<p>姓名:<input type="text" name="username" size=20 /></p>
<p>说明:<input type="text" name="remark" size=80 /></p>
<p>计数器初始值:<input type="text" name="initcount" size=10 value="0"/></p>
<p><input type="submit" value="提交"></input></p>
</form>
<hr></hr>
<p>by liigo, <a href="http://blog.csdn.net/liigo/">http://blog.csdn.net/liigo/</a></p>
</body>
</html>
"];
}
公开 类 启动类
{
静态 Sqlite数据库 _db;
公开 静态 启动()
{
_db = new Sqlite数据库();
_db.打开("../db/pagecounter.file", true);
if(_db.表是否存在("pagecounter") == false)
createTable();
FCGI fcgi = new FCGI;
while(fcgi.Accept() >= 0)
{
if(fcgi.REQUEST_METHOD.到小写() == "post")
fcgi.ReadContentAsQueryString();
文本 html = html_template.替换全部("$(title)", "网页计数器(由EF开发)");
string pathinfo = fcgi.PATH_INFO();
string message;
//处理提交的表单(form)
if(pathinfo == "/submit")
{
int id;
if(fcgi.QUERY_STRING("username") != "" || fcgi.QUERY_STRING("remark") != "")
id = registerID(fcgi);
fcgi.Output("Location: /efcgi/pagecounter.efcgi/id" + id.到文本() + " ");
continue;
}
//显示处理结果
if(pathinfo.左边(3) == "/id")
{
string id = pathinfo.右边(pathinfo.长度 - 3); // "/id123" -> "123"
if(id == "0")
{
message += "<p><b>对不起,操作失败。请尝试重新申请。</b></p>";
}
else
{
message += "<p><b>恭喜,注册成功,id为 " + id + "(请务必记住)。</b></p>";
message += "<p>欲使用此计数器,请将以下代码放到HTML中任意位置:</p>";
message += "<p><pre><script src="http://liigo.com/efcgi/pagecounter.efcgi?id=" + id + ""></script></pre></p>";
}
}
//根据id查计数器, 并返回相应的js代码
if(fcgi.QUERY_STRING("id") != "")
{
通用型 count = _db.读字段值("pagecounter", "count", Sqlite字段类型.整数, "id=" + fcgi.QUERY_STRING("id"));
count = count.取整数() + 1;
_db.执行SQL("update pagecounter set count = " + count + " where id=" + fcgi.QUERY_STRING("id"));
fcgi.Output("Content-type: application/x-javascript document.write("" + count.取整数().到文本() + "");");
continue;
}
html = html.替换("$(message)", message);
fcgi.Output(html.到UTF8());
}
_db.关闭();
}
private static createTable()
{
Sqlite表结构 tdef = new Sqlite表结构();
tdef.添加字段("id", Sqlite字段类型.主键整数);
tdef.添加字段("count", Sqlite字段类型.整数);
tdef.添加字段("username", Sqlite字段类型.文本);
tdef.添加字段("remark", Sqlite字段类型.文本);
_db.创建表("pagecounter", tdef);
}
//注册计数器,返回id
private static int registerID(FCGI fcgi)
{
string username = fcgi.QUERY_STRING("username");
if(username == "") username = "EF爱好者";
string initcount = fcgi.QUERY_STRING("initcount");
string sql = "insert into pagecounter(username, remark, count) values('" + username + "','" + fcgi.QUERY_STRING("remark") + "'," + initcount + ")";
if(_db.执行SQL(sql))
return (int)_db.取最新插入ID();
else
return 0;
}
常量 文本 html_template = ["Content-type: text/html
<html><head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>$(title)</title>
</head>
<body>
<h1>$(title)</h1>
<p>网页记数器:为单个网页提供流量统计。</p>
$(message)
<hr></hr>
<h3>您还没有计数器?请先申请:</h3>
<form method="post" action="/efcgi/pagecounter.efcgi/submit">
<p>姓名:<input type="text" name="username" size=20 /></p>
<p>说明:<input type="text" name="remark" size=80 /></p>
<p>计数器初始值:<input type="text" name="initcount" size=10 value="0"/></p>
<p><input type="submit" value="提交"></input></p>
</form>
<hr></hr>
<p>by liigo, <a href="http://blog.csdn.net/liigo/">http://blog.csdn.net/liigo/</a></p>
</body>
</html>
"];
}
在注册得到ID之后,请将以下代码放置到欲添加计数器的HTML网页源代码中:
这里有一个使用了此网页计数器的测试网页。
浙公网安备 33010602011771号