记录游客页面访问IP的简易实现代码 (asp.net+txt)
程序员文章站
2024-03-08 23:23:16
记录处理类复制代码 代码如下: using system; using system.io; /// /// file ///
记录处理类
using system;
using system.io;
/// <summary>
/// file
/// </summary>
public class file
{
protected string filepath;
/// <summary>
/// file构造
/// </summary>
/// <param name="filepath">需要操作的文本路径</param>
public file(string filepath)
{
this.filepath = filepath;
}
/// <summary>
/// 文本内容写入
/// </summary>
/// <param name="info">写入内容</param>
public void filewrite(string info)
{
try
{
fileinfo file = new fileinfo(filepath);
if (!file.exists)
{
using (streamwriter sw = file.createtext())
{
sw.writeline(info);
}
}
else
{
using (streamwriter sw = file.appendtext())
{
sw.writeline(info);
}
}
}
catch(filenotfoundexception filece)
{
throw filece;
}
catch (exception ce)
{
throw ce;
}
}
}
页面调用代码
public partial class _default : system.web.ui.page
{
protected void page_load(object sender, eventargs e)
{
if (!ispostback)
{
//判断当前用户是否访问过,只记录未访问过的用户
if (request.cookies["isexitsip"] == null)
{
//每天一个记事本.txt
string filename = string.format("{0}{1}{2}", datetime.now.year.tostring(), datetime.now.month.tostring(), datetime.now.day.tostring());
file file = new file(server.mappath("~/test/" + filename + ".txt"));
file.filewrite(request.userhostname);
//给正在访问的用户添加已访问标记
httpcookie cokie = new httpcookie("isexitsip");
cokie.values.add("ip", request.userhostname);
response.appendcookie(cokie);
}
}
}
}
复制代码 代码如下:
using system;
using system.io;
/// <summary>
/// file
/// </summary>
public class file
{
protected string filepath;
/// <summary>
/// file构造
/// </summary>
/// <param name="filepath">需要操作的文本路径</param>
public file(string filepath)
{
this.filepath = filepath;
}
/// <summary>
/// 文本内容写入
/// </summary>
/// <param name="info">写入内容</param>
public void filewrite(string info)
{
try
{
fileinfo file = new fileinfo(filepath);
if (!file.exists)
{
using (streamwriter sw = file.createtext())
{
sw.writeline(info);
}
}
else
{
using (streamwriter sw = file.appendtext())
{
sw.writeline(info);
}
}
}
catch(filenotfoundexception filece)
{
throw filece;
}
catch (exception ce)
{
throw ce;
}
}
}
页面调用代码
复制代码 代码如下:
public partial class _default : system.web.ui.page
{
protected void page_load(object sender, eventargs e)
{
if (!ispostback)
{
//判断当前用户是否访问过,只记录未访问过的用户
if (request.cookies["isexitsip"] == null)
{
//每天一个记事本.txt
string filename = string.format("{0}{1}{2}", datetime.now.year.tostring(), datetime.now.month.tostring(), datetime.now.day.tostring());
file file = new file(server.mappath("~/test/" + filename + ".txt"));
file.filewrite(request.userhostname);
//给正在访问的用户添加已访问标记
httpcookie cokie = new httpcookie("isexitsip");
cokie.values.add("ip", request.userhostname);
response.appendcookie(cokie);
}
}
}
}
上一篇: java使用HttpSession实现QQ访问记录
下一篇: java实现用户自动登录