Groups | Blog | Home
all groups > inetserver asp general > june 2005 >

inetserver asp general : Simple username checking


nix
6/7/2005 12:00:00 AM
Hi

I want to create a simple user validation script without using a database.

Let's say I have a text file in my asp folder with a list of valid
usernames. How can i do something like the following using ASP?


// IN: sUsername

try
AUsernameList:=TStringlist.create;
try
AUsernameList.loadfromfile('usernames.txt');
bValidUser:=IndexOf(sUsername)>=0;
except
// OUTPUT AN ERROR TO THE USER
end;
finally
AUsernameList.free;
end;

// OUT: bValidUser


Thanks

nix

Dave Anderson
6/7/2005 1:39:45 AM
[quoted text, click to view]

Supposing usernames.txt has a different username on each line, this general
approach would do...

<%@ Language=JScript %><%
var UserID = Request.Form("UserID").Item,
filePathName = "c:\\your\\path\\usernames.txt",
fso = Server.CreateObject("Scripting.FileSystemObject"),
file = fso.OpenTextFile(filePathName).ReadAll(),
re = new RegExp("\r\n"+UserID+"\r\n", "i")

if (re.test(file)) {
// success code
} else {
// failure code
}
%>

Note: I used a regular expression to avoid iteration. String.indexOf() could
be used as well. Either should test for being a whole match -- a simple
indexOf("n") would match "nix" if you are not careful. That's why I
bookended the matching string with newline characters. This, of course,
requires a newline character before the first entry and another after the
last.



Of course, your text file might contain more than just user ids. Suppose
each line contained UserID, last name, and first name, tab-delimited. Then
you would want an iterative approach. Here's one:

var UserID = Request.Form("UserID").Item,
filePathName = "c:\\your\\path\\usernames.txt",
fso = Server.CreateObject("Scripting.FileSystemObject"),
file = fso.OpenTextFile(filePathName).ReadAll(),
re = new RegExp("\r\n"+UserID+"\t", "i"),
users = file.split("\r\n")

if (re.test(file)) {
re = new RegExp(UserID+"\t", "i")
for (var i=0; i<users.length; i++)
if (re.test(users[i])) {
var UserID = users[i].split("\t")[0],
LastName = users[i].split("\t")[1],
FirstName = users[i].split("\t")[2]
// remainder of success code
}
} else {
// failure code
}



--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms. Please do not contact
me directly or ask me to contact you directly for assistance. If your
question is worth asking, it's worth posting.

nix
6/9/2005 12:00:00 AM
Excellent,

Dave

AddThis Social Bookmark Button