国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

記錄游客頁面訪問IP的簡易實現(xiàn)代碼 (asp.net+txt)

2024-07-10 12:42:38
字體:
供稿:網(wǎng)友
記錄處理類
代碼如下:
using System;
using System.IO;

/// <summary>
/// File
/// </summary>
public class File
{
protected string FilePath;

/// <summary>
/// File構(gòu)造
/// </summary>
/// <param name="filePath">需要操作的文本路徑</param>
public File(string filePath)
{
this.FilePath = filePath;
}

/// <summary>
/// 文本內(nèi)容寫入
/// </summary>
/// <param name="info">寫入內(nèi)容</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;
}
}
}

頁面調(diào)用代碼
代碼如下:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//判斷當(dāng)前用戶是否訪問過,只記錄未訪問過的用戶
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);
//給正在訪問的用戶添加已訪問標(biāo)記
HttpCookie cokie = new HttpCookie("IsExitsIP");
cokie.Values.Add("ip", Request.UserHostName);
Response.AppendCookie(cokie);
}
}
}
}
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 怀化市| 湘潭县| 武汉市| 聊城市| 高密市| 田阳县| 南平市| 瑞丽市| 济宁市| 仙游县| 寿阳县| 灯塔市| 永胜县| 衡阳市| 蓬莱市| 灌南县| 西乡县| 鄂州市| 花莲县| 大足县| 济宁市| 荥经县| 石阡县| 阳高县| 汶上县| 景德镇市| 鹤岗市| 平顶山市| 福泉市| 中江县| 合江县| 浑源县| 德安县| 长泰县| 晋城| 翁源县| 忻州市| 色达县| 枣强县| 普宁市| 固安县|