Record visitor page access IP addresses (Asp.net for notepad)

Source: Internet
Author: User

 

Record Processing

  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

  Public     Partial     Class  _ Default: system. Web. UI. Page
{
Protected Void Page_load ( Object Sender, eventargs E)
{
If ( ! Ispostback)
{
// Checks whether the current user has accessed the website and records only the users that have not accessed the website.
If (Request. Cookies [ " Isexitsip " ] = Null )
{
// Upload a TXT file every day
String Filename = String . Format ( " {0} {1} {2} " , Datetime. Now. year. tostring (), datetime. Now. Month. tostring (), datetime. Now. Day. tostring ());
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.