我一直存在的问题是,在我注册用户名/密码后,请尝试登录,如果我获得了任何字母或登录/密码的任何字母或数字,则可以接受它,例如我的用户名是fof,我的密码是tog,我输入用户名,因为它会接受它。
这是用python闲置3.7写的代码:
if Game == "1":
username = input("Please enter your username: ")
if username in open("Names.txt").read(): #fix
print ("Welcome " + username)
password = input("Please enter your password: ")
if password in open("Passwords.txt").read():
print ("success!")
else:
print("Username incorrect!")
对您需要的解释:
您需要查找文件中单词匹配的 eact ,而不仅仅是in
,因为这始终会返回True
,因此它将绕过:
一个例子:
名称列表:
Fof
Abc
Def
,然后:
import re
text = input("enter Name to be searched:")
NamesFile = open("NamesList.txt", "r")
for line in NamesFile:
if re.search(r"b" + text + r"b", line):
print(line)
else:
print("Name not found")
break
输出:
enter Name to be searched:Fof
Fof
在另一种情况下:
enter Name to be searched:f
Name not found
如果您以自己的方式存储登录和密码,那么一个用户可以使用另一个用户的密码,反之亦然。最好将登录名配对一起存储:
文件credentials.json
:
{"Fof": "tog"}
代码:
import json
with open('credentials.json') as f:
credentials = json.load(f)
username = input('Please enter your username: ')
if credentials.get(username):
print('Welcome {}'.format(username))
password = input('Please enter your password: ')
if credentials[username] == password:
print('success!')
else:
print('Username incorrect!')
让我们尝试入侵:
Please enter your username: f
Username incorrect!
成功登录:
Please enter your username: Fof
Welcome Fof
Please enter your password: tog
success!