当前位置: 移动技术网 > IT编程>开发语言>.net > 记录游客页面访问IP的简易实现代码 (asp.net+txt)

记录游客页面访问IP的简易实现代码 (asp.net+txt)

2017年12月12日  | 移动技术网IT编程  | 我要评论

泰兴仙鹤湾论坛,逆境出人才的事例,dy289

记录处理类
复制代码 代码如下:

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);
}
}
}
}

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网