4 posts
Posted 09 March 2014 - 12:01 AM
So I wanted to make a login and register program where you register and the computer writes a file with the username as the name and inside the file writes
'pass' = pass
where pass (not in quotations) was already defined in the registry
The program can't seem to write to the file or load strings from the file. Can I get some assistance? Code below.
Login file code:
write("Username: ")
local username = read()
write("Password: ")
local password = read()
local h = fs.open(username, "r")
h.readLine("1")
if password == pass
then print("Logged in!")
else
print("Failed.")
h.close()
end
Register file code:
write("Username: ")
local user = read()
write("Password: ")
local pass = read("*")
local h = fs.open(user, "w")
h.write("'pass' = " .. pass)
print("Successfully Registered!")
h.close()
1281 posts
Posted 09 March 2014 - 01:48 AM
readLine does not work like that at all. First off, it does not accept any arguments, it just returns the next line in the file. It does not execute the line, so what it returns is just the string "'pass' = whatever", meaning you have to save this string into a variable, then extract the info you need from it.
Edited on 09 March 2014 - 12:49 AM
4 posts
Posted 09 March 2014 - 06:53 AM
readLine does not work like that at all. First off, it does not accept any arguments, it just returns the next line in the file. It does not execute the line, so what it returns is just the string "'pass' = whatever", meaning you have to save this string into a variable, then extract the info you need from it.
Okay, I went in and fixed a ton of code. All I need to know now is if there is a way to load an API and use a string from it, but like this:
os.loadAPI(username) <- username being a string the user already defined
if password = username.password then
etc.
How can I get it so it's looking for the user-defined string username and not the string "username"?
147 posts
Location
My Computer
Posted 09 March 2014 - 12:05 PM
That might sound a little confusing, so let me give you some code
write("Username:")
local username = read()
write("Password:")
local password = read()
h = fs.open(username, "r")
g = h.readLine() --Reads the first line and saves it into "g"
h.close() --Close the file here, it just makes it easier
if password == g then
print("Logged in!")
else
print("Failed to login")
end
Register code:
write("Username:")
local user = read()
write("Password:")
local pass = read("*")
if fs.exists(user) == false then --This is so that they cannot override existing users accounts
local h = fs.open(user, "w")
h.write(pass) -- We want the login code to read only the password from the file, not the password saved as a variable.
h.close()
else
print("That user exists already")
end
Hope it helps!
1852 posts
Location
Sweden
Posted 09 March 2014 - 02:48 PM
A tips, If you have a multiple user system I would suggest you should try to keep all users in one file
Login
local users = {}
local f = fs.open( "USERS.txt", "r" )
local users = textutils.unserialize( f.readAll() )
f.close()
local function login( name, pass )
for i = 1,#users do
if name == users[i].name then
if pass = users[i].pass then
return true
end
end
end
return true
end
repeat
term.clear()
term.setCursorPos(1,1)
write( "Username: " )
local username = read()
write( "Password: " )
local password = read("*")
local success = login( username, password )
until success
print("Logged in!")
Register
local users = {}
local function register( user )
for i = 1,#users do
if string.lower( user.name ) == string.lower( users[i].name ) then
return false
end
end
table.insert( users, user )
local f = fs.open("USERS.txt", "w")
f.writeLine( textutils.serialize( users ) )
f.close()
return true
end
repeat
term.clear()
term.setCursorPos(1,1)
local user = {}
write("Username: " )
user.name = read()
write("Password: " )
user.pass = read("*")
local success = register( user )
until success
print("Registered! :D/>/>/>")
Edited on 09 March 2014 - 01:50 PM
4 posts
Posted 09 March 2014 - 07:50 PM
That might sound a little confusing, so let me give you some code
write("Username:")
local username = read()
write("Password:")
local password = read()
h = fs.open(username, "r")
g = h.readLine() --Reads the first line and saves it into "g"
h.close() --Close the file here, it just makes it easier
if password == g then
print("Logged in!")
else
print("Failed to login")
end
Register code:
write("Username:")
local user = read()
write("Password:")
local pass = read("*")
if fs.exists(user) == false then --This is so that they cannot override existing users accounts
local h = fs.open(user, "w")
h.write(pass) -- We want the login code to read only the password from the file, not the password saved as a variable.
h.close()
else
print("That user exists already")
end
Hope it helps!
A tips, If you have a multiple user system I would suggest you should try to keep all users in one file
Login
local users = {}
local f = fs.open( "USERS.txt", "r" )
local users = textutils.unserialize( f.readAll() )
f.close()
local function login( name, pass )
for i = 1,#users do
if name == users[i].name then
if pass = users[i].pass then
return true
end
end
end
return true
end
repeat
term.clear()
term.setCursorPos(1,1)
write( "Username: " )
local username = read()
write( "Password: " )
local password = read("*")
local success = login( username, password )
until success
print("Logged in!")
Register
local users = {}
local function register( user )
for i = 1,#users do
if string.lower( user.name ) == string.lower( users[i].name ) then
return false
end
end
table.insert( users, user )
local f = fs.open("USERS.txt", "w")
f.writeLine( textutils.serialize( users ) )
f.close()
return true
end
repeat
term.clear()
term.setCursorPos(1,1)
local user = {}
write("Username: " )
user.name = read()
write("Password: " )
user.pass = read("*")
local success = register( user )
until success
print("Registered! :D/>/>/>/>")
Thanks for this guys! I didn't think to use h as the string!
22 posts
Location
The United States
Posted 12 March 2014 - 12:04 PM
Hi, I remember you saying you wanted a way to load APIs automatically on program run. That's relatively easy and if you're on a server and want to use an API, you can use this code in the beginning of your program so it automatically downloads the API and loads it. Make sure to change the Pastebin URL to your liking. Also, make sure you load the API with the same name as the program name.
The code:
http://pastebin.com/SVqKVtmf
1852 posts
Location
Sweden
Posted 12 March 2014 - 01:00 PM
Hi, I remember you saying you wanted a way to load APIs automatically on program run. That's relatively easy and if you're on a server and want to use an API, you can use this code in the beginning of your program so it automatically downloads the API and loads it. Make sure to change the Pastebin URL to your liking. Also, make sure you load the API with the same name as the program name.
The code:
http://pastebin.com/SVqKVtmf
You could just have used a code tag for that, But anway, It would be good to check if the file exists so it knows if it should download it or not
if not fs.exists( "<Filename>" ) then
shell.run( "pastebin", "get", "<Code>", "<Filename>" )
os.loadAPI( "<Filename>" )
end
I think it will error if it exists, Something like 'File exists'