Simple implementation code for recording visitor page access IP addresses (asp.net + txt)

Source: Internet
Author: User

Record Processing
Copy codeThe Code is as follows:
Using System;
Using System. IO;

/// <Summary>
/// File
/// </Summary>
Public class File
{
Protected string FilePath;

/// <Summary>
/// File structure
/// </Summary>
/// <Param name = "filePath"> text path to be operated </param>
Public File (string filePath)
{
This. FilePath = filePath;
}

/// <Summary>
/// Write text content
/// </Summary>
/// <Param name = "info"> write content </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;
}
}
}

Page call code
Copy codeThe Code is as follows:
Public partial class _ Default: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
If (! IsPostBack)
{
// Determine whether the current user has accessed the website and only records the users that have not accessed the website.
If (Request. Cookies ["IsExitsIP"] = null)
{
// Upload the TXT file every day
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 );
// Add an access tag to the user being accessed
HttpCookie cokie = new HttpCookie ("IsExitsIP ");
Cokie. Values. Add ("ip", Request. UserHostName );
Response. AppendCookie (cokie );
}
}
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.