Using ASP file operation to realize user management

Source: Internet
Author: User
Tags exit chr functions modify readline
User management is simple, easy to use, high development efficiency, etc. are the advantages of ASP, but its system operation aspects such as file operation
And so on is relatively weak. To implement this functionality, only the method of invoking the component is taken.
The FileSystemObject object is an example. After the ASP is installed, the object is registered in the system.
However, the function is very general, only text-type files can be manipulated. But it can also satisfy some of our
I need it. A friend of a colleague of mine had applied for a space that supports ASP on the net a few days ago, want to
To achieve such functions as user registration, information modification, user logoff, and do not want to use the database to achieve, because
To make a lot of settings. In fact, the use of FileSystemObject to manipulate text files can be achieved.
The idea is to create a text file for each message, such as name, gender, age, etc. The
The same information item that has a user is saved in the corresponding file. If each user's name information is saved
In the name file. And each user's information starts and ends with a special flag, and reads the information
Locate according to these flags. For example, the start sign uses "' Username '", and the end sign uses "' E '".
As long as it is not a commonly used string, it can be positioned as a flag. In addition, there are two documents that must be
Less: That is the user name, password file. To determine whether the user exists, user login is to use these two
Files are implemented. The following is the specific implementation of my Code.
Before the program was written, I got into a common function unit fun.inc, which is some comparison
A common function that is included and invoked by other ASP files.
The first is a few user information functions, including user name, filename, user information three parameters, are
String type in which the user information can also be multiple lines. The code is as follows:

Sub Writeuser (Username,filename,userinfo)
Dim FS, F
Set fs = CreateObject ("Scripting.FileSystemObject")
If FS. FileExists (FileName) = False Then
Fs. CreateTextFile (FileName)
End If
' To open a file as an addition
Set f = fs. OpenTextFile (FileName, 8)
' User information start flag
F.writeline ("'" & UserName & "")
F.writeline (UserInfo)
' User information End flag
F.writeline ("' E '")

F.close
Set F = Nothing
Set fs = Nothing
End Sub

Functions that read user information require only two parameters: User name and file name, user information as function
's return value, the idea is to find the start, end tag, and return the information between them. Generation
Code as follows:

Function Readuser (Username,filename)
Dim I
Dim s
DIM ret
Dim FS, F

Set fs = CreateObject ("Scripting.FileSystemObject")
If Fs. FileExists (FileName) =false Then
Set fs=nothing
Readuser= ""
Exit Function
End If
' Open file as read-only
Set f = fs. OpenTextFile (FileName, 1)

Do While F.atendofstream <> True
s = f.readline
If s = "'" & UserName & "'" Then
s = f.readline
Ret= ""
Do while S <> "' E '"
If ret= "" Then
RET = ret + S
Else
RET = ret + Chr (& Chr) +s
End If
s = f.readline
Loop
Exit do
End If
Loop

F.close
Set F = Nothing
Set fs = Nothing
Readuser = RET
End Function

The following is a function to delete the user information, including the username and file name two parameters. Program by line
Read the corresponding file, if the other user's information, then save in a string variable, otherwise do not
Save, and then rewrite the file with the contents of the variable. The code is as follows:

Sub DeleteUser (Username,filename)
Dim I
Dim s
Dim tmp
Dim FS, F

Set fs = CreateObject ("Scripting.FileSystemObject")
If Fs. FileExists (FileName) =false Then
Set fs=nothing
Exit Sub
End If
Set f = fs. OpenTextFile (FileName, 1)
Tmp= ""
Do While F.atendofstream <> True
s = f.readline
If s <> "'" & UserName & "" Then
If tmp= "" Then
TMP = TMP + S
Else
TMP = tmp + CHR & Chr (a) + s
End If
Else
Do while S <> "' E '"
s = f.readline
Loop
End If
Loop

F.close
Set f = fs. CreateTextFile (FileName, True)
F.writeline tmp

F.close
Set F = Nothing
Set fs = Nothing
End Sub

Modify the user information function and write user information function, with user name, file name, user information
Three parameters, except that the user information for the function is the updated information. Implementation of the program and deletion of the user's
The process is similar, delete the user does not save to delete the user's information to the variable, modify the user will update the letter
Save to the variable to replace the original information. The code is as follows:

Sub ModifyUser (Username,filename,newuserinfo)
Dim I
Dim s
Dim tmp
Dim FS, F

Set fs = CreateObject ("Scripting.FileSystemObject")
If Fs. FileExists (FileName) =false Then
Set fs=nothing
Exit Sub
End If
Set f = fs. OpenTextFile (FileName, 1)
Tmp= ""
Do While F.atendofstream <> True
s = f.readline
If s <> "'" & UserName & "" Then
If tmp= "" Then
TMP = TMP + S
Else
TMP = tmp + CHR & Chr (a) + s
End If
Else
Do while S <> "' E '"
s = f.readline
Loop
If tmp= "" Then
TMP = tmp + "'" & UserName & "'"
Else
TMP = tmp + CHR & Chr (a) + "'" & UserName & ""
End If
TMP = tmp + CHR & Chr (a) + Newuseri



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.