For Programmers: Free Programming Magazines  


Home > Archive > ASP > June 2005 > Simple username checking









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Simple username checking
nix

2005-06-07, 3:55 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

2005-06-07, 8:55 am

nix wrote:
> 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?
>
> 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;


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

2005-06-09, 8:55 am

Excellent,

Dave


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com