栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > asp

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

asp 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

记录处理类
复制代码 代码如下:
using System;
using System.IO;

///
/// File
///

public class File
{
protected string FilePath;

///
/// File构造
///

/// 需要操作的文本路径
public File(string filePath)
{
this.FilePath = filePath;
}

///
/// 文本内容写入
///

/// 写入内容
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);
}
}
}
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/58918.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号