Record Visitor page Access IP Simple Implementation code (Asp.net+txt) _ Practical skills

Source: Internet
Author: User
Record Processing class
Copy Code code as follows:

Using System;
Using System.IO;

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

<summary>
File construction
</summary>
<param name= "FilePath" > Text path required for action </param>
Public File (String filePath)
{
This. FilePath = FilePath;
}

<summary>
Text content Write
</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 Code code as follows:

Public partial class _default:system.web.ui.page
{
protected voi d Page_Load (object sender, EventArgs e)
{
if (! IsPostBack)
{
///To determine whether the current user has been visited, to record only the users that have not been accessed
if (request.cookies["isexitsip"] = = null)
{
//daily Notepad . 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);
//Add an access token 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.